Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Illuminate/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Mockery;
use Mockery\LegacyMockInterface;

/**
* @mixin \Illuminate\Cache\Repository
Expand Down Expand Up @@ -81,9 +83,17 @@ public function memo($driver = null)
{
$driver = $driver ?: $this->getDefaultDriver();

$this->app->scopedIf($bindingKey = "cache.__memoized:{$driver}", fn () => $this->repository(
new MemoizedStore($driver, $this->store($driver)), ['events' => false]
));
$bindingKey = "cache.__memoized:{$driver}";

$isSpy = isset($this->app['cache']) && $this->app['cache'] instanceof LegacyMockInterface;

$this->app->scopedIf($bindingKey, function () use ($driver, $isSpy) {
$repository = $this->repository(
new MemoizedStore($driver, $this->store($driver)), ['events' => false]
);

return $isSpy ? Mockery::spy($repository) : $repository;
});

return $this->app->make($bindingKey);
}
Expand Down
25 changes: 25 additions & 0 deletions src/Illuminate/Support/Facades/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Support\Facades;

use Mockery;

/**
* @method static \Illuminate\Contracts\Cache\Repository store(string|null $name = null)
* @method static \Illuminate\Contracts\Cache\Repository driver(string|null $driver = null)
Expand Down Expand Up @@ -71,4 +73,27 @@ protected static function getFacadeAccessor()
{
return 'cache';
}

/**
* Convert the facade into a Mockery spy.
*
* @return \Mockery\MockInterface
*/
public static function spy()
{
if (! static::isMock()) {
$class = static::getMockableClass();
$instance = static::getFacadeRoot();

if ($class && $instance) {
return tap(Mockery::spy($instance)->makePartial(), function ($spy) {
static::swap($spy);
});
}

return tap($class ? Mockery::spy($class) : Mockery::spy(), function ($spy) {
static::swap($spy);
});
}
}
}
94 changes: 94 additions & 0 deletions tests/Cache/CacheSpyMemoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Illuminate\Tests\Cache;

use Closure;
use Illuminate\Cache\CacheManager;
use Illuminate\Config\Repository as ConfigRepository;
use Illuminate\Container\Container;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Facade;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery\LegacyMockInterface;
use PHPUnit\Framework\TestCase;

class CacheSpyMemoTest extends TestCase
{
use MockeryPHPUnitIntegration;

protected function setUp(): void
{
parent::setUp();

$container = new Container;

$container->instance('config', new ConfigRepository([
'cache' => [
'default' => 'array',
'stores' => [
'array' => [
'driver' => 'array',
],
],
],
]));

$container->instance('cache', new CacheManager($container));

Facade::setFacadeApplication($container);
}

protected function tearDown(): void
{
Facade::clearResolvedInstances();
Facade::setFacadeApplication(null);

parent::tearDown();
}

public function test_cache_spy_works_with_memoized_cache()
{
$cache = Cache::spy();

Cache::memo()->remember('key', 60, fn() => 'bar');

$cache->shouldHaveReceived('memo')->once();
}

public function test_cache_spy_tracks_remember_on_memoized_cache_as_described_in_issue()
{
$cache = Cache::spy();

$memoizedCache = Cache::memo();
$value = $memoizedCache->remember('key', 60, fn() => 'bar');

$this->assertSame('bar', $value);

$memoizedCache->shouldHaveReceived('remember')->once()->with('key', 60, Mockery::type(Closure::class));
}

public function test_cache_spy_tracks_remember_calls_on_memoized_cache()
{
$cache = Cache::spy();

$memoizedCache = Cache::memo();
$memoizedCache->remember('key', 60, fn() => 'bar');

$memoizedCache->shouldHaveReceived('remember')->once()->with('key', 60, Mockery::type(Closure::class));
}

public function test_cache_spy_memo_returns_spied_repository()
{
$cache = Cache::spy();

$memoizedCache = Cache::memo();

$this->assertInstanceOf(LegacyMockInterface::class, $memoizedCache);

$memoizedCache->remember('key', 60, fn() => 'bar');

$memoizedCache->shouldHaveReceived('remember')->once();
}
}

Loading