hookTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * Hook类测试
  13. * @author liu21st <liu21st@gmail.com>
  14. */
  15. namespace tests\thinkphp\library\think;
  16. use think\Hook;
  17. class hookTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testRun()
  20. {
  21. Hook::add('my_pos', '\tests\thinkphp\library\think\behavior\One');
  22. Hook::add('my_pos', ['\tests\thinkphp\library\think\behavior\Two']);
  23. Hook::add('my_pos', '\tests\thinkphp\library\think\behavior\Three', true);
  24. $data['id'] = 0;
  25. $data['name'] = 'thinkphp';
  26. Hook::listen('my_pos', $data);
  27. $this->assertEquals(2, $data['id']);
  28. $this->assertEquals('thinkphp', $data['name']);
  29. $this->assertEquals([
  30. '\tests\thinkphp\library\think\behavior\Three',
  31. '\tests\thinkphp\library\think\behavior\One',
  32. '\tests\thinkphp\library\think\behavior\Two'],
  33. Hook::get('my_pos'));
  34. }
  35. public function testImport()
  36. {
  37. Hook::import(['my_pos' => [
  38. '\tests\thinkphp\library\think\behavior\One',
  39. '\tests\thinkphp\library\think\behavior\Three'],
  40. ]);
  41. Hook::import(['my_pos' => ['\tests\thinkphp\library\think\behavior\Two']], false);
  42. Hook::import(['my_pos' => ['\tests\thinkphp\library\think\behavior\Three', '_overlay' => true]]);
  43. $data['id'] = 0;
  44. $data['name'] = 'thinkphp';
  45. Hook::listen('my_pos', $data);
  46. $this->assertEquals(3, $data['id']);
  47. }
  48. public function testExec()
  49. {
  50. $data['id'] = 0;
  51. $data['name'] = 'thinkphp';
  52. $this->assertEquals(true, Hook::exec('\tests\thinkphp\library\think\behavior\One'));
  53. $this->assertEquals(false, Hook::exec('\tests\thinkphp\library\think\behavior\One', 'test', $data));
  54. $this->assertEquals('test', $data['name']);
  55. $this->assertEquals('Closure', Hook::exec(function (&$data) {$data['name'] = 'Closure';return 'Closure';}));
  56. }
  57. }