Skip to content

Commit d2856e4

Browse files
committed
another tests fixed
1 parent 8fa4296 commit d2856e4

File tree

4 files changed

+27
-49
lines changed

4 files changed

+27
-49
lines changed

ContainerAwareRegistry.php

+11-18
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Enqueue\AsyncEventDispatcher;
44

5-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
6-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
5+
use Psr\Container\ContainerInterface;
76

8-
class ContainerAwareRegistry implements Registry, ContainerAwareInterface
7+
class ContainerAwareRegistry implements Registry
98
{
10-
use ContainerAwareTrait;
9+
/**
10+
* @var ContainerInterface
11+
*/
12+
private $container;
1113

1214
/**
1315
* @var string[]
@@ -23,23 +25,21 @@ class ContainerAwareRegistry implements Registry, ContainerAwareInterface
2325
* @param string[] $eventsMap [eventName => transformerName]
2426
* @param string[] $transformersMap [transformerName => transformerServiceId]
2527
*/
26-
public function __construct(array $eventsMap, array $transformersMap)
28+
public function __construct(array $eventsMap, array $transformersMap, ContainerInterface $container)
2729
{
2830
$this->eventsMap = $eventsMap;
2931
$this->transformersMap = $transformersMap;
32+
$this->container = $container;
3033
}
3134

32-
/**
33-
* {@inheritdoc}
34-
*/
3535
public function getTransformerNameForEvent($eventName)
3636
{
3737
$transformerName = null;
3838
if (array_key_exists($eventName, $this->eventsMap)) {
3939
$transformerName = $this->eventsMap[$eventName];
4040
} else {
4141
foreach ($this->eventsMap as $eventNamePattern => $name) {
42-
if ('/' != $eventNamePattern[0]) {
42+
if ('/' !== $eventNamePattern[0]) {
4343
continue;
4444
}
4545

@@ -58,9 +58,6 @@ public function getTransformerNameForEvent($eventName)
5858
return $transformerName;
5959
}
6060

61-
/**
62-
* {@inheritdoc}
63-
*/
6461
public function getTransformer($name)
6562
{
6663
if (false == array_key_exists($name, $this->transformersMap)) {
@@ -69,12 +66,8 @@ public function getTransformer($name)
6966

7067
$transformer = $this->container->get($this->transformersMap[$name]);
7168

72-
if (false == $transformer instanceof EventTransformer) {
73-
throw new \LogicException(sprintf(
74-
'The container must return instance of %s but got %s',
75-
EventTransformer::class,
76-
is_object($transformer) ? get_class($transformer) : gettype($transformer)
77-
));
69+
if (false == $transformer instanceof EventTransformer) {
70+
throw new \LogicException(sprintf('The container must return instance of %s but got %s', EventTransformer::class, is_object($transformer) ? $transformer::class : gettype($transformer)));
7871
}
7972

8073
return $transformer;

DependencyInjection/Configuration.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
class Configuration implements ConfigurationInterface
99
{
10-
/**
11-
* {@inheritdoc}
12-
*/
13-
public function getConfigTreeBuilder()
10+
public function getConfigTreeBuilder(): TreeBuilder
1411
{
1512
if (method_exists(TreeBuilder::class, 'getRootNode')) {
1613
$tb = new TreeBuilder('enqueue_async_event_dispatcher');

Resources/config/services.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ services:
88
enqueue.events.registry:
99
class: 'Enqueue\AsyncEventDispatcher\ContainerAwareRegistry'
1010
public: false
11-
arguments: [[], []]
12-
calls:
13-
- ['setContainer', ['@service_container']]
11+
arguments: [[], [], '@service_container']
1412

1513
enqueue.events.async_listener:
1614
class: 'Enqueue\AsyncEventDispatcher\AsyncListener'

Tests/ContainerAwareRegistryTest.php

+14-24
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,25 @@ public function testShouldImplementRegistryInterface()
2020
$this->assertClassImplements(Registry::class, ContainerAwareRegistry::class);
2121
}
2222

23-
public function testCouldBeConstructedWithEventsMapAndTransformersMapAsArguments()
24-
{
25-
new ContainerAwareRegistry([], []);
26-
}
27-
28-
public function testShouldSetContainerToContainerProperty()
23+
public function testShouldAllowGetTransportNameByEventName()
2924
{
3025
$container = new Container();
3126

32-
$registry = new ContainerAwareRegistry([], []);
33-
34-
$registry->setContainer($container);
35-
36-
$this->assertAttributeSame($container, 'container', $registry);
37-
}
38-
39-
public function testShouldAllowGetTransportNameByEventName()
40-
{
4127
$registry = new ContainerAwareRegistry([
42-
'fooEvent' => 'fooTrans',
43-
], []);
28+
'fooEvent' => 'fooTrans',
29+
], [], $container);
4430

4531
$this->assertEquals('fooTrans', $registry->getTransformerNameForEvent('fooEvent'));
4632
}
4733

4834
public function testShouldAllowDefineTransportNameAsRegExpPattern()
4935
{
36+
$container = new Container();
37+
5038
$registry = new ContainerAwareRegistry([
5139
'/.*/' => 'fooRegExpTrans',
5240
'fooEvent' => 'fooTrans',
53-
], []);
41+
], [], $container);
5442

5543
// guard
5644
$this->assertEquals('fooTrans', $registry->getTransformerNameForEvent('fooEvent'));
@@ -60,9 +48,11 @@ public function testShouldAllowDefineTransportNameAsRegExpPattern()
6048

6149
public function testThrowIfNotSupportedEventGiven()
6250
{
51+
$container = new Container();
52+
6353
$registry = new ContainerAwareRegistry([
6454
'fooEvent' => 'fooTrans',
65-
], []);
55+
], [], $container);
6656

6757
$this->expectException(\LogicException::class);
6858
$this->expectExceptionMessage('There is no transformer registered for the given event fooNotSupportedEvent');
@@ -71,9 +61,11 @@ public function testThrowIfNotSupportedEventGiven()
7161

7262
public function testThrowIfThereIsNoRegisteredTransformerWithSuchName()
7363
{
64+
$container = new Container();
65+
7466
$registry = new ContainerAwareRegistry([], [
7567
'fooTrans' => 'foo_trans_id',
76-
]);
68+
], $container);
7769

7870
$this->expectException(\LogicException::class);
7971
$this->expectExceptionMessage('There is no transformer named fooNotRegisteredName');
@@ -87,8 +79,7 @@ public function testThrowIfContainerReturnsServiceNotInstanceOfEventTransformer(
8779

8880
$registry = new ContainerAwareRegistry([], [
8981
'fooTrans' => 'foo_trans_id',
90-
]);
91-
$registry->setContainer($container);
82+
], $container);
9283

9384
$this->expectException(\LogicException::class);
9485
$this->expectExceptionMessage('The container must return instance of Enqueue\AsyncEventDispatcher\EventTransformer but got stdClass');
@@ -104,8 +95,7 @@ public function testShouldReturnEventTransformer()
10495

10596
$registry = new ContainerAwareRegistry([], [
10697
'fooTrans' => 'foo_trans_id',
107-
]);
108-
$registry->setContainer($container);
98+
], $container);
10999

110100
$this->assertSame($eventTransformerMock, $registry->getTransformer('fooTrans'));
111101
}

0 commit comments

Comments
 (0)