SupportTestingEventFakeTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Illuminate\Contracts\Events\Dispatcher;
  4. use Illuminate\Support\Testing\Fakes\EventFake;
  5. use Mockery as m;
  6. use PHPUnit\Framework\Constraint\ExceptionMessage;
  7. use PHPUnit\Framework\ExpectationFailedException;
  8. use PHPUnit\Framework\TestCase;
  9. class SupportTestingEventFakeTest extends TestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. $this->fake = new EventFake(m::mock(Dispatcher::class));
  15. }
  16. public function testAssertDispatched()
  17. {
  18. try {
  19. $this->fake->assertDispatched(EventStub::class);
  20. $this->fail();
  21. } catch (ExpectationFailedException $e) {
  22. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\EventStub] event was not dispatched.'));
  23. }
  24. $this->fake->dispatch(EventStub::class);
  25. $this->fake->assertDispatched(EventStub::class);
  26. }
  27. public function testAssertDispatchedWithClosure()
  28. {
  29. $this->fake->dispatch(new EventStub);
  30. $this->fake->assertDispatched(function (EventStub $event) {
  31. return true;
  32. });
  33. }
  34. public function testAssertDispatchedWithCallbackInt()
  35. {
  36. $this->fake->dispatch(EventStub::class);
  37. $this->fake->dispatch(EventStub::class);
  38. try {
  39. $this->fake->assertDispatched(EventStub::class, 1);
  40. $this->fail();
  41. } catch (ExpectationFailedException $e) {
  42. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\EventStub] event was dispatched 2 times instead of 1 times.'));
  43. }
  44. $this->fake->assertDispatched(EventStub::class, 2);
  45. }
  46. public function testAssertDispatchedTimes()
  47. {
  48. $this->fake->dispatch(EventStub::class);
  49. $this->fake->dispatch(EventStub::class);
  50. try {
  51. $this->fake->assertDispatchedTimes(EventStub::class, 1);
  52. $this->fail();
  53. } catch (ExpectationFailedException $e) {
  54. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\EventStub] event was dispatched 2 times instead of 1 times.'));
  55. }
  56. $this->fake->assertDispatchedTimes(EventStub::class, 2);
  57. }
  58. public function testAssertNotDispatched()
  59. {
  60. $this->fake->assertNotDispatched(EventStub::class);
  61. $this->fake->dispatch(EventStub::class);
  62. try {
  63. $this->fake->assertNotDispatched(EventStub::class);
  64. $this->fail();
  65. } catch (ExpectationFailedException $e) {
  66. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\EventStub] event was dispatched.'));
  67. }
  68. }
  69. public function testAssertNotDispatchedWithClosure()
  70. {
  71. $this->fake->dispatch(new EventStub);
  72. try {
  73. $this->fake->assertNotDispatched(function (EventStub $event) {
  74. return true;
  75. });
  76. $this->fail();
  77. } catch (ExpectationFailedException $e) {
  78. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\EventStub] event was dispatched.'));
  79. }
  80. }
  81. public function testAssertDispatchedWithIgnore()
  82. {
  83. $dispatcher = m::mock(Dispatcher::class);
  84. $dispatcher->shouldReceive('dispatch')->once();
  85. $fake = new EventFake($dispatcher, [
  86. 'Foo',
  87. function ($event, $payload) {
  88. return $event === 'Bar' && $payload['id'] === 1;
  89. },
  90. ]);
  91. $fake->dispatch('Foo');
  92. $fake->dispatch('Bar', ['id' => 1]);
  93. $fake->dispatch('Baz');
  94. $fake->assertDispatched('Foo');
  95. $fake->assertDispatched('Bar');
  96. $fake->assertNotDispatched('Baz');
  97. }
  98. public function testAssertNothingDispatched()
  99. {
  100. $this->fake->assertNothingDispatched();
  101. $this->fake->dispatch(EventStub::class);
  102. $this->fake->dispatch(EventStub::class);
  103. try {
  104. $this->fake->assertNothingDispatched();
  105. $this->fail();
  106. } catch (ExpectationFailedException $e) {
  107. $this->assertThat($e, new ExceptionMessage('2 unexpected events were dispatched.'));
  108. }
  109. }
  110. }
  111. class EventStub
  112. {
  113. //
  114. }