laravel模块之事件

事件

1
2
3
4
5
6
"require": {
"php": "^7.1.3",
"illuminate/container": "5.7.*",
"illuminate/contracts": "5.7.*",
"illuminate/support": "5.7.*"
},

测试

监听listen、触发fire

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
<?php
/**
* Created by PhpStorm.
* User: 小粽子
* Date: 2018/6/16
* Time: 15:32
*/
namespace Tests\Events;

use Illuminate\Events\Dispatcher;
use PHPUnit\Framework\TestCase;
use Mockery as m;

class EventsTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function testBasicEventExecution()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher();
$d->listen('foo', function ($foo) {
$_SERVER['__event.test'] = $foo;
});
$d->fire('foo', ['bar']);
$this->assertEquals('bar', $_SERVER['__event.test']);
}

public function testContainerResolutionOfEventHandlers()
{
$d = new Dispatcher($container = m::mock('Illuminate\Container\Container'));
$container->shouldReceive('make')->once()->with('FooHandler')->andReturn($handler = m::mock('stdClass'));
$handler->shouldReceive('onFooEvent')->once()->with('foo', 'bar');
$d->listen('foo', 'FooHandler@onFooEvent');
$d->fire('foo', ['foo', 'bar']);
}

public function testMock()
{
$mock = m::mock(MM::class);
$mock->shouldReceive('getName')->once()->with('zui')->andReturn('zui');
$this->assertEquals($mock->getName('zui'), (new MM())->getName('zui'));
}

}

class MM
{
public function getName($name)
{
return $name;
}
}