From f0361ffb67080485d3bc9a42fb5ea35d029b17c0 Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Thu, 13 Jun 2024 13:50:36 +0200 Subject: [PATCH] [EasyApiPlatform] Refactor tests (#1391) --- .phpstorm.meta.php | 6 + packages/EasyApiPlatform/composer.json | 5 +- .../OperationRequestInitiatorTrait.php | 27 --- .../Bridge/Symfony/Listeners/ReadListener.php | 3 +- .../Serializers/SerializerContextBuilder.php | 2 +- .../tests/AbstractApiTestCase.php | 48 +++++ .../tests/AbstractTestCase.php | 43 ++--- .../Symfony/AbstractSymfonyTestCase.php | 25 --- .../HttpKernelViewEventListenerTest.php | 83 --------- .../Symfony/Listeners/ReadListenerTest.php | 172 ++++-------------- .../tests/Bridge/Symfony/Stubs/KernelStub.php | 46 ----- .../Stubs/SerializerContextBuilderStub.php | 15 -- .../tests/Filters/AbstractFilterTestCase.php | 15 +- .../Filters/AdvancedSearchFilterTest.php | 6 +- .../app/config/packages/api_platform.php | 4 +- .../Fixtures/app/config/packages/doctrine.php | 19 +- .../ApiResource/Dummy.php | 6 +- .../ApiResource/DummyFriend.php | 2 +- .../ApiResource/EmbeddableDummy.php | 2 +- .../ApiResource/FourthLevel.php | 2 +- .../ApiResource/ParentDummy.php | 2 +- .../ApiResource/RelatedDummy.php | 6 +- .../ApiResource/RelatedOwnedDummy.php | 2 +- .../ApiResource/RelatedOwningDummy.php | 2 +- .../ApiResource/RelatedToDummyFriend.php | 2 +- .../ApiResource/ThirdLevel.php | 2 +- .../NameConverter/CustomConverter.php | 2 +- .../CustomPaginator/ApiResource/Category.php | 45 +++++ .../src/ReadListener/ApiResource/Question.php | 54 ++++++ .../tests/Paginators/CustomPaginatorTest.php | 82 +++------ .../SerializerContextBuilderTest.php | 72 -------- quality/phpstan.neon | 8 +- 32 files changed, 266 insertions(+), 544 deletions(-) delete mode 100644 packages/EasyApiPlatform/src/Bridge/Symfony/Listeners/OperationRequestInitiatorTrait.php create mode 100644 packages/EasyApiPlatform/tests/AbstractApiTestCase.php delete mode 100644 packages/EasyApiPlatform/tests/Bridge/Symfony/AbstractSymfonyTestCase.php delete mode 100644 packages/EasyApiPlatform/tests/Bridge/Symfony/Listeners/HttpKernelViewEventListenerTest.php delete mode 100644 packages/EasyApiPlatform/tests/Bridge/Symfony/Stubs/KernelStub.php delete mode 100644 packages/EasyApiPlatform/tests/Bridge/Symfony/Stubs/SerializerContextBuilderStub.php rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/Dummy.php (96%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/DummyFriend.php (91%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/EmbeddableDummy.php (96%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/FourthLevel.php (93%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/ParentDummy.php (85%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/RelatedDummy.php (93%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/RelatedOwnedDummy.php (93%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/RelatedOwningDummy.php (93%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/RelatedToDummyFriend.php (96%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/ApiResource/ThirdLevel.php (95%) rename packages/EasyApiPlatform/tests/Fixtures/app/src/{ => AdvancedSearchFilter}/Serializer/NameConverter/CustomConverter.php (88%) create mode 100644 packages/EasyApiPlatform/tests/Fixtures/app/src/CustomPaginator/ApiResource/Category.php create mode 100644 packages/EasyApiPlatform/tests/Fixtures/app/src/ReadListener/ApiResource/Question.php delete mode 100644 packages/EasyApiPlatform/tests/Serializers/SerializerContextBuilderTest.php diff --git a/.phpstorm.meta.php b/.phpstorm.meta.php index b2c9c4982..ad362a9a7 100644 --- a/.phpstorm.meta.php +++ b/.phpstorm.meta.php @@ -8,3 +8,9 @@ // $envelope->last(Stamp::class) -> instance of "Stamp" override(\Symfony\Component\Messenger\Envelope::last(0), type(0)); + +// self::getService(Type::class) → instance of "Type" +override( + \EonX\EasyTest\Traits\ContainerServiceTrait::getService(), + type(0), +); diff --git a/packages/EasyApiPlatform/composer.json b/packages/EasyApiPlatform/composer.json index ba4be0415..7dc528fe1 100644 --- a/packages/EasyApiPlatform/composer.json +++ b/packages/EasyApiPlatform/composer.json @@ -19,11 +19,14 @@ "doctrine/common": "^3.0", "doctrine/annotations": "^1.14", "doctrine/doctrine-bundle": "^2.8", + "eonx-com/easy-test": "^5.11", "mockery/mockery": "^1.6", "phpspec/prophecy": "^1.0", "phpspec/prophecy-phpunit": "^2.0", "phpunit/phpunit": "^10.2", - "symfony/symfony": "^6.4" + "symfony/browser-kit": "^6.4", + "symfony/framework-bundle": "^6.4", + "symfony/routing": "^6.4" }, "autoload": { "psr-4": { diff --git a/packages/EasyApiPlatform/src/Bridge/Symfony/Listeners/OperationRequestInitiatorTrait.php b/packages/EasyApiPlatform/src/Bridge/Symfony/Listeners/OperationRequestInitiatorTrait.php deleted file mode 100644 index 600646109..000000000 --- a/packages/EasyApiPlatform/src/Bridge/Symfony/Listeners/OperationRequestInitiatorTrait.php +++ /dev/null @@ -1,27 +0,0 @@ - ['accept' => ['application/json']], + ] + ); + } + + protected static function createKernel(array $options = []): KernelInterface + { + return new ApplicationKernel('test', false); + } + + protected function initDatabase(): void + { + $entityManager = self::getService(EntityManagerInterface::class); + $metaData = $entityManager->getMetadataFactory() +->getAllMetadata(); + $schemaTool = new SchemaTool($entityManager); + $schemaTool->dropSchema($metaData); + $schemaTool->updateSchema($metaData); + } +} diff --git a/packages/EasyApiPlatform/tests/AbstractTestCase.php b/packages/EasyApiPlatform/tests/AbstractTestCase.php index 4147ab3b4..65c27bc3d 100644 --- a/packages/EasyApiPlatform/tests/AbstractTestCase.php +++ b/packages/EasyApiPlatform/tests/AbstractTestCase.php @@ -3,41 +3,24 @@ namespace EonX\EasyApiPlatform\Tests; -use Mockery; -use Mockery\MockInterface; -use PHPUnit\Framework\TestCase; -use Symfony\Component\Filesystem\Filesystem; +use EonX\EasyApiPlatform\Tests\Fixtures\App\Kernel\ApplicationKernel; +use EonX\EasyTest\Traits\ContainerServiceTrait; +use EonX\EasyTest\Traits\PrivatePropertyAccessTrait; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\HttpKernel\KernelInterface; -/** - * This class has for objective to provide common features to all tests without having to update - * the class they all extend. - */ -abstract class AbstractTestCase extends TestCase +abstract class AbstractTestCase extends KernelTestCase { - protected function tearDown(): void - { - $fs = new Filesystem(); - $var = __DIR__ . '/../var'; - - if ($fs->exists($var)) { - $fs->remove($var); - } + use ContainerServiceTrait; + use PrivatePropertyAccessTrait; - parent::tearDown(); + protected function setUp(): void + { + self::bootKernel(); } - /** - * @param class-string|object $target - */ - protected static function mock(string|object $target, ?callable $expectations = null): MockInterface + protected static function createKernel(array $options = []): KernelInterface { - /** @var \Mockery\MockInterface $mock */ - $mock = Mockery::mock($target); - - if ($expectations !== null) { - $expectations($mock); - } - - return $mock; + return new ApplicationKernel('test', false); } } diff --git a/packages/EasyApiPlatform/tests/Bridge/Symfony/AbstractSymfonyTestCase.php b/packages/EasyApiPlatform/tests/Bridge/Symfony/AbstractSymfonyTestCase.php deleted file mode 100644 index 5868eb0ef..000000000 --- a/packages/EasyApiPlatform/tests/Bridge/Symfony/AbstractSymfonyTestCase.php +++ /dev/null @@ -1,25 +0,0 @@ -kernel !== null) { - return $this->kernel; - } - - $this->kernel = new KernelStub(); - $this->kernel->boot(); - - return $this->kernel; - } -} diff --git a/packages/EasyApiPlatform/tests/Bridge/Symfony/Listeners/HttpKernelViewEventListenerTest.php b/packages/EasyApiPlatform/tests/Bridge/Symfony/Listeners/HttpKernelViewEventListenerTest.php deleted file mode 100644 index 9692d012d..000000000 --- a/packages/EasyApiPlatform/tests/Bridge/Symfony/Listeners/HttpKernelViewEventListenerTest.php +++ /dev/null @@ -1,83 +0,0 @@ - [ - 'controllerResult' => new stdClass(), - 'isCustomPaginator' => false, - ]; - - yield 'The controller result is Paginator' => [ - 'controllerResult' => self::getApiPlatformPaginator(), - 'isCustomPaginator' => true, - ]; - } - - #[DataProvider('providerTestListener')] - public function testListener(object $controllerResult, bool $isCustomPaginator): void - { - $event = new ViewEvent( - $this->getKernel(), - new Request(), - HttpKernelInterface::MAIN_REQUEST, - $controllerResult - ); - $listener = new HttpKernelViewEventListener(); - - $listener($event); - - self::assertSame($isCustomPaginator, $event->getControllerResult() instanceof CustomPaginatorInterface); - } - - private static function getApiPlatformPaginator(): Paginator - { - /** @var \Doctrine\ORM\EntityManagerInterface $manager */ - $manager = self::mock(EntityManagerInterface::class, static function (MockInterface $mock): void { - $mock->shouldReceive('getConfiguration') - ->atLeast() - ->once() - ->withNoArgs() - ->andReturn(new Configuration()); - }); - - $query = new Query($manager); - $query->setFirstResult(1) - ->setMaxResults(15); - - /** @var \Doctrine\ORM\Tools\Pagination\Paginator $doctrinePaginator */ - $doctrinePaginator = self::mock( - DoctrinePaginator::class, - static function (MockInterface $mock) use ($query): void { - $mock->shouldReceive('getQuery') - ->once() - ->withNoArgs() - ->andReturn($query); - } - ); - - return new Paginator($doctrinePaginator); - } -} diff --git a/packages/EasyApiPlatform/tests/Bridge/Symfony/Listeners/ReadListenerTest.php b/packages/EasyApiPlatform/tests/Bridge/Symfony/Listeners/ReadListenerTest.php index 636721db6..5b804eb81 100644 --- a/packages/EasyApiPlatform/tests/Bridge/Symfony/Listeners/ReadListenerTest.php +++ b/packages/EasyApiPlatform/tests/Bridge/Symfony/Listeners/ReadListenerTest.php @@ -3,157 +3,47 @@ namespace EonX\EasyApiPlatform\Tests\Bridge\Symfony\Listeners; -use ApiPlatform\Metadata\Link; -use ApiPlatform\Metadata\NotExposed; -use ApiPlatform\Metadata\Post; use EonX\EasyApiPlatform\Bridge\Symfony\Listeners\ReadListener; -use EonX\EasyApiPlatform\Tests\Bridge\Symfony\AbstractSymfonyTestCase; -use EonX\EasyApiPlatform\Tests\Fixtures\App\ApiResource\Dummy; -use PHPUnit\Framework\Attributes\DataProvider; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Event\RequestEvent; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -use Symfony\Component\HttpKernel\HttpKernelInterface; +use EonX\EasyApiPlatform\Tests\AbstractApiTestCase; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; -final class ReadListenerTest extends AbstractSymfonyTestCase +final class ReadListenerTest extends AbstractApiTestCase { - /** - * @see testItThrowsException - */ - public static function provideRequestsForCasesWithThrownException(): iterable + public function testItSucceeds(): void { - yield 'a request with operation with URI variables' => [ - 'request' => new Request( - attributes: [ - '_api_operation' => new Post(uriVariables: ['id' => new Link()]), - '_api_operation_name' => 'post_dummy', - '_api_resource_class' => Dummy::class, - 'receive' => true, + $this->initDatabase(); + + $response = self::$client->request( + 'POST', + '/questions/1/mark-as-answered', + [ + 'headers' => [ + 'content-type' => 'application/json', ], - server: [ - 'REQUEST_METHOD' => 'POST', - ] - ), - ]; + ] + ); - yield 'a request with method safe equals to true' => [ - 'request' => new Request( - attributes: [ - '_api_operation' => new Post(uriVariables: []), - '_api_operation_name' => 'post_dummy', - '_api_resource_class' => Dummy::class, - 'receive' => true, - ], - server: [ - 'REQUEST_METHOD' => 'GET', - ] - ), - ]; + self::assertSame(404, $response->getStatusCode()); } - /** - * @see testItSucceeds - */ - public static function provideRequestsForSuccessfulCases(): iterable + public function testItSucceedsWithoutReadListener(): void { - yield 'a request without attributes' => [ - 'request' => new Request( - server: [ - 'REQUEST_METHOD' => 'POST', - ] - ), - ]; - - yield 'a request without "receive" attribute' => [ - 'request' => new Request( - attributes: [ - '_api_operation' => new Post(uriVariables: []), - '_api_operation_name' => 'post_dummy', - '_api_resource_class' => Dummy::class, - ], - server: [ - 'REQUEST_METHOD' => 'POST', - ] - ), - ]; - - yield 'a request with "receive" attribute equals to "false"' => [ - 'request' => new Request( - attributes: [ - '_api_operation' => new Post(uriVariables: []), - '_api_operation_name' => 'post_dummy', - '_api_resource_class' => Dummy::class, - 'receive' => false, - ], - server: [ - 'REQUEST_METHOD' => 'POST', - ] - ), - ]; - - yield 'a request without operation' => [ - 'request' => new Request( - attributes: [ - '_api_operation' => null, - '_api_operation_name' => 'post_dummy', - '_api_resource_class' => Dummy::class, - 'receive' => true, - ], - server: [ - 'REQUEST_METHOD' => 'POST', - ] - ), - ]; - - yield 'a request with operation that cannot read' => [ - 'request' => new Request( - attributes: [ - '_api_operation' => new NotExposed(uriVariables: []), - '_api_operation_name' => 'post_dummy', - '_api_resource_class' => Dummy::class, - 'receive' => true, - ], - server: [ - 'REQUEST_METHOD' => 'POST', - ] - ), - ]; - - yield 'a request with method safe equals to false and with operation without URI variables' => [ - 'request' => new Request( - attributes: [ - '_api_operation' => new Post(uriVariables: []), - '_api_operation_name' => 'post_dummy', - '_api_resource_class' => Dummy::class, - 'receive' => true, + $this->initDatabase(); + self::getService(EventDispatcherInterface::class)->removeListener( + 'kernel.request', + [self::getService(ReadListener::class), '__invoke'] + ); + + $response = self::$client->request( + 'POST', + '/questions/1/mark-as-answered', + [ + 'headers' => [ + 'content-type' => 'application/json', ], - server: [ - 'REQUEST_METHOD' => 'POST', - ] - ), - ]; - } - - #[DataProvider('provideRequestsForSuccessfulCases')] - public function testItSucceeds(Request $request): void - { - $event = new RequestEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST); - $sut = new ReadListener(); - - $sut($event); - - self::assertTrue(true); - } - - #[DataProvider('provideRequestsForCasesWithThrownException')] - public function testItThrowsException(Request $request): void - { - $event = new RequestEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST); - $sut = new ReadListener(); - - $this->expectException(NotFoundHttpException::class); - $this->expectExceptionMessage('Not Found'); + ] + ); - $sut($event); + self::assertSame(200, $response->getStatusCode()); } } diff --git a/packages/EasyApiPlatform/tests/Bridge/Symfony/Stubs/KernelStub.php b/packages/EasyApiPlatform/tests/Bridge/Symfony/Stubs/KernelStub.php deleted file mode 100644 index 409e86f93..000000000 --- a/packages/EasyApiPlatform/tests/Bridge/Symfony/Stubs/KernelStub.php +++ /dev/null @@ -1,46 +0,0 @@ -configs = $configs ?? []; - - parent::__construct('test', true); - } - - public function process(ContainerBuilder $container): void - { - $container->setDefinition( - 'api_platform.serializer.context_builder', - new Definition(SerializerContextBuilderStub::class) - ); - } - - /** - * @return iterable<\Symfony\Component\HttpKernel\Bundle\BundleInterface> - */ - public function registerBundles(): iterable - { - yield new EasyApiPlatformSymfonyBundle(); - } - - public function registerContainerConfiguration(LoaderInterface $loader): void - { - foreach ($this->configs as $config) { - $loader->load($config); - } - } -} diff --git a/packages/EasyApiPlatform/tests/Bridge/Symfony/Stubs/SerializerContextBuilderStub.php b/packages/EasyApiPlatform/tests/Bridge/Symfony/Stubs/SerializerContextBuilderStub.php deleted file mode 100644 index 5e72173b2..000000000 --- a/packages/EasyApiPlatform/tests/Bridge/Symfony/Stubs/SerializerContextBuilderStub.php +++ /dev/null @@ -1,15 +0,0 @@ -getContainer()->get('doctrine'); @@ -113,9 +111,4 @@ public function testApply( ); } } - - protected static function createKernel(array $options = []): KernelInterface - { - return new ApplicationKernel('test', false); - } } diff --git a/packages/EasyApiPlatform/tests/Filters/AdvancedSearchFilterTest.php b/packages/EasyApiPlatform/tests/Filters/AdvancedSearchFilterTest.php index 1c5296933..911881f8c 100644 --- a/packages/EasyApiPlatform/tests/Filters/AdvancedSearchFilterTest.php +++ b/packages/EasyApiPlatform/tests/Filters/AdvancedSearchFilterTest.php @@ -9,9 +9,9 @@ use ApiPlatform\Metadata\Get; use Doctrine\Persistence\ManagerRegistry; use EonX\EasyApiPlatform\Filters\AdvancedSearchFilter; -use EonX\EasyApiPlatform\Tests\Fixtures\App\ApiResource\Dummy; -use EonX\EasyApiPlatform\Tests\Fixtures\App\ApiResource\RelatedDummy; -use EonX\EasyApiPlatform\Tests\Fixtures\App\Serializer\NameConverter\CustomConverter; +use EonX\EasyApiPlatform\Tests\Fixtures\App\AdvancedSearchFilter\ApiResource\Dummy; +use EonX\EasyApiPlatform\Tests\Fixtures\App\AdvancedSearchFilter\ApiResource\RelatedDummy; +use EonX\EasyApiPlatform\Tests\Fixtures\App\AdvancedSearchFilter\Serializer\NameConverter\CustomConverter; use Prophecy\Argument; use Prophecy\Prophet; diff --git a/packages/EasyApiPlatform/tests/Fixtures/app/config/packages/api_platform.php b/packages/EasyApiPlatform/tests/Fixtures/app/config/packages/api_platform.php index 4d76fbf55..d5d4870df 100644 --- a/packages/EasyApiPlatform/tests/Fixtures/app/config/packages/api_platform.php +++ b/packages/EasyApiPlatform/tests/Fixtures/app/config/packages/api_platform.php @@ -42,7 +42,9 @@ $apiPlatformConfig->mapping() ->paths([ - param('kernel.project_dir') . '/src/ApiResource/', + param('kernel.project_dir') . '/src/AdvancedSearchFilter/ApiResource/', + param('kernel.project_dir') . '/src/CustomPaginator/ApiResource/', + param('kernel.project_dir') . '/src/ReadListener/ApiResource/', ]); $apiPlatformConfig->swagger() diff --git a/packages/EasyApiPlatform/tests/Fixtures/app/config/packages/doctrine.php b/packages/EasyApiPlatform/tests/Fixtures/app/config/packages/doctrine.php index 00adbe8c8..133753add 100644 --- a/packages/EasyApiPlatform/tests/Fixtures/app/config/packages/doctrine.php +++ b/packages/EasyApiPlatform/tests/Fixtures/app/config/packages/doctrine.php @@ -19,10 +19,21 @@ $entityManager = $doctrineConfig->orm() ->entityManager('default'); - $entityManager->mapping('AppApiResource') - ->alias('AppApiResource') - ->dir(param('kernel.project_dir') . '/src/ApiResource') + $entityManager->mapping('AppCaseAdvancedSearchFilterApiResource') + ->dir(param('kernel.project_dir') . '/src/AdvancedSearchFilter/ApiResource') ->isBundle(false) - ->prefix('EonX\EasyApiPlatform\Tests\Fixtures\App\ApiResource') + ->prefix('EonX\EasyApiPlatform\Tests\Fixtures\App\AdvancedSearchFilter\ApiResource') + ->type('attribute'); + + $entityManager->mapping('AppCaseCustomPaginatorApiResource') + ->dir(param('kernel.project_dir') . '/src/CustomPaginator/ApiResource') + ->isBundle(false) + ->prefix('EonX\EasyApiPlatform\Tests\Fixtures\App\CustomPaginator\ApiResource') + ->type('attribute'); + + $entityManager->mapping('AppCaseReadListenerApiResource') + ->dir(param('kernel.project_dir') . '/src/ReadListener/ApiResource') + ->isBundle(false) + ->prefix('EonX\EasyApiPlatform\Tests\Fixtures\App\ReadListener\ApiResource') ->type('attribute'); }; diff --git a/packages/EasyApiPlatform/tests/Fixtures/app/src/ApiResource/Dummy.php b/packages/EasyApiPlatform/tests/Fixtures/app/src/AdvancedSearchFilter/ApiResource/Dummy.php similarity index 96% rename from packages/EasyApiPlatform/tests/Fixtures/app/src/ApiResource/Dummy.php rename to packages/EasyApiPlatform/tests/Fixtures/app/src/AdvancedSearchFilter/ApiResource/Dummy.php index 6e286856d..c060b5ad4 100644 --- a/packages/EasyApiPlatform/tests/Fixtures/app/src/ApiResource/Dummy.php +++ b/packages/EasyApiPlatform/tests/Fixtures/app/src/AdvancedSearchFilter/ApiResource/Dummy.php @@ -1,7 +1,7 @@ Several dummies + * @var \Doctrine\Common\Collections\Collection Several dummies */ #[ORM\ManyToMany(targetEntity: RelatedDummy::class)] private Collection $relatedDummies; @@ -169,7 +169,7 @@ public function getNameConverted(): ?string } /** - * @return \Doctrine\Common\Collections\Collection + * @return \Doctrine\Common\Collections\Collection */ public function getRelatedDummies(): Collection { diff --git a/packages/EasyApiPlatform/tests/Fixtures/app/src/ApiResource/DummyFriend.php b/packages/EasyApiPlatform/tests/Fixtures/app/src/AdvancedSearchFilter/ApiResource/DummyFriend.php similarity index 91% rename from packages/EasyApiPlatform/tests/Fixtures/app/src/ApiResource/DummyFriend.php rename to packages/EasyApiPlatform/tests/Fixtures/app/src/AdvancedSearchFilter/ApiResource/DummyFriend.php index 28bbd237b..104306c14 100644 --- a/packages/EasyApiPlatform/tests/Fixtures/app/src/ApiResource/DummyFriend.php +++ b/packages/EasyApiPlatform/tests/Fixtures/app/src/AdvancedSearchFilter/ApiResource/DummyFriend.php @@ -1,7 +1,7 @@ + * @var \Doctrine\Common\Collections\Collection */ #[Groups(['fakemanytomany', 'friends'])] #[ORM\OneToMany(mappedBy: 'relatedDummy', targetEntity: RelatedToDummyFriend::class, cascade: ['persist'])] @@ -95,7 +95,7 @@ public function getName(): ?string } /** - * @return \Doctrine\Common\Collections\Collection + * @return \Doctrine\Common\Collections\Collection */ public function getRelatedToDummyFriend(): Collection { diff --git a/packages/EasyApiPlatform/tests/Fixtures/app/src/ApiResource/RelatedOwnedDummy.php b/packages/EasyApiPlatform/tests/Fixtures/app/src/AdvancedSearchFilter/ApiResource/RelatedOwnedDummy.php similarity index 93% rename from packages/EasyApiPlatform/tests/Fixtures/app/src/ApiResource/RelatedOwnedDummy.php rename to packages/EasyApiPlatform/tests/Fixtures/app/src/AdvancedSearchFilter/ApiResource/RelatedOwnedDummy.php index cedbe34e1..d576f0449 100644 --- a/packages/EasyApiPlatform/tests/Fixtures/app/src/ApiResource/RelatedOwnedDummy.php +++ b/packages/EasyApiPlatform/tests/Fixtures/app/src/AdvancedSearchFilter/ApiResource/RelatedOwnedDummy.php @@ -1,7 +1,7 @@ id; + } + + public function getTitle(): string + { + return $this->title; + } + + public function setId(int $id): self + { + $this->id = $id; + + return $this; + } + + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } +} diff --git a/packages/EasyApiPlatform/tests/Fixtures/app/src/ReadListener/ApiResource/Question.php b/packages/EasyApiPlatform/tests/Fixtures/app/src/ReadListener/ApiResource/Question.php new file mode 100644 index 000000000..a5f2b0957 --- /dev/null +++ b/packages/EasyApiPlatform/tests/Fixtures/app/src/ReadListener/ApiResource/Question.php @@ -0,0 +1,54 @@ +id; + } + + public function getQuestion(): string + { + return $this->question; + } + + public function setId(int $id): self + { + $this->id = $id; + + return $this; + } + + public function setQuestion(string $question): self + { + $this->question = $question; + + return $this; + } +} diff --git a/packages/EasyApiPlatform/tests/Paginators/CustomPaginatorTest.php b/packages/EasyApiPlatform/tests/Paginators/CustomPaginatorTest.php index 9a59e7897..3b814bcb2 100644 --- a/packages/EasyApiPlatform/tests/Paginators/CustomPaginatorTest.php +++ b/packages/EasyApiPlatform/tests/Paginators/CustomPaginatorTest.php @@ -3,72 +3,32 @@ namespace EonX\EasyApiPlatform\Tests\Paginators; -use ApiPlatform\Doctrine\Orm\Paginator; -use ArrayIterator; -use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Query; -use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; -use EonX\EasyApiPlatform\Paginators\CustomPaginator; -use EonX\EasyApiPlatform\Tests\AbstractTestCase; -use Mockery\MockInterface; +use EonX\EasyApiPlatform\Tests\AbstractApiTestCase; +use EonX\EasyApiPlatform\Tests\Fixtures\App\CustomPaginator\ApiResource\Category; -final class CustomPaginatorTest extends AbstractTestCase +final class CustomPaginatorTest extends AbstractApiTestCase { public function testCustomPaginator(): void { - $paginator = new CustomPaginator($this->getApiPlatformPaginator()); - - $expectedPagination = [ - 'currentPage' => 1, - 'hasNextPage' => false, - 'hasPreviousPage' => false, - 'itemsPerPage' => 15, - 'totalItems' => 0, - 'totalPages' => 1, - ]; - - self::assertEmpty($paginator->getItems()); - self::assertEquals($expectedPagination, $paginator->getPagination()); - } - - /** - * @return \ApiPlatform\Doctrine\Orm\Paginator - */ - private function getApiPlatformPaginator(): Paginator - { - /** @var \Doctrine\ORM\EntityManagerInterface $manager */ - $manager = self::mock(EntityManagerInterface::class, static function (MockInterface $mock): void { - $mock->shouldReceive('getConfiguration') - ->atLeast() - ->once() - ->withNoArgs() - ->andReturn(new Configuration()); - }); - - $query = new Query($manager); - $query->setFirstResult(1) - ->setMaxResults(15); - - /** @var \Doctrine\ORM\Tools\Pagination\Paginator $doctrinePaginator */ - $doctrinePaginator = self::mock( - DoctrinePaginator::class, - static function (MockInterface $mock) use ($query): void { - $mock->shouldReceive('getQuery') - ->once() - ->withNoArgs() - ->andReturn($query); - $mock->shouldReceive('getIterator') - ->once() - ->withNoArgs() - ->andReturn(new ArrayIterator()); - $mock->shouldReceive('count') - ->once() - ->withNoArgs() - ->andReturn(0); - } + $this->initDatabase(); + $entityManager = self::getService(EntityManagerInterface::class); + $entityManager->persist((new Category())->setTitle('Some category')); + $entityManager->flush(); + + $response = self::$client->request('GET', '/categories'); + + $responseData = $response->toArray(false); + self::assertSame( + [ + 'currentPage' => 1, + 'hasNextPage' => false, + 'hasPreviousPage' => false, + 'itemsPerPage' => 25, + 'totalItems' => 1, + 'totalPages' => 1, + ], + $responseData['pagination'] ); - - return new Paginator($doctrinePaginator); } } diff --git a/packages/EasyApiPlatform/tests/Serializers/SerializerContextBuilderTest.php b/packages/EasyApiPlatform/tests/Serializers/SerializerContextBuilderTest.php deleted file mode 100644 index 94425449a..000000000 --- a/packages/EasyApiPlatform/tests/Serializers/SerializerContextBuilderTest.php +++ /dev/null @@ -1,72 +0,0 @@ - [ - 'contextFromDecorated' => [ - 'operation' => new Get(), - ], - 'isGroupAdded' => false, - ]; - - yield 'Group is added' => [ - 'contextFromDecorated' => [ - 'operation' => new GetCollection(), - ], - 'isGroupAdded' => true, - ]; - } - - #[DataProvider('providerTestCreateFromRequest')] - public function testCreateFromRequest(array $contextFromDecorated, bool $isGroupAdded): void - { - $request = new Request(); - $contextBuilder = new SerializerContextBuilder( - $this->mockDecoratedSerializerContextBuilder($contextFromDecorated) - ); - - $context = $contextBuilder->createFromRequest($request, true); - - /** @var string[] $groups */ - $groups = $context['groups'] ?? []; - self::assertSame( - $isGroupAdded, - \in_array(CustomPaginatorInterface::SERIALIZER_GROUP, $groups, true) - ); - } - - private function mockDecoratedSerializerContextBuilder( - array $contextFromDecorated, - ): SerializerContextBuilderInterface { - /** @var \ApiPlatform\Serializer\SerializerContextBuilderInterface $decorated */ - $decorated = self::mock( - SerializerContextBuilderInterface::class, - static function (MockInterface $mock) use ($contextFromDecorated): void { - $mock - ->shouldReceive('createFromRequest') - ->once() - ->andReturn($contextFromDecorated); - } - ); - - return $decorated; - } -} diff --git a/quality/phpstan.neon b/quality/phpstan.neon index 36d2ea872..cad146dca 100644 --- a/quality/phpstan.neon +++ b/quality/phpstan.neon @@ -48,15 +48,9 @@ parameters: - message: '#Call to an undefined method Prophecy\\Prophecy\\ObjectProphecy\:\:getResourceFromIri\(\)#' path: %currentWorkingDirectory%/packages/EasyApiPlatform/tests/Filters/AdvancedSearchFilterTest.php - - message: '#Call to an undefined method Prophecy\\Prophecy\\ObjectProphecy\:\:getId\(\)#' + - message: '#Call to an undefined method Prophecy\\Prophecy\\ObjectProphecy\:\:getId\(\)#' path: %currentWorkingDirectory%/packages/EasyApiPlatform/tests/Filters/AdvancedSearchFilterTest.php - - message: '#Access to an undefined property EonX\\EasyApiPlatform\\Bridge\\Symfony\\Listeners\\ReadListener\:\:\$resourceMetadataCollectionFactory#' - path: %currentWorkingDirectory%/packages/EasyApiPlatform/src/Bridge/Symfony/Listeners/ReadListener.php - - - message: '#Call to an undefined method EonX\\EasyApiPlatform\\Bridge\\Symfony\\Listeners\\ReadListener\:\:initializeOperation\(\)#' - path: %currentWorkingDirectory%/packages/EasyApiPlatform/src/Bridge/Symfony/Listeners/ReadListener.php - # ---- EasyApiToken ---- - message: '#Parameter \#1 \$payload of static method Firebase\\JWT\\JWT\:\:encode\(\) expects array, array\|object given#' path: %currentWorkingDirectory%/packages/EasyApiToken/src/External/FirebaseJwtDriver.php