|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Config; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Psr\Container\ContainerInterface; |
|
7
|
|
|
use Yiisoft\EventDispatcher\Provider\Provider; |
|
8
|
|
|
use Yiisoft\Yii\Web\Config\EventConfigurator; |
|
9
|
|
|
|
|
10
|
|
|
class EventConfiguratorTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
public function testAddEventListeners(): void |
|
13
|
|
|
{ |
|
14
|
|
|
$event = new Event(); |
|
15
|
|
|
|
|
16
|
|
|
$container = $this->getContainer([Event::class => new Event()]); |
|
17
|
|
|
$provider = new Provider(); |
|
18
|
|
|
$configurator = new EventConfigurator($provider, $container); |
|
19
|
|
|
$eventConfig = $this->getEventsConfig(); |
|
20
|
|
|
$configurator->registerListeners($eventConfig); |
|
21
|
|
|
$listeners = iterator_to_array($provider->getListenersForEvent($event)); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertCount(2, $listeners); |
|
24
|
|
|
$this->assertSame($eventConfig[0], $listeners[0]); |
|
25
|
|
|
$this->assertInstanceOf(Event::class, $listeners[1][0]); |
|
26
|
|
|
$this->assertSame('register', $listeners[1][1]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function getEventsConfig(): array |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
function (Event $event) { |
|
33
|
|
|
$event->register(1); |
|
34
|
|
|
}, |
|
35
|
|
|
Event::class => [ |
|
36
|
|
|
[Event::class, 'register'] |
|
37
|
|
|
], |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function getContainer(array $instances): ContainerInterface |
|
42
|
|
|
{ |
|
43
|
|
|
return new class($instances) implements ContainerInterface { |
|
44
|
|
|
private array $instances; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct(array $instances) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->instances = $instances; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function get($id) |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->instances[$id]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function has($id) |
|
57
|
|
|
{ |
|
58
|
|
|
return isset($this->instances[$id]); |
|
59
|
|
|
} |
|
60
|
|
|
}; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|