From 5e9114e2da7920b1edf5a22266db95b18e0efebe Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Thu, 28 Mar 2024 21:12:36 +0700 Subject: [PATCH] test: Test UserController --- .gitignore | 2 +- .php-cs-fixer.php | 2 +- composer.json | 2 +- phpstan.neon | 2 +- phpunit.xml.dist | 2 +- .../Controller/UserControllerTest.php | 61 +++++++++++++++++++ .../Service/EnvelopeCollectorTest.php | 8 +-- .../config/packages/framework.php | 8 +-- tests/TestApplication/config/routes.php | 8 +++ .../TestApplication/config/services.php | 5 +- .../src/Controller/UserController.php | 38 ++++++++++++ .../TestApplication/src/Kernel.php | 2 +- .../src/Message/UserCreated.php | 2 +- .../src/Message/UserDeleted.php | 2 +- .../src/Message/UserUpdated.php | 2 +- 15 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 tests/Application/Controller/UserControllerTest.php rename tests/{Integration => }/TestApplication/config/packages/framework.php (53%) create mode 100644 tests/TestApplication/config/routes.php rename tests/{Integration => }/TestApplication/config/services.php (65%) create mode 100644 tests/TestApplication/src/Controller/UserController.php rename tests/{Integration => }/TestApplication/src/Kernel.php (94%) rename tests/{Integration => }/TestApplication/src/Message/UserCreated.php (54%) rename tests/{Integration => }/TestApplication/src/Message/UserDeleted.php (54%) rename tests/{Integration => }/TestApplication/src/Message/UserUpdated.php (54%) diff --git a/.gitignore b/.gitignore index 0887d25..f8d0702 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,4 @@ /.phpunit.cache /clover.xml /coveralls-upload.json -/tests/Integration/TestApplication/var/ +/tests/TestApplication/var/ diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 64c2119..46220be 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -3,7 +3,7 @@ $finder = PhpCsFixer\Finder::create() ->in(__DIR__.'/src') ->in(__DIR__.'/tests') - ->exclude('Integration/TestApplication/var') + ->exclude('TestApplication/var') ; $config = new PhpCsFixer\Config(); diff --git a/composer.json b/composer.json index 7eed89b..b39b03f 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ }, "autoload-dev": { "psr-4": { - "Tienvx\\Bundle\\PactMessengerBundle\\Tests\\Integration\\TestApplication\\": "tests/Integration/TestApplication/src/", + "Tienvx\\Bundle\\PactMessengerBundle\\Tests\\TestApplication\\": "tests/TestApplication/src/", "Tienvx\\Bundle\\PactMessengerBundle\\Tests\\": "tests/" } }, diff --git a/phpstan.neon b/phpstan.neon index 684ce9c..b0ef41a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,4 +4,4 @@ parameters: - src - tests excludePaths: - - 'tests/Integration/TestApplication/var' + - 'tests/TestApplication/var' diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e5509b6..289f26e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,7 +5,7 @@ - + diff --git a/tests/Application/Controller/UserControllerTest.php b/tests/Application/Controller/UserControllerTest.php new file mode 100644 index 0000000..333e10c --- /dev/null +++ b/tests/Application/Controller/UserControllerTest.php @@ -0,0 +1,61 @@ +request('POST', '/create'); + $this->assertResponseIsSuccessful(); + + $container = static::getContainer(); + $collector = $container->get(EnvelopeCollectorInterface::class); + $this->assertCount(1, $all = $collector->getAll()); + + $this->assertInstanceOf(Envelope::class, $created = $collector->getSingle(UserCreated::class)); + $this->assertTrue(in_array($created, $all)); + $this->assertInstanceOf(UserCreated::class, $message = $created->getMessage()); + $this->assertSame(123, $message->userId); + } + + public function testUpdateUser(): void + { + $client = static::createClient(); + $client->request('PUT', '/update/123'); + $this->assertResponseIsSuccessful(); + + $container = static::getContainer(); + $collector = $container->get(EnvelopeCollectorInterface::class); + $this->assertCount(1, $all = $collector->getAll()); + + $this->assertInstanceOf(Envelope::class, $created = $collector->getSingle(UserUpdated::class)); + $this->assertTrue(in_array($created, $all)); + $this->assertInstanceOf(UserUpdated::class, $message = $created->getMessage()); + $this->assertSame(123, $message->userId); + } + + public function testDeleteUser(): void + { + $client = static::createClient(); + $client->request('DELETE', '/delete/123'); + $this->assertResponseIsSuccessful(); + + $container = static::getContainer(); + $collector = $container->get(EnvelopeCollectorInterface::class); + $this->assertCount(1, $all = $collector->getAll()); + + $this->assertInstanceOf(Envelope::class, $created = $collector->getSingle(UserDeleted::class)); + $this->assertTrue(in_array($created, $all)); + $this->assertInstanceOf(UserDeleted::class, $message = $created->getMessage()); + $this->assertSame(123, $message->userId); + } +} diff --git a/tests/Integration/Service/EnvelopeCollectorTest.php b/tests/Integration/Service/EnvelopeCollectorTest.php index bc2d2cf..9467bde 100644 --- a/tests/Integration/Service/EnvelopeCollectorTest.php +++ b/tests/Integration/Service/EnvelopeCollectorTest.php @@ -1,14 +1,14 @@ false, @@ -15,9 +15,9 @@ 'audit' => 'in-memory://', ], 'routing' => [ - 'Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Message\UserCreated' => 'async', - 'Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Message\UserUpdated' => 'async', - 'Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Message\UserDeleted' => ['async', 'audit'], + 'Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserCreated' => 'async', + 'Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserUpdated' => 'async', + 'Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserDeleted' => ['async', 'audit'], ], ], ]; diff --git a/tests/TestApplication/config/routes.php b/tests/TestApplication/config/routes.php new file mode 100644 index 0000000..08eff1a --- /dev/null +++ b/tests/TestApplication/config/routes.php @@ -0,0 +1,8 @@ +import('../src/Controller/', Kernel::MAJOR_VERSION >= 6 ? 'attribute' : 'annotation'); +}; diff --git a/tests/Integration/TestApplication/config/services.php b/tests/TestApplication/config/services.php similarity index 65% rename from tests/Integration/TestApplication/config/services.php rename to tests/TestApplication/config/services.php index dcd08c6..203ffde 100644 --- a/tests/Integration/TestApplication/config/services.php +++ b/tests/TestApplication/config/services.php @@ -9,6 +9,9 @@ ->autoconfigure() ; - $services->load('Tienvx\\Bundle\\PactMessengerBundle\\Tests\\Integration\\TestApplication\\', '../src/*') + $services->load('Tienvx\\Bundle\\PactMessengerBundle\\Tests\\TestApplication\\', '../src/*') ->exclude('../{Entity,Tests,Kernel.php}'); + + $services->load('Tienvx\\Bundle\\PactMessengerBundle\\Tests\\TestApplication\\Controller\\', '../src/Controller/') + ->tag('controller.service_arguments'); }; diff --git a/tests/TestApplication/src/Controller/UserController.php b/tests/TestApplication/src/Controller/UserController.php new file mode 100644 index 0000000..c902149 --- /dev/null +++ b/tests/TestApplication/src/Controller/UserController.php @@ -0,0 +1,38 @@ +dispatch(new UserCreated(123)); + + return new Response('created'); + } + + #[Route('/update/{id}', name: 'update_user', methods: Request::METHOD_PUT)] + public function update(int $id, MessageBusInterface $bus): Response + { + $bus->dispatch(new UserUpdated($id)); + + return new Response('updated'); + } + + #[Route('/delete/{id}', name: 'delete_user', methods: Request::METHOD_DELETE)] + public function delete(int $id, MessageBusInterface $bus): Response + { + $bus->dispatch(new UserDeleted($id)); + + return new Response('deleted'); + } +} diff --git a/tests/Integration/TestApplication/src/Kernel.php b/tests/TestApplication/src/Kernel.php similarity index 94% rename from tests/Integration/TestApplication/src/Kernel.php rename to tests/TestApplication/src/Kernel.php index 8703948..705d840 100644 --- a/tests/Integration/TestApplication/src/Kernel.php +++ b/tests/TestApplication/src/Kernel.php @@ -1,6 +1,6 @@