|
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
|
|
|
use Yiisoft\Yii\Web\Session\Session; |
|
10
|
|
|
|
|
11
|
|
|
class EventConfiguratorTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function testAddEventListeners(): void |
|
14
|
|
|
{ |
|
15
|
|
|
$event = new Event(); |
|
16
|
|
|
|
|
17
|
|
|
$container = $this->getContainer([Event::class => new Event()]); |
|
18
|
|
|
$provider = new Provider(); |
|
19
|
|
|
$configurator = new EventConfigurator($provider, $container); |
|
20
|
|
|
$eventConfig = $this->getEventsConfig(); |
|
21
|
|
|
$configurator->registerListeners($eventConfig); |
|
22
|
|
|
$listeners = iterator_to_array($provider->getListenersForEvent($event)); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertCount(2, $listeners); |
|
25
|
|
|
$this->assertInstanceOf(\Closure::class, $listeners[0]); |
|
26
|
|
|
$this->assertInstanceOf(\Closure::class, $listeners[1]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testAddEventListenerInjection(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$event = new Event(); |
|
32
|
|
|
|
|
33
|
|
|
$container = $this->getContainer([ |
|
34
|
|
|
Event::class => new Event(), |
|
35
|
|
|
Session::class => new Session(), |
|
36
|
|
|
]); |
|
37
|
|
|
$provider = new Provider(); |
|
38
|
|
|
$configurator = new EventConfigurator($provider, $container); |
|
39
|
|
|
$eventConfig = $this->getEventsConfigWithDependency(); |
|
40
|
|
|
$configurator->registerListeners($eventConfig); |
|
41
|
|
|
$listeners = iterator_to_array($provider->getListenersForEvent($event)); |
|
42
|
|
|
$listeners[0]($event); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertInstanceOf(Session::class, $event->registered()[0]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function getEventsConfig(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
Event::class => [ |
|
51
|
|
|
[Event::class, 'register'], |
|
52
|
|
|
static function (Event $event) { |
|
53
|
|
|
$event->register(1); |
|
54
|
|
|
}, |
|
55
|
|
|
], |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function getEventsConfigWithDependency(): array |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
|
|
Event::class => [ |
|
63
|
|
|
static function (Event $event, Session $session) { |
|
64
|
|
|
$event->register($session); |
|
65
|
|
|
}, |
|
66
|
|
|
], |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function getContainer(array $instances): ContainerInterface |
|
71
|
|
|
{ |
|
72
|
|
|
return new class($instances) implements ContainerInterface { |
|
73
|
|
|
private array $instances; |
|
74
|
|
|
|
|
75
|
|
|
public function __construct(array $instances) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->instances = $instances; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function get($id) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->instances[$id]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function has($id) |
|
86
|
|
|
{ |
|
87
|
|
|
return isset($this->instances[$id]); |
|
88
|
|
|
} |
|
89
|
|
|
}; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|