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