SupportFacadesEventTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Illuminate\Cache\CacheManager;
  4. use Illuminate\Cache\Events\CacheMissed;
  5. use Illuminate\Config\Repository as ConfigRepository;
  6. use Illuminate\Container\Container;
  7. use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Events\Dispatcher;
  10. use Illuminate\Support\Facades\Cache;
  11. use Illuminate\Support\Facades\Event;
  12. use Illuminate\Support\Facades\Facade;
  13. use Illuminate\Support\Testing\Fakes\EventFake;
  14. use Mockery as m;
  15. use PHPUnit\Framework\TestCase;
  16. class SupportFacadesEventTest extends TestCase
  17. {
  18. private $events;
  19. protected function setUp(): void
  20. {
  21. parent::setUp();
  22. $this->events = m::mock(Dispatcher::class);
  23. $container = new Container;
  24. $container->instance('events', $this->events);
  25. $container->alias('events', DispatcherContract::class);
  26. $container->instance('cache', new CacheManager($container));
  27. $container->instance('config', new ConfigRepository($this->getCacheConfig()));
  28. Facade::setFacadeApplication($container);
  29. }
  30. protected function tearDown(): void
  31. {
  32. Event::clearResolvedInstances();
  33. Event::setFacadeApplication(null);
  34. m::close();
  35. }
  36. public function testFakeFor()
  37. {
  38. Event::fakeFor(function () {
  39. (new FakeForStub)->dispatch();
  40. Event::assertDispatched(EventStub::class);
  41. });
  42. $this->events->shouldReceive('dispatch')->once();
  43. (new FakeForStub)->dispatch();
  44. }
  45. public function testFakeForSwapsDispatchers()
  46. {
  47. $arrayRepository = Cache::store('array');
  48. Event::fakeFor(function () use ($arrayRepository) {
  49. $this->assertInstanceOf(EventFake::class, Event::getFacadeRoot());
  50. $this->assertInstanceOf(EventFake::class, Model::getEventDispatcher());
  51. $this->assertInstanceOf(EventFake::class, $arrayRepository->getEventDispatcher());
  52. });
  53. $this->assertSame($this->events, Event::getFacadeRoot());
  54. $this->assertSame($this->events, Model::getEventDispatcher());
  55. $this->assertSame($this->events, $arrayRepository->getEventDispatcher());
  56. }
  57. public function testFakeSwapsDispatchersInResolvedCacheRepositories()
  58. {
  59. $arrayRepository = Cache::store('array');
  60. $this->events->shouldReceive('dispatch')->once();
  61. $arrayRepository->get('foo');
  62. Event::fake();
  63. $arrayRepository->get('bar');
  64. Event::assertDispatched(CacheMissed::class);
  65. }
  66. protected function getCacheConfig()
  67. {
  68. return [
  69. 'cache' => [
  70. 'stores' => [
  71. 'array' => [
  72. 'driver' => 'array',
  73. ],
  74. ],
  75. ],
  76. ];
  77. }
  78. }
  79. class FakeForStub
  80. {
  81. public function dispatch()
  82. {
  83. Event::dispatch(EventStub::class);
  84. }
  85. }