Skip to content

Commit

Permalink
test: Test UserController
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Mar 28, 2024
1 parent 0dcc248 commit 5e9114e
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
/.phpunit.cache
/clover.xml
/coveralls-upload.json
/tests/Integration/TestApplication/var/
/tests/TestApplication/var/
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
}
},
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ parameters:
- src
- tests
excludePaths:
- 'tests/Integration/TestApplication/var'
- 'tests/TestApplication/var'
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<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"/>
<server name="KERNEL_CLASS" value="Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Kernel"/>
</php>
<testsuites>
<testsuite name="tests">
Expand Down
61 changes: 61 additions & 0 deletions tests/Application/Controller/UserControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Tienvx\Bundle\PactMessengerBundle\Tests\Application\Controller;

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

class UserControllerTest extends WebTestCase
{
public function testCreateUser(): void
{
$client = static::createClient();
$client->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);
}
}
8 changes: 4 additions & 4 deletions tests/Integration/Service/EnvelopeCollectorTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Tests\Integration\Service;
namespace Tienvx\Bundle\PactMessengerBundle\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;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserCreated;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserDeleted;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserUpdated;

class EnvelopeCollectorTest extends KernelTestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

$configuration = [
'http_method_override' => false,
Expand All @@ -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'],
],
],
];
Expand Down
8 changes: 8 additions & 0 deletions tests/TestApplication/config/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Kernel;

return function (RoutingConfigurator $routes) {
$routes->import('../src/Controller/', Kernel::MAJOR_VERSION >= 6 ? 'attribute' : 'annotation');
};
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};
38 changes: 38 additions & 0 deletions tests/TestApplication/src/Controller/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserCreated;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserDeleted;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserUpdated;

class UserController
{
#[Route('/create', name: 'create_user', methods: Request::METHOD_POST)]
public function create(MessageBusInterface $bus): Response
{
$bus->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');
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

class UserCreated
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

class UserDeleted
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

class UserUpdated
{
Expand Down

0 comments on commit 5e9114e

Please sign in to comment.