Skip to content

Commit

Permalink
Subscribers can have same argument defined explicitly
Browse files Browse the repository at this point in the history
[Closes #94] [Closes #93]
  • Loading branch information
sneznaovca authored and fprochazka committed May 1, 2016
1 parent 616b598 commit a85bd6f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Kdyby/Events/DI/EventsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ public function loadConfiguration()
}

Nette\Utils\Validators::assertField($config, 'subscribers', 'array');
foreach ($config['subscribers'] as $subscriber) {
$def = $builder->addDefinition($this->prefix('subscriber.' . md5(Nette\Utils\Json::encode($subscriber))));
foreach ($config['subscribers'] as $i => $subscriber) {
$def = $builder->addDefinition($this->prefix('subscriber.' . $i));

$def->setFactory(Nette\DI\Compiler::filterArguments([
is_string($subscriber) ? new Nette\DI\Statement($subscriber) : $subscriber
])[0]);
Expand Down
13 changes: 13 additions & 0 deletions tests/KdybyTests/Events/Extension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ class ExtensionTest extends Tester\TestCase



public function testRegisterListenersWithSameArguments()
{
$container = $this->createContainer('subscribersWithSameArgument');
$manager = $container->getService('events.manager');

/** @var Kdyby\Events\EventManager $manager */
Assert::true($manager instanceof Kdyby\Events\EventManager);
Assert::same(['onFoo'], array_keys($manager->getListeners()));
Assert::count(2, $manager->getListeners('onFoo'));
}



public function testValidate_direct()
{
$me = $this;
Expand Down
10 changes: 10 additions & 0 deletions tests/KdybyTests/Events/config/subscribersWithSameArgument.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
events:
optimize: no
validate: no
autowire: no
subscribers:
- KdybyTests\Events\EventListenerConstructorMock(@routerFactory)
- KdybyTests\Events\EventListenerConstructorMock2(@routerFactory)

services:
routerFactory: KdybyTests\Events\RouterFactory
66 changes: 66 additions & 0 deletions tests/KdybyTests/Events/mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,72 @@ public function onBar()



/**
* @author Pavol Sivý <[email protected]>
*/
class EventListenerConstructorMock extends Nette\Object implements Kdyby\Events\Subscriber
{

public function __construct(RouterFactory $routerFactory)
{
}



/**
* @return array
*/
public function getSubscribedEvents()
{
return [
'onFoo',
];
}



public function onFoo(EventArgsMock $args)
{
// pass
}

}



/**
* @author Pavol Sivý <[email protected]>
*/
class EventListenerConstructorMock2 extends Nette\Object implements Kdyby\Events\Subscriber
{

public function __construct(RouterFactory $routerFactory)
{
}



/**
* @return array
*/
public function getSubscribedEvents()
{
return [
'onFoo',
];
}



public function onFoo(EventArgsMock $args)
{
// pass
}

}



/**
* @author Filip Procházka <[email protected]>
*/
Expand Down

0 comments on commit a85bd6f

Please sign in to comment.