EventFakeTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Illuminate\Tests\Integration\Events;
  3. use Closure;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Support\Facades\Event;
  7. use Illuminate\Support\Facades\Schema;
  8. use Orchestra\Testbench\TestCase;
  9. class EventFakeTest extends TestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. Schema::create('posts', function (Blueprint $table) {
  15. $table->increments('id');
  16. $table->string('title');
  17. $table->string('slug')->unique();
  18. $table->timestamps();
  19. });
  20. }
  21. protected function tearDown(): void
  22. {
  23. Schema::dropIfExists('posts');
  24. parent::tearDown();
  25. }
  26. public function testNonFakedEventGetsProperlyDispatched()
  27. {
  28. Event::fake(NonImportantEvent::class);
  29. Post::observe([PostObserver::class]);
  30. $post = new Post;
  31. $post->title = 'xyz';
  32. $post->save();
  33. $this->assertSame('xyz-Test', $post->slug);
  34. Event::assertNotDispatched(NonImportantEvent::class);
  35. }
  36. public function testNonFakedEventGetsProperlyDispatchedAndReturnsResponses()
  37. {
  38. Event::fake(NonImportantEvent::class);
  39. Event::listen('test', function () {
  40. // one
  41. });
  42. Event::listen('test', function () {
  43. return 'two';
  44. });
  45. Event::listen('test', function () {
  46. //
  47. });
  48. $this->assertEquals([null, 'two', null], Event::dispatch('test'));
  49. Event::assertNotDispatched(NonImportantEvent::class);
  50. }
  51. public function testNonFakedEventGetsProperlyDispatchedAndCancelsFutureListeners()
  52. {
  53. Event::fake(NonImportantEvent::class);
  54. Event::listen('test', function () {
  55. // one
  56. });
  57. Event::listen('test', function () {
  58. return false;
  59. });
  60. Event::listen('test', function () {
  61. $this->fail('should not be called');
  62. });
  63. $this->assertEquals([null], Event::dispatch('test'));
  64. Event::assertNotDispatched(NonImportantEvent::class);
  65. }
  66. public function testNonFakedHaltedEventGetsProperlyDispatchedAndReturnsResponse()
  67. {
  68. Event::fake(NonImportantEvent::class);
  69. Event::listen('test', function () {
  70. // one
  71. });
  72. Event::listen('test', function () {
  73. return 'two';
  74. });
  75. Event::listen('test', function () {
  76. $this->fail('should not be called');
  77. });
  78. $this->assertSame('two', Event::until('test'));
  79. Event::assertNotDispatched(NonImportantEvent::class);
  80. }
  81. public function testFakeExceptAllowsGivenEventToBeDispatched()
  82. {
  83. Event::fakeExcept(NonImportantEvent::class);
  84. Event::dispatch(NonImportantEvent::class);
  85. Event::assertNotDispatched(NonImportantEvent::class);
  86. }
  87. public function testFakeExceptAllowsGivenEventsToBeDispatched()
  88. {
  89. Event::fakeExcept([
  90. NonImportantEvent::class,
  91. 'non-fake-event',
  92. ]);
  93. Event::dispatch(NonImportantEvent::class);
  94. Event::dispatch('non-fake-event');
  95. Event::assertNotDispatched(NonImportantEvent::class);
  96. Event::assertNotDispatched('non-fake-event');
  97. }
  98. public function testAssertListening()
  99. {
  100. Event::fake();
  101. Event::listen('event', 'listener');
  102. Event::listen('event', PostEventSubscriber::class);
  103. Event::listen('event', 'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle');
  104. Event::listen('event', [PostEventSubscriber::class, 'foo']);
  105. Event::subscribe(PostEventSubscriber::class);
  106. Event::listen(function (NonImportantEvent $event) {
  107. // do something
  108. });
  109. Event::assertListening('event', 'listener');
  110. Event::assertListening('event', PostEventSubscriber::class);
  111. Event::assertListening('event', PostAutoEventSubscriber::class);
  112. Event::assertListening('event', [PostEventSubscriber::class, 'foo']);
  113. Event::assertListening('post-created', [PostEventSubscriber::class, 'handlePostCreated']);
  114. Event::assertListening(NonImportantEvent::class, Closure::class);
  115. }
  116. }
  117. class Post extends Model
  118. {
  119. public $table = 'posts';
  120. }
  121. class NonImportantEvent
  122. {
  123. //
  124. }
  125. class PostEventSubscriber
  126. {
  127. public function handlePostCreated($event)
  128. {
  129. }
  130. public function subscribe($events)
  131. {
  132. $events->listen(
  133. 'post-created',
  134. [PostEventSubscriber::class, 'handlePostCreated']
  135. );
  136. }
  137. }
  138. class PostAutoEventSubscriber
  139. {
  140. public function handle($event)
  141. {
  142. //
  143. }
  144. }
  145. class PostObserver
  146. {
  147. public function saving(Post $post)
  148. {
  149. $post->slug = sprintf('%s-Test', $post->title);
  150. }
  151. }