1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace think\tests; |
4
|
|
|
|
5
|
|
|
use Mockery as m; |
6
|
|
|
use Mockery\MockInterface; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use think\App; |
9
|
|
|
use think\Config; |
10
|
|
|
use think\Container; |
11
|
|
|
use think\Event; |
12
|
|
|
|
13
|
|
|
class EventTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var App|MockInterface */ |
|
|
|
|
16
|
|
|
protected $app; |
17
|
|
|
|
18
|
|
|
/** @var Event|MockInterface */ |
|
|
|
|
19
|
|
|
protected $event; |
20
|
|
|
|
21
|
|
|
/** @var Config|MockInterface */ |
|
|
|
|
22
|
|
|
protected $config; |
23
|
|
|
|
24
|
|
|
protected function tearDown(): void |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
m::close(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function setUp() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->app = m::mock(App::class)->makePartial(); |
32
|
|
|
|
33
|
|
|
Container::setInstance($this->app); |
34
|
|
|
$this->app->shouldReceive('make')->with(App::class)->andReturn($this->app); |
35
|
|
|
$this->config = m::mock(Config::class)->makePartial(); |
36
|
|
|
$this->app->shouldReceive('get')->with('config')->andReturn($this->config); |
37
|
|
|
|
38
|
|
|
$this->event = new Event($this->app); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testBasic() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->event->bind(['foo' => 'baz']); |
44
|
|
|
|
45
|
|
|
$this->event->listen('foo', function ($bar) { |
|
|
|
|
46
|
|
|
$this->assertEquals('bar', $bar); |
47
|
|
|
}); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->assertTrue($this->event->hasListener('foo')); |
50
|
|
|
|
51
|
|
|
$this->event->trigger('baz', 'bar'); |
52
|
|
|
|
53
|
|
|
$this->event->remove('foo'); |
54
|
|
|
|
55
|
|
|
$this->assertFalse($this->event->hasListener('foo')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testOnceEvent() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->event->listen('AppInit', function ($bar) { |
|
|
|
|
61
|
|
|
$this->assertEquals('bar', $bar); |
62
|
|
|
return 'foo'; |
63
|
|
|
}); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->assertEquals('foo', $this->event->trigger('AppInit', 'bar', true)); |
66
|
|
|
$this->assertEquals(['foo'], $this->event->trigger('AppInit', 'bar')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testClassListener() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$listener = m::mock("overload:SomeListener", TestListener::class); |
72
|
|
|
|
73
|
|
|
$listener->shouldReceive('handle')->andReturnTrue(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->event->listen('some', "SomeListener"); |
76
|
|
|
|
77
|
|
|
$this->assertTrue($this->event->until('some'));; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testSubscribe() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$listener = m::mock("overload:SomeListener", TestListener::class); |
83
|
|
|
|
84
|
|
|
$listener->shouldReceive('subscribe')->andReturnUsing(function (Event $event) use ($listener) { |
|
|
|
|
85
|
|
|
|
86
|
|
|
$listener->shouldReceive('onBar')->once()->andReturnFalse(); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$event->listenEvents(['SomeListener::onBar' => [[$listener, 'onBar']]]); |
89
|
|
|
}); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->event->subscribe('SomeListener'); |
92
|
|
|
|
93
|
|
|
$this->assertTrue($this->event->hasListener('SomeListener::onBar')); |
94
|
|
|
|
95
|
|
|
$this->event->trigger('SomeListener::onBar'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testObserve() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$listener = m::mock("overload:SomeListener", TestListener::class); |
101
|
|
|
|
102
|
|
|
$listener->shouldReceive('onBar')->once(); |
103
|
|
|
|
104
|
|
|
$this->event->observe('SomeListener', ['bar', 'foo']); |
105
|
|
|
|
106
|
|
|
$this->event->trigger('bar'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testAutoObserve() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$listener = m::mock("overload:SomeListener", TestListener::class); |
112
|
|
|
|
113
|
|
|
$listener->shouldReceive('onBar')->once(); |
114
|
|
|
|
115
|
|
|
$this->app->shouldReceive('make')->with('SomeListener')->andReturn($listener); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$this->event->observe('SomeListener'); |
118
|
|
|
|
119
|
|
|
$this->event->trigger('bar'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testWithoutEvent() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$this->event->withEvent(false); |
125
|
|
|
|
126
|
|
|
$this->event->listen('SomeListener', TestListener::class); |
127
|
|
|
|
128
|
|
|
$this->assertFalse($this->event->hasListener('SomeListener')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
class TestListener |
134
|
|
|
{ |
135
|
|
|
public function handle() |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function onBar() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function onFoo() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function subscribe() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|