Skip to content

Commit

Permalink
test: Test EnvelopeCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Mar 28, 2024
1 parent 2de4564 commit 2f6b3d5
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.phpunit.cache
/clover.xml
/coveralls-upload.json
/tests/Integration/TestApplication/var/
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
->exclude('Integration/TestApplication/var')
;

$config = new PhpCsFixer\Config();
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -20,6 +22,7 @@
},
"autoload-dev": {
"psr-4": {
"Tienvx\\Bundle\\PactMessengerBundle\\Tests\\Integration\\TestApplication\\": "tests/Integration/TestApplication/src/",
"Tienvx\\Bundle\\PactMessengerBundle\\Tests\\": "tests/"
}
},
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ parameters:
paths:
- src
- tests
excludePaths:
- 'tests/Integration/TestApplication/var'
6 changes: 6 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache">
<php>
<ini name="display_errors" value="1"/>
<ini name="error_reporting" value="-1"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<server name="KERNEL_CLASS" value="Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Kernel"/>
</php>
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
Expand Down
44 changes: 44 additions & 0 deletions tests/Integration/Service/EnvelopeCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Tests\Integration\Service;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Tienvx\Bundle\PactMessengerBundle\Service\EnvelopeCollectorInterface;
use Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Message\UserCreated;
use Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Message\UserDeleted;
use Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Message\UserUpdated;

class EnvelopeCollectorTest extends KernelTestCase
{
private int $id = 123;

public function testEnvelopCollector(): void
{
self::bootKernel();

$container = static::getContainer();

$bus = $container->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);
}
}
29 changes: 29 additions & 0 deletions tests/Integration/TestApplication/config/packages/framework.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Kernel;

$configuration = [
'http_method_override' => 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);
14 changes: 14 additions & 0 deletions tests/Integration/TestApplication/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $container) {
$services = $container->services()
->defaults()
->autowire()
->autoconfigure()
;

$services->load('Tienvx\\Bundle\\PactMessengerBundle\\Tests\\Integration\\TestApplication\\', '../src/*')
->exclude('../{Entity,Tests,Kernel.php}');
};
41 changes: 41 additions & 0 deletions tests/Integration/TestApplication/src/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication;

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as SymfonyKernel;
use Tienvx\Bundle\PactMessengerBundle\TienvxPactMessengerBundle;

final class Kernel extends SymfonyKernel
{
use MicroKernelTrait;

public function __construct()
{
parent::__construct('test', true);
}

public function registerBundles(): iterable
{
return [
new FrameworkBundle(),
new TienvxPactMessengerBundle(),
];
}

public function getProjectDir(): string
{
return \dirname(__DIR__);
}

protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void
{
$loader->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');
}
}
10 changes: 10 additions & 0 deletions tests/Integration/TestApplication/src/Message/UserCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Message;

class UserCreated
{
public function __construct(public readonly int $userId)
{
}
}
10 changes: 10 additions & 0 deletions tests/Integration/TestApplication/src/Message/UserDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Message;

class UserDeleted
{
public function __construct(public readonly int $userId)
{
}
}
10 changes: 10 additions & 0 deletions tests/Integration/TestApplication/src/Message/UserUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tienvx\Bundle\PactMessengerBundle\Tests\Integration\TestApplication\Message;

class UserUpdated
{
public function __construct(public readonly int $userId)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Tests\DependencyInjection;
namespace Tienvx\Bundle\MbtBundle\Tests\Unit\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down

0 comments on commit 2f6b3d5

Please sign in to comment.