diff --git a/.gitignore b/.gitignore index c719a34..0887d25 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /.phpunit.cache /clover.xml /coveralls-upload.json +/tests/Integration/TestApplication/var/ diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 78082c3..64c2119 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -3,6 +3,7 @@ $finder = PhpCsFixer\Finder::create() ->in(__DIR__.'/src') ->in(__DIR__.'/tests') + ->exclude('Integration/TestApplication/var') ; $config = new PhpCsFixer\Config(); diff --git a/composer.json b/composer.json index 9731f8e..7eed89b 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,9 @@ "symfony/messenger": "^5.4|^6.4|^7.0" }, "require-dev": { - "phpunit/phpunit": "^10.1" + "phpunit/phpunit": "^10.1", + "symfony/framework-bundle": "^5.4|^6.4|7.0", + "symfony/test-pack": "^1.0" }, "license": "MIT", "autoload": { @@ -20,6 +22,7 @@ }, "autoload-dev": { "psr-4": { + "Tienvx\\Bundle\\PactMessengerBundle\\Tests\\Integration\\TestApplication\\": "tests/Integration/TestApplication/src/", "Tienvx\\Bundle\\PactMessengerBundle\\Tests\\": "tests/" } }, diff --git a/phpstan.neon b/phpstan.neon index 47e3df0..684ce9c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,3 +3,5 @@ parameters: paths: - src - tests + excludePaths: + - 'tests/Integration/TestApplication/var' diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 764d561..e5509b6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,12 @@ + + + + + + tests diff --git a/tests/Integration/Service/EnvelopeCollectorTest.php b/tests/Integration/Service/EnvelopeCollectorTest.php new file mode 100644 index 0000000..bc2d2cf --- /dev/null +++ b/tests/Integration/Service/EnvelopeCollectorTest.php @@ -0,0 +1,44 @@ +get(MessageBusInterface::class); + $bus->dispatch(new UserCreated($this->id)); + $bus->dispatch(new UserUpdated($this->id)); + $bus->dispatch(new UserDeleted($this->id)); + + $collector = $container->get(EnvelopeCollectorInterface::class); + $this->assertCount(3, $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($this->id, $message->userId); + $this->assertInstanceOf(Envelope::class, $updated = $collector->getSingle(UserUpdated::class)); + $this->assertTrue(in_array($updated, $all)); + $this->assertInstanceOf(UserUpdated::class, $message = $updated->getMessage()); + $this->assertSame($this->id, $message->userId); + $this->assertInstanceOf(Envelope::class, $deleted = $collector->getSingle(UserDeleted::class)); + $this->assertTrue(in_array($deleted, $all)); + $this->assertInstanceOf(UserDeleted::class, $message = $deleted->getMessage()); + $this->assertSame($this->id, $message->userId); + } +} diff --git a/tests/Integration/TestApplication/config/packages/framework.php b/tests/Integration/TestApplication/config/packages/framework.php new file mode 100644 index 0000000..e217637 --- /dev/null +++ b/tests/Integration/TestApplication/config/packages/framework.php @@ -0,0 +1,29 @@ + false, + 'handle_all_throwables' => true, + 'php_errors' => [ + 'log' => true, + ], + 'test' => true, + 'messenger' => [ + 'transports' => [ + 'async' => 'in-memory://', + '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'], + ], + ], +]; + +if (Kernel::MAJOR_VERSION <= 5) { + unset($configuration['handle_all_throwables']); +} + +$container->loadFromExtension('framework', $configuration); diff --git a/tests/Integration/TestApplication/config/services.php b/tests/Integration/TestApplication/config/services.php new file mode 100644 index 0000000..dcd08c6 --- /dev/null +++ b/tests/Integration/TestApplication/config/services.php @@ -0,0 +1,14 @@ +services() + ->defaults() + ->autowire() + ->autoconfigure() + ; + + $services->load('Tienvx\\Bundle\\PactMessengerBundle\\Tests\\Integration\\TestApplication\\', '../src/*') + ->exclude('../{Entity,Tests,Kernel.php}'); +}; diff --git a/tests/Integration/TestApplication/src/Kernel.php b/tests/Integration/TestApplication/src/Kernel.php new file mode 100644 index 0000000..8703948 --- /dev/null +++ b/tests/Integration/TestApplication/src/Kernel.php @@ -0,0 +1,41 @@ +load($this->getProjectDir().'/config/{packages}/*.php', 'glob'); + $loader->load($this->getProjectDir().'/config/{packages}/'.$this->environment.'/*.php', 'glob'); + $loader->load($this->getProjectDir().'/config/{services}.php', 'glob'); + $loader->load($this->getProjectDir().'/config/{services}_'.$this->environment.'.php', 'glob'); + } +} diff --git a/tests/Integration/TestApplication/src/Message/UserCreated.php b/tests/Integration/TestApplication/src/Message/UserCreated.php new file mode 100644 index 0000000..8693d3c --- /dev/null +++ b/tests/Integration/TestApplication/src/Message/UserCreated.php @@ -0,0 +1,10 @@ +