Skip to content

Commit

Permalink
Merge branch 'event-fake-replaces-cache-dispatcher' of https://github…
Browse files Browse the repository at this point in the history
….com/colindecarlo/framework into colindecarlo-event-fake-replaces-cache-dispatcher
  • Loading branch information
taylorotwell committed Jan 16, 2020
2 parents bfd17f0 + 381ee1f commit 9831c74
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
29 changes: 23 additions & 6 deletions src/Illuminate/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,32 @@ protected function createDynamodbDriver(array $config)
*/
public function repository(Store $store)
{
$repository = new Repository($store);
return tap(new Repository($store), function ($repository) {
$this->addEventDispatcher($repository);
});
}

if ($this->app->bound(DispatcherContract::class)) {
$repository->setEventDispatcher(
$this->app[DispatcherContract::class]
);
/**
* @param Repository $repository
*/
protected function addEventDispatcher(Repository $repository)
{
if (! $this->app->bound(DispatcherContract::class)) {
return;
}

return $repository;
$repository->setEventDispatcher(
$this->app[DispatcherContract::class]
);
}

/**
* Refreshes the event dispatcher of all resolved repositories
* with the currently bound event dispatcher implementation.
*/
public function refreshEventDispatcher()
{
array_map([$this, 'addEventDispatcher'], $this->stores);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,16 @@ public function setEventDispatcher(Dispatcher $events)
$this->events = $events;
}

/**
* Get the event dispatcher instance.
*
* @return \Illuminate\Contracts\Events\Dispatcher $events
*/
public function getEventDispatcher()
{
return $this->events;
}

/**
* Determine if a cached value exists.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Support/Facades/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function fake($eventsToFake = [])
static::swap($fake = new EventFake(static::getFacadeRoot(), $eventsToFake));

Model::setEventDispatcher($fake);
Cache::refreshEventDispatcher();

return $fake;
}
Expand All @@ -59,6 +60,7 @@ public static function fakeFor(callable $callable, array $eventsToFake = [])
static::swap($originalDispatcher);

Model::setEventDispatcher($originalDispatcher);
Cache::refreshEventDispatcher();
});
}

Expand Down
41 changes: 40 additions & 1 deletion tests/Support/SupportFacadesEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Illuminate\Tests\Support;

use Illuminate\Cache\CacheManager;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Config\Repository as ConfigRepository;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Testing\Fakes\EventFake;
Expand All @@ -23,6 +28,9 @@ protected function setUp(): void

$container = new Container;
$container->instance('events', $this->events);
$container->alias('events', DispatcherContract::class);
$container->instance('cache', new CacheManager($container));
$container->instance('config', new ConfigRepository($this->getCacheConfig()));

Facade::setFacadeApplication($container);
}
Expand All @@ -49,13 +57,44 @@ public function testFakeFor()

public function testFakeForSwapsDispatchers()
{
Event::fakeFor(function () {
$arrayRepository = Cache::store('array');

Event::fakeFor(function () use ($arrayRepository) {
$this->assertInstanceOf(EventFake::class, Event::getFacadeRoot());
$this->assertInstanceOf(EventFake::class, Model::getEventDispatcher());
$this->assertInstanceOf(EventFake::class, $arrayRepository->getEventDispatcher());
});

$this->assertSame($this->events, Event::getFacadeRoot());
$this->assertSame($this->events, Model::getEventDispatcher());
$this->assertSame($this->events, $arrayRepository->getEventDispatcher());
}

public function testFakeSwapsDispatchersInResolvedCacheRepositories()
{
$arrayRepository = Cache::store('array');

$this->events->shouldReceive('dispatch')->once();
$arrayRepository->get('foo');

Event::fake();

$arrayRepository->get('bar');

Event::assertDispatched(CacheMissed::class);
}

protected function getCacheConfig()
{
return [
'cache' => [
'stores' => [
'array' => [
'driver' => 'array',
],
],
],
];
}
}

Expand Down

0 comments on commit 9831c74

Please sign in to comment.