Completed
Push — 6.0 ( 8c479e...198445 )
by liu
05:57
created

EventTest::testWithoutEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A TestListener::handle() 0 2 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
16
    protected $app;
17
18
    /** @var Event|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
19
    protected $event;
20
21
    /** @var Config|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
22
    protected $config;
23
24
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
25
    {
26
        m::close();
27
    }
28
29
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
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);
0 ignored issues
show
Bug introduced by
$this->app of type Mockery\Mock is incompatible with the type think\App expected by parameter $app of think\Event::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $this->event = new Event(/** @scrutinizer ignore-type */ $this->app);
Loading history...
39
    }
40
41
    public function testBasic()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testBasic()
Loading history...
42
    {
43
        $this->event->bind(['foo' => 'baz']);
44
45
        $this->event->listen('foo', function ($bar) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
46
            $this->assertEquals('bar', $bar);
47
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testOnceEvent()
Loading history...
59
    {
60
        $this->event->listen('AppInit', function ($bar) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
61
            $this->assertEquals('bar', $bar);
62
            return 'foo';
63
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testClassListener()
Loading history...
70
    {
71
        $listener = m::mock("overload:SomeListener", TestListener::class);
72
73
        $listener->shouldReceive('handle')->andReturnTrue();
0 ignored issues
show
Bug introduced by
The method andReturnTrue() does not exist on Mockery\ExpectationInterface. Did you maybe mean andReturn()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $listener->shouldReceive('handle')->/** @scrutinizer ignore-call */ andReturnTrue();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
75
        $this->event->listen('some', "SomeListener");
76
77
        $this->assertTrue($this->event->until('some'));
78
    }
79
80
    public function testSubscribe()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testSubscribe()
Loading history...
81
    {
82
        $listener = m::mock("overload:SomeListener", TestListener::class);
83
84
        $listener->shouldReceive('subscribe')->andReturnUsing(function (Event $event) use ($listener) {
0 ignored issues
show
Bug introduced by
The method andReturnUsing() does not exist on Mockery\ExpectationInterface. Did you maybe mean andReturn()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        $listener->shouldReceive('subscribe')->/** @scrutinizer ignore-call */ andReturnUsing(function (Event $event) use ($listener) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
85
86
            $listener->shouldReceive('onBar')->once()->andReturnFalse();
0 ignored issues
show
Bug introduced by
The method andReturnFalse() does not exist on Mockery\ExpectationInterface. Did you maybe mean andReturn()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
            $listener->shouldReceive('onBar')->once()->/** @scrutinizer ignore-call */ andReturnFalse();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
88
            $event->listenEvents(['SomeListener::onBar' => [[$listener, 'onBar']]]);
89
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testAutoObserve()
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on think\App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
        $this->app->/** @scrutinizer ignore-call */ 
105
                    shouldReceive('make')->with('SomeListener')->andReturn($listener);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
106
        $this->event->observe('SomeListener');
107
108
        $this->event->trigger('bar');
109
    }
110
111
}
112
113
class TestListener
114
{
115
    public function handle()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function handle()
Loading history...
116
    {
117
118
    }
119
120
    public function onBar()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function onBar()
Loading history...
121
    {
122
123
    }
124
125
    public function onFoo()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function onFoo()
Loading history...
126
    {
127
128
    }
129
130
    public function subscribe()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function subscribe()
Loading history...
131
    {
132
133
    }
134
}
135