zhaojs
2023-10-07 6c7bba2e05c011a3d640b6565a113204228e92e0
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
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
 
/**
 * Hook类测试
 * @author    liu21st <liu21st@gmail.com>
 */
 
namespace tests\thinkphp\library\think;
 
use think\Hook;
 
class hookTest extends \PHPUnit_Framework_TestCase
{
 
    public function testRun()
    {
        Hook::add('my_pos', '\tests\thinkphp\library\think\behavior\One');
        Hook::add('my_pos', ['\tests\thinkphp\library\think\behavior\Two']);
        Hook::add('my_pos', '\tests\thinkphp\library\think\behavior\Three', true);
        $data['id']   = 0;
        $data['name'] = 'thinkphp';
        Hook::listen('my_pos', $data);
        $this->assertEquals(2, $data['id']);
        $this->assertEquals('thinkphp', $data['name']);
        $this->assertEquals([
            '\tests\thinkphp\library\think\behavior\Three',
            '\tests\thinkphp\library\think\behavior\One',
            '\tests\thinkphp\library\think\behavior\Two'],
            Hook::get('my_pos'));
    }
 
    public function testImport()
    {
        Hook::import(['my_pos' => [
            '\tests\thinkphp\library\think\behavior\One',
            '\tests\thinkphp\library\think\behavior\Three'],
        ]);
        Hook::import(['my_pos' => ['\tests\thinkphp\library\think\behavior\Two']], false);
        Hook::import(['my_pos' => ['\tests\thinkphp\library\think\behavior\Three', '_overlay' => true]]);
        $data['id']   = 0;
        $data['name'] = 'thinkphp';
        Hook::listen('my_pos', $data);
        $this->assertEquals(3, $data['id']);
 
    }
 
    public function testExec()
    {
        $data['id']   = 0;
        $data['name'] = 'thinkphp';
        $this->assertEquals(true, Hook::exec('\tests\thinkphp\library\think\behavior\One'));
        $this->assertEquals(false, Hook::exec('\tests\thinkphp\library\think\behavior\One', 'test', $data));
        $this->assertEquals('test', $data['name']);
        $this->assertEquals('Closure', Hook::exec(function (&$data) {$data['name'] = 'Closure';return 'Closure';}));
 
    }
 
}