Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tests] Add test for Grid with custom repository method #624

Open
wants to merge 1 commit into
base: poc-new-resource-metadata
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Bundle/test/src/Repository/BookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use App\Entity\Book;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepositoryTrait;

Expand All @@ -26,4 +27,12 @@ public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Book::class);
}

public function createListQueryBuilder(): QueryBuilder
{
$queryBuilder = $this->createQueryBuilder('o');
$queryBuilder->andWhere($queryBuilder->expr()->isNotNull('o.archivedAt'));

return $queryBuilder;
}
Comment on lines +31 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add this code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal of this PR is to test if the grid configuration is applied (such as a repository method defined on the grid)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but where is it used?

}
6 changes: 5 additions & 1 deletion src/Bundle/test/src/Subscription/Entity/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Subscription\Entity;

use App\Subscription\Form\Type\SubscriptionType;
use App\Subscription\Repository\SubscriptionRepository;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Metadata\ApplyStateMachineTransition;
use Sylius\Component\Resource\Metadata\Create;
Expand All @@ -40,12 +41,15 @@
#[ApplyStateMachineTransition(stateMachineTransition: 'reject')]
#[Delete]
#[Show(template: 'subscription/show.html.twig')]
#[ORM\Entity]
#[ORM\Entity(repositoryClass: SubscriptionRepository::class)]
class Subscription implements ResourceInterface
{
#[ORM\Column(type: 'string')]
public string $state = 'new';

#[ORM\Column(type: 'datetime', nullable: true)]
public ?\DateTimeInterface $archivedAt = null;

public function __construct(
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/test/src/Subscription/Grid/SubscriptionGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function getName(): string
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->setRepositoryMethod('createListQueryBuilder', ['showArchived' => true])
->orderBy('email', 'asc')
->addField(
StringField::create('email')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Subscription\Repository;

use App\Subscription\Entity\Subscription;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepositoryTrait;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class SubscriptionRepository extends ServiceEntityRepository implements RepositoryInterface
{
use ResourceRepositoryTrait;

public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Subscription::class);
}

public function createListQueryBuilder(bool $showArchived = false): QueryBuilder
{
$queryBuilder = $this->createQueryBuilder('o');

if (!$showArchived) {
$queryBuilder->andWhere(
$queryBuilder->expr()->isNull('o.archivedAt'),
);
}

return $queryBuilder;
}
}
5 changes: 5 additions & 0 deletions src/Bundle/test/src/Tests/Controller/SubscriptionUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public function it_allows_browsing_subscriptions(): void
$this->assertStringContainsString(sprintf('<a href="/admin/subscriptions/%s">Show</a>', $subscriptions['subscription_doc']->getId()), $content);
$this->assertStringContainsString(sprintf('<a href="/admin/subscriptions/%s/edit">Edit</a>', $subscriptions['subscription_doc']->getId()), $content);
$this->assertStringContainsString(sprintf('<form action="/admin/subscriptions/%s" method="post">', $subscriptions['subscription_doc']->getId()), $content);

$this->assertStringContainsString('<td>[email protected]</td>', $content);
$this->assertStringContainsString(sprintf('<a href="/admin/subscriptions/%s">Show</a>', $subscriptions['subscription_archived']->getId()), $content);
$this->assertStringContainsString(sprintf('<a href="/admin/subscriptions/%s/edit">Edit</a>', $subscriptions['subscription_archived']->getId()), $content);
$this->assertStringContainsString(sprintf('<form action="/admin/subscriptions/%s" method="post">', $subscriptions['subscription_archived']->getId()), $content);
}

/** @test */
Expand Down
3 changes: 3 additions & 0 deletions src/Bundle/test/src/Tests/DataFixtures/ORM/subscriptions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ App\Subscription\Entity\Subscription:
email: '[email protected]'
subscription_doc:
email: '[email protected]'
subscription_archived:
email: '[email protected]'
archivedAt: '<dateTimeBetween("-200 days", "now")>'