Fix interacting with cache in tests

This commit is contained in:
printempw 2018-07-27 14:26:32 +08:00
parent 3c8f0c9e22
commit bcd4b059d5
4 changed files with 119 additions and 45 deletions

View File

@ -74,7 +74,7 @@ class AuthController extends Controller
->withCookie('token', $user->getToken(), $time);
} else {
// Increase the counter
Cache::put($loginFailsCacheKey, ++$loginFails);
Cache::put($loginFailsCacheKey, ++$loginFails, 60);
return json(trans('auth.validation.password'), 1, [
'login_fails' => $loginFails

View File

@ -89,7 +89,7 @@ class AuthControllerTest extends TestCase
'msg' => trans('auth.validation.password'),
'login_fails' => 1
]
); // Unable to assert cache content since array driver has unexpected behaviors
)->assertCacheHas($loginFailsCacheKey);
$this->flushCache();
$this->flushSession();

View File

@ -0,0 +1,115 @@
<?php
use PHPUnit_Framework_Assert as PHPUnit;
/**
* Add ability to interact with cache in tests.
*
* @see Illuminate\Foundation\Testing\Concerns\InteractsWithSession
*/
trait InteractsWithCache
{
/**
* Set the cache to the given array.
*
* @param array $data
* @return $this
*/
public function withCache(array $data, $minutes = 60)
{
$this->cache($data, $minutes);
return $this;
}
/**
* Set the cache to the given array.
*
* @param array $data
* @return void
*/
public function cache(array $data, $minutes = 60)
{
foreach ($data as $key => $value) {
$this->app['cache']->put($key, $value, $minutes);
}
}
/**
* Flush all of the current cache data.
*
* @return void
*/
public function flushCache()
{
$this->app['cache']->flush();
}
/**
* Assert that the cache has a given value.
*
* @param string|array $key
* @param mixed $value
* @return void
*/
public function seeInCache($key, $value = null)
{
$this->assertCacheHas($key, $value);
return $this;
}
/**
* Assert that the cache has a given value.
*
* @param string|array $key
* @param mixed $value
* @return void
*/
public function assertCacheHas($key, $value = null)
{
if (is_array($key)) {
return $this->assertCacheHasAll($key);
}
if (is_null($value)) {
PHPUnit::assertTrue($this->app['cache.store']->has($key), "Cache missing key: $key");
} else {
PHPUnit::assertEquals($value, $this->app['cache.store']->get($key));
}
}
/**
* Assert that the cache has a given list of values.
*
* @param array $bindings
* @return void
*/
public function assertCacheHasAll(array $bindings)
{
foreach ($bindings as $key => $value) {
if (is_int($key)) {
$this->assertCacheHas($value);
} else {
$this->assertCacheHas($key, $value);
}
}
}
/**
* Assert that the cache does not have a given key.
*
* @param string|array $key
* @return void
*/
public function assertCacheMissing($key)
{
if (is_array($key)) {
foreach ($key as $k) {
$this->assertCacheMissing($k);
}
} else {
PHPUnit::assertFalse($this->app['cache.store']->has($key), "Cache has unexpected key: $key");
}
}
}

View File

@ -2,6 +2,8 @@
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
use InteractsWithCache;
/**
* The base URL to use while testing the application.
*
@ -43,49 +45,6 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
return $this->withSession(['uid' => $role->uid, 'token' => $role->getToken()]);
}
/**
* Set the cache to the given array.
*
* @param array $data
* @param int $minutes
* @return $this
*/
public function withCache(array $data, $minutes = 60)
{
foreach ($data as $key => $value) {
$this->app['cache.store']->put($key, $value, $minutes);
}
return $this;
}
/**
* Assert that the cache does not have a given key.
*
* @param string|array $key
* @return void
*/
public function assertCacheMissing($key)
{
if (is_array($key)) {
foreach ($key as $k) {
$this->assertCacheMissing($k);
}
} else {
PHPUnit_Framework_Assert::assertFalse($this->app['cache.store']->has($key), "Cache has unexpected key: $key");
}
}
/**
* Flush all of the current cache data.
*
* @return void
*/
public function flushCache()
{
$this->app['cache.store']->flush();
}
protected function tearDown()
{
$this->beforeApplicationDestroyed(function () {