Completed
Branch 6.0 (d30585)
by yun
04:17
created

EventTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 96
rs 10
wmc 7
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 testAutoObserve()
99
    {
100
        $listener = m::mock("overload:SomeListener", TestListener::class);
101
102
        $listener->shouldReceive('onBar')->once();
103
104
        $this->app->shouldReceive('make')->with('SomeListener')->andReturn($listener);
105
106
        $this->event->observe('SomeListener');
107
108
        $this->event->trigger('bar');
109
    }
110
111
}
112
113
class TestListener
114
{
115
    public function handle()
116
    {
117
118
    }
119
120
    public function onBar()
121
    {
122
123
    }
124
125
    public function onFoo()
126
    {
127
128
    }
129
130
    public function subscribe()
131
    {
132
133
    }
134
}
135