Skip to content

Commit

Permalink
another tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
zavitkov committed Nov 15, 2024
1 parent 8fa4296 commit d2856e4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 49 deletions.
29 changes: 11 additions & 18 deletions ContainerAwareRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Enqueue\AsyncEventDispatcher;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Psr\Container\ContainerInterface;

class ContainerAwareRegistry implements Registry, ContainerAwareInterface
class ContainerAwareRegistry implements Registry
{
use ContainerAwareTrait;
/**
* @var ContainerInterface
*/
private $container;

/**
* @var string[]
Expand All @@ -23,23 +25,21 @@ class ContainerAwareRegistry implements Registry, ContainerAwareInterface
* @param string[] $eventsMap [eventName => transformerName]
* @param string[] $transformersMap [transformerName => transformerServiceId]
*/
public function __construct(array $eventsMap, array $transformersMap)
public function __construct(array $eventsMap, array $transformersMap, ContainerInterface $container)
{
$this->eventsMap = $eventsMap;
$this->transformersMap = $transformersMap;
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function getTransformerNameForEvent($eventName)
{
$transformerName = null;
if (array_key_exists($eventName, $this->eventsMap)) {
$transformerName = $this->eventsMap[$eventName];
} else {
foreach ($this->eventsMap as $eventNamePattern => $name) {
if ('/' != $eventNamePattern[0]) {
if ('/' !== $eventNamePattern[0]) {
continue;
}

Expand All @@ -58,9 +58,6 @@ public function getTransformerNameForEvent($eventName)
return $transformerName;
}

/**
* {@inheritdoc}
*/
public function getTransformer($name)
{
if (false == array_key_exists($name, $this->transformersMap)) {
Expand All @@ -69,12 +66,8 @@ public function getTransformer($name)

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

if (false == $transformer instanceof EventTransformer) {
throw new \LogicException(sprintf(
'The container must return instance of %s but got %s',
EventTransformer::class,
is_object($transformer) ? get_class($transformer) : gettype($transformer)
));
if (false == $transformer instanceof EventTransformer) {
throw new \LogicException(sprintf('The container must return instance of %s but got %s', EventTransformer::class, is_object($transformer) ? $transformer::class : gettype($transformer)));
}

return $transformer;
Expand Down
5 changes: 1 addition & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$tb = new TreeBuilder('enqueue_async_event_dispatcher');
Expand Down
4 changes: 1 addition & 3 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ services:
enqueue.events.registry:
class: 'Enqueue\AsyncEventDispatcher\ContainerAwareRegistry'
public: false
arguments: [[], []]
calls:
- ['setContainer', ['@service_container']]
arguments: [[], [], '@service_container']

enqueue.events.async_listener:
class: 'Enqueue\AsyncEventDispatcher\AsyncListener'
Expand Down
38 changes: 14 additions & 24 deletions Tests/ContainerAwareRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,25 @@ public function testShouldImplementRegistryInterface()
$this->assertClassImplements(Registry::class, ContainerAwareRegistry::class);
}

public function testCouldBeConstructedWithEventsMapAndTransformersMapAsArguments()
{
new ContainerAwareRegistry([], []);
}

public function testShouldSetContainerToContainerProperty()
public function testShouldAllowGetTransportNameByEventName()
{
$container = new Container();

$registry = new ContainerAwareRegistry([], []);

$registry->setContainer($container);

$this->assertAttributeSame($container, 'container', $registry);
}

public function testShouldAllowGetTransportNameByEventName()
{
$registry = new ContainerAwareRegistry([
'fooEvent' => 'fooTrans',
], []);
'fooEvent' => 'fooTrans',
], [], $container);

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

public function testShouldAllowDefineTransportNameAsRegExpPattern()
{
$container = new Container();

$registry = new ContainerAwareRegistry([
'/.*/' => 'fooRegExpTrans',
'fooEvent' => 'fooTrans',
], []);
], [], $container);

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

public function testThrowIfNotSupportedEventGiven()
{
$container = new Container();

$registry = new ContainerAwareRegistry([
'fooEvent' => 'fooTrans',
], []);
], [], $container);

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

public function testThrowIfThereIsNoRegisteredTransformerWithSuchName()
{
$container = new Container();

$registry = new ContainerAwareRegistry([], [
'fooTrans' => 'foo_trans_id',
]);
], $container);

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

$registry = new ContainerAwareRegistry([], [
'fooTrans' => 'foo_trans_id',
]);
$registry->setContainer($container);
], $container);

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

$registry = new ContainerAwareRegistry([], [
'fooTrans' => 'foo_trans_id',
]);
$registry->setContainer($container);
], $container);

$this->assertSame($eventTransformerMock, $registry->getTransformer('fooTrans'));
}
Expand Down

0 comments on commit d2856e4

Please sign in to comment.