Mock Laravel Cache remember for ever

Sometimes you want to make sure certain things are cached properly. With mocking this is possible. In this example I will show how to test Cache::rememberForever and see the results.


    $cacheRepository = Cache::driver();
    $cacheRepositorySpy = Mockery::spy($cacheRepository);

    Cache::swap($cacheRepositorySpy);

    $this->get('/call-to-cached-endpoint')

    $cacheRepositorySpy->shouldHaveReceived('rememberForever')
        ->with('users', \Closure::class);

    // It's also possible to get back what was put in the cache
    $store = $cacheRepositorySpy->getStore();

    // ...and write assertsions against this
    $this->assertEquals('[email protected]', $store->get('users')->first()->value);