EventTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace think\tests;
  3. use Mockery as m;
  4. use Mockery\MockInterface;
  5. use PHPUnit\Framework\TestCase;
  6. use think\App;
  7. use think\Config;
  8. use think\Container;
  9. use think\Event;
  10. class EventTest extends TestCase
  11. {
  12. /** @var App|MockInterface */
  13. protected $app;
  14. /** @var Event|MockInterface */
  15. protected $event;
  16. /** @var Config|MockInterface */
  17. protected $config;
  18. protected function tearDown(): void
  19. {
  20. m::close();
  21. }
  22. protected function setUp()
  23. {
  24. $this->app = m::mock(App::class)->makePartial();
  25. Container::setInstance($this->app);
  26. $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
  27. $this->config = m::mock(Config::class)->makePartial();
  28. $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
  29. $this->event = new Event($this->app);
  30. }
  31. public function testBasic()
  32. {
  33. $this->event->bind(['foo' => 'baz']);
  34. $this->event->listen('foo', function ($bar) {
  35. $this->assertEquals('bar', $bar);
  36. });
  37. $this->assertTrue($this->event->hasListener('foo'));
  38. $this->event->trigger('baz', 'bar');
  39. $this->event->remove('foo');
  40. $this->assertFalse($this->event->hasListener('foo'));
  41. }
  42. public function testOnceEvent()
  43. {
  44. $this->event->listen('AppInit', function ($bar) {
  45. $this->assertEquals('bar', $bar);
  46. return 'foo';
  47. });
  48. $this->assertEquals('foo', $this->event->trigger('AppInit', 'bar', true));
  49. $this->assertEquals(['foo'], $this->event->trigger('AppInit', 'bar'));
  50. }
  51. public function testClassListener()
  52. {
  53. $listener = m::mock("overload:SomeListener", TestListener::class);
  54. $listener->shouldReceive('handle')->andReturnTrue();
  55. $this->event->listen('some', "SomeListener");
  56. $this->assertTrue($this->event->until('some'));
  57. }
  58. public function testSubscribe()
  59. {
  60. $listener = m::mock("overload:SomeListener", TestListener::class);
  61. $listener->shouldReceive('subscribe')->andReturnUsing(function (Event $event) use ($listener) {
  62. $listener->shouldReceive('onBar')->once()->andReturnFalse();
  63. $event->listenEvents(['SomeListener::onBar' => [[$listener, 'onBar']]]);
  64. });
  65. $this->event->subscribe('SomeListener');
  66. $this->assertTrue($this->event->hasListener('SomeListener::onBar'));
  67. $this->event->trigger('SomeListener::onBar');
  68. }
  69. public function testAutoObserve()
  70. {
  71. $listener = m::mock("overload:SomeListener", TestListener::class);
  72. $listener->shouldReceive('onBar')->once();
  73. $this->app->shouldReceive('make')->with('SomeListener')->andReturn($listener);
  74. $this->event->observe('SomeListener');
  75. $this->event->trigger('bar');
  76. }
  77. }
  78. class TestListener
  79. {
  80. public function handle()
  81. {
  82. }
  83. public function onBar()
  84. {
  85. }
  86. public function onFoo()
  87. {
  88. }
  89. public function subscribe()
  90. {
  91. }
  92. }