From eebcb641c8039bcf62b8ca5d5fdbdf2181789095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Mon, 6 Mar 2023 14:24:16 +0100 Subject: [PATCH] Grid with custom repository method --- .../test/src/Repository/BookRepository.php | 9 ++++ .../src/Subscription/Entity/Subscription.php | 6 ++- .../Subscription/Grid/SubscriptionGrid.php | 1 + .../Repository/SubscriptionRepository.php | 44 +++++++++++++++++++ .../Tests/Controller/SubscriptionUiTest.php | 5 +++ .../Tests/DataFixtures/ORM/subscriptions.yml | 3 ++ 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/Bundle/test/src/Subscription/Repository/SubscriptionRepository.php diff --git a/src/Bundle/test/src/Repository/BookRepository.php b/src/Bundle/test/src/Repository/BookRepository.php index 5cc1bb49f..8fe159909 100644 --- a/src/Bundle/test/src/Repository/BookRepository.php +++ b/src/Bundle/test/src/Repository/BookRepository.php @@ -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; @@ -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; + } } diff --git a/src/Bundle/test/src/Subscription/Entity/Subscription.php b/src/Bundle/test/src/Subscription/Entity/Subscription.php index fcfe981ea..91ba76853 100644 --- a/src/Bundle/test/src/Subscription/Entity/Subscription.php +++ b/src/Bundle/test/src/Subscription/Entity/Subscription.php @@ -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; @@ -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)] diff --git a/src/Bundle/test/src/Subscription/Grid/SubscriptionGrid.php b/src/Bundle/test/src/Subscription/Grid/SubscriptionGrid.php index 2f3e886fd..f916bd3ae 100644 --- a/src/Bundle/test/src/Subscription/Grid/SubscriptionGrid.php +++ b/src/Bundle/test/src/Subscription/Grid/SubscriptionGrid.php @@ -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') diff --git a/src/Bundle/test/src/Subscription/Repository/SubscriptionRepository.php b/src/Bundle/test/src/Subscription/Repository/SubscriptionRepository.php new file mode 100644 index 000000000..eec9df1a5 --- /dev/null +++ b/src/Bundle/test/src/Subscription/Repository/SubscriptionRepository.php @@ -0,0 +1,44 @@ +createQueryBuilder('o'); + + if (!$showArchived) { + $queryBuilder->andWhere( + $queryBuilder->expr()->isNull('o.archivedAt'), + ); + } + + return $queryBuilder; + } +} diff --git a/src/Bundle/test/src/Tests/Controller/SubscriptionUiTest.php b/src/Bundle/test/src/Tests/Controller/SubscriptionUiTest.php index caa18886a..f928ed893 100644 --- a/src/Bundle/test/src/Tests/Controller/SubscriptionUiTest.php +++ b/src/Bundle/test/src/Tests/Controller/SubscriptionUiTest.php @@ -55,6 +55,11 @@ public function it_allows_browsing_subscriptions(): void $this->assertStringContainsString(sprintf('Show', $subscriptions['subscription_doc']->getId()), $content); $this->assertStringContainsString(sprintf('Edit', $subscriptions['subscription_doc']->getId()), $content); $this->assertStringContainsString(sprintf('
', $subscriptions['subscription_doc']->getId()), $content); + + $this->assertStringContainsString('archived@example.com', $content); + $this->assertStringContainsString(sprintf('Show', $subscriptions['subscription_archived']->getId()), $content); + $this->assertStringContainsString(sprintf('Edit', $subscriptions['subscription_archived']->getId()), $content); + $this->assertStringContainsString(sprintf('', $subscriptions['subscription_archived']->getId()), $content); } /** @test */ diff --git a/src/Bundle/test/src/Tests/DataFixtures/ORM/subscriptions.yml b/src/Bundle/test/src/Tests/DataFixtures/ORM/subscriptions.yml index 079cc63e7..544f5e078 100644 --- a/src/Bundle/test/src/Tests/DataFixtures/ORM/subscriptions.yml +++ b/src/Bundle/test/src/Tests/DataFixtures/ORM/subscriptions.yml @@ -3,3 +3,6 @@ App\Subscription\Entity\Subscription: email: 'marty.mcfly@bttf.com' subscription_doc: email: 'doc.brown@bttf.com' + subscription_archived: + email: 'archived@example.com' + archivedAt: ''