Framework: [5.2] Using Cache in unitTest doesn't actually set properly

Created on 17 Mar 2016  路  13Comments  路  Source: laravel/framework

I am trying to use the Cache in a unitTest, however whenever I go to actually grab the value again from the cache, my test shows that the key I tried to set doesn't actually exist and returns null. My test configuration has the test driver set to array.

public function testCache()
{
    Cache::put('test', true);
    $this->assertTrue(Cache::get('test'));
}
needs more info

Most helpful comment

Could this be caused by phpunit.xml? It contains <env name="CACHE_DRIVER" value="array"/>. Also, the $minutes = null in getMinutes looks like a bug at first glance.

Edit: I just tried and @sisve's solution definitely fixes the problem.

All 13 comments

Likely the framework hasn't booted. Please make sure your test case extends ours rather than phpunit's default if you want to use facades.

ok...is there a way to make sure, because just to double check myself I did the following straight out of the documentation:

php artisan make:test CacheTest

and then in my tests/CacheTest.php, the only test I am running is the above. Here's the entire file:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Cache;

class CacheTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        Cache::put('test', true);
        $this->assertTrue(Cache::get('test'));
    }
}

Here's output of phpunit tests/CacheTest:

phpunit tests/CacheTest
PHPUnit 4.8.22 by Sebastian Bergmann and contributors.

F

Time: 3.4 seconds, Memory: 19.00Mb

There was 1 failure:

1) CacheTest::testExample
Failed asserting that null is true.

.../tests/CacheTest.php:18

Very odd. Hmm.

Can you call Cache::getDefaultDriver() please?

and dump the output, or something

From php artisan tinker:

Cache::getDefaultDriver()
=> "array"

From php artisan tinker:

No, from your test case please.

Cache::put('test', true);
var_dump(Cache::getDefaultDriver());
$this->assertTrue(Cache::get('test'));

yields: Failed test and string(5) "array" from the var_dump

Cache::put has a nullable $minutes parameter ... but null isn't a valid value. Passing $minutes = null means
getMinutes return null, and the value isn't stored. Try passing any integer value as a third parameter.

put: https://github.com/laravel/framework/blob/5.2/src/Illuminate/Cache/Repository.php#L197
getMinutes: https://github.com/laravel/framework/blob/5.2/src/Illuminate/Cache/Repository.php#L463

Could this be caused by phpunit.xml? It contains <env name="CACHE_DRIVER" value="array"/>. Also, the $minutes = null in getMinutes looks like a bug at first glance.

Edit: I just tried and @sisve's solution definitely fixes the problem.

I was having the exact same problem(and nearly pulling my hair out) and setting the minutes in the call to Cache::put solves it. Thanks @sisve.
This definitely looks like a bug.

+1

Yeah not sure why this was closed. Passing a integer fixes this when running in tests. Definitely a bug.

Was this page helpful?
0 / 5 - 0 ratings