Skip to content

Commit a012766

Browse files
committed
add functional tests for auto-register feature
1 parent 878d2df commit a012766

File tree

9 files changed

+151
-53
lines changed

9 files changed

+151
-53
lines changed

tests/Functional/SmokeTest.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@
44

55
use Doctrine\ORM\EntityManager;
66
use Doctrine\ORM\Tools\SchemaTool;
7+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommand;
8+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent;
79
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestCommand;
810
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestKernel;
11+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
912
use Symfony\Component\DependencyInjection\ContainerInterface;
1013

11-
class SmokeTest extends \PHPUnit_Framework_TestCase
14+
class SmokeTest extends KernelTestCase
1215
{
16+
protected static function getKernelClass()
17+
{
18+
return TestKernel::class;
19+
}
20+
1321
/**
1422
* @test
1523
*/
1624
public function it_handles_a_command_then_dispatches_events_for_all_modified_entities()
1725
{
18-
$kernel = new TestKernel('test', true);
19-
$kernel->boot();
20-
$container = $kernel->getContainer();
26+
self::bootKernel(['environment' => 'config1']);
27+
$container = self::$kernel->getContainer();
2128

2229
$this->createSchema($container);
2330

@@ -42,6 +49,42 @@ public function it_handles_a_command_then_dispatches_events_for_all_modified_ent
4249
$this->assertContains('event_bus.DEBUG: Finished notifying a subscriber', $loggedMessages);
4350
}
4451

52+
/**
53+
* @test
54+
*/
55+
public function it_can_auto_register_event_subscribers()
56+
{
57+
self::bootKernel(['environment' => 'config2']);
58+
$container = self::$kernel->getContainer();
59+
60+
$subscriber = $container->get('auto_event_subscriber');
61+
$event = new AutoEvent();
62+
63+
$this->assertNull($subscriber->handled);
64+
65+
$container->get('event_bus')->handle($event);
66+
67+
$this->assertSame($event, $subscriber->handled);
68+
}
69+
70+
/**
71+
* @test
72+
*/
73+
public function it_can_auto_register_command_handlers()
74+
{
75+
self::bootKernel(['environment' => 'config2']);
76+
$container = self::$kernel->getContainer();
77+
78+
$handler = $container->get('auto_command_handler');
79+
$command = new AutoCommand();
80+
81+
$this->assertNull($handler->handled);
82+
83+
$container->get('command_bus')->handle($command);
84+
85+
$this->assertSame($command, $handler->handled);
86+
}
87+
4588
private function createSchema(ContainerInterface $container)
4689
{
4790
$entityManager = $container->get('doctrine.orm.entity_manager');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
4+
5+
final class AutoCommand
6+
{
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
4+
5+
final class AutoCommandHandler
6+
{
7+
public $handled;
8+
9+
public function __invoke(AutoCommand $command)
10+
{
11+
$this->handled = $command;
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
4+
5+
final class AutoEvent
6+
{
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
4+
5+
final class AutoEventSubscriber
6+
{
7+
public $handled;
8+
9+
public function __invoke(AutoEvent $event)
10+
{
11+
$this->handled = $event;
12+
}
13+
}

tests/Functional/SmokeTest/TestKernel.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public function __construct($environment, $debug)
1818
{
1919
parent::__construct($environment, $debug);
2020

21-
$this->tempDir = sys_get_temp_dir() . '/' . uniqid();
22-
mkdir($this->tempDir, 0777, true);
21+
$this->tempDir = sys_get_temp_dir() . '/simplebus-symfony-bridge';
2322
}
2423

2524
public function registerBundles()
@@ -35,7 +34,7 @@ public function registerBundles()
3534

3635
public function registerContainerConfiguration(LoaderInterface $loader)
3736
{
38-
$loader->load(__DIR__ . '/config.yml');
37+
$loader->load(sprintf('%s/%s.yml', __DIR__, $this->environment));
3938
}
4039

4140
public function getCacheDir()

tests/Functional/SmokeTest/config.yml

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,7 @@
1-
parameters:
2-
log_file: %kernel.logs_dir%/%kernel.environment%.log
3-
41
services:
52
annotation_reader:
63
class: Doctrine\Common\Annotations\AnnotationReader
74

8-
test_command_handler:
9-
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestCommandHandler
10-
arguments:
11-
- '@doctrine.orm.default_entity_manager'
12-
tags:
13-
- { name: command_handler, handles: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestCommand }
14-
15-
some_other_test_command_handler:
16-
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\SomeOtherTestCommandHandler
17-
arguments:
18-
- '@event_recorder'
19-
tags:
20-
- { name: command_handler, handles: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\SomeOtherTestCommand }
21-
22-
test_event_subscriber:
23-
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestEntityCreatedEventSubscriber
24-
tags:
25-
- { name: event_subscriber, subscribes_to: test_entity_created }
26-
arguments:
27-
- '@command_bus'
28-
29-
some_other_event_subscriber:
30-
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\SomeOtherEventSubscriber
31-
tags:
32-
- { name: event_subscriber, subscribes_to: some_other_event }
33-
arguments:
34-
- '@command_bus'
35-
365
doctrine:
376
dbal:
387
driver: pdo_sqlite
@@ -49,18 +18,3 @@ doctrine:
4918
prefix: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Entity
5019
alias: Test
5120
is_bundle: false
52-
53-
command_bus:
54-
command_name_resolver_strategy: class_based
55-
logging: ~
56-
57-
event_bus:
58-
event_name_resolver_strategy: named_message
59-
logging: ~
60-
61-
monolog:
62-
handlers:
63-
main:
64-
type: stream
65-
path: %log_file%
66-
level: debug
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
imports:
2+
- { resource: config.yml }
3+
4+
parameters:
5+
log_file: %kernel.logs_dir%/%kernel.environment%.log
6+
7+
services:
8+
test_command_handler:
9+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestCommandHandler
10+
arguments:
11+
- '@doctrine.orm.default_entity_manager'
12+
tags:
13+
- { name: command_handler, handles: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestCommand }
14+
15+
some_other_test_command_handler:
16+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\SomeOtherTestCommandHandler
17+
arguments:
18+
- '@event_recorder'
19+
tags:
20+
- { name: command_handler, handles: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\SomeOtherTestCommand }
21+
22+
test_event_subscriber:
23+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestEntityCreatedEventSubscriber
24+
tags:
25+
- { name: event_subscriber, subscribes_to: test_entity_created }
26+
arguments:
27+
- '@command_bus'
28+
29+
some_other_event_subscriber:
30+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\SomeOtherEventSubscriber
31+
tags:
32+
- { name: event_subscriber, subscribes_to: some_other_event }
33+
arguments:
34+
- '@command_bus'
35+
36+
command_bus:
37+
command_name_resolver_strategy: class_based
38+
logging: ~
39+
40+
event_bus:
41+
event_name_resolver_strategy: named_message
42+
logging: ~
43+
44+
monolog:
45+
handlers:
46+
main:
47+
type: stream
48+
path: %log_file%
49+
level: debug
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
imports:
2+
- { resource: config.yml }
3+
4+
services:
5+
auto_command_handler:
6+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommandHandler
7+
tags:
8+
- { name: command_handler }
9+
10+
auto_event_subscriber:
11+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEventSubscriber
12+
tags:
13+
- { name: event_subscriber }

0 commit comments

Comments
 (0)