From bcd4b059d5ab1a22de458b9c5e3abf11cda691bd Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 27 Jul 2018 14:26:32 +0800 Subject: [PATCH] Fix interacting with cache in tests --- app/Http/Controllers/AuthController.php | 2 +- tests/AuthControllerTest.php | 2 +- tests/Concerns/InteractsWithCache.php | 115 ++++++++++++++++++++++++ tests/TestCase.php | 45 +--------- 4 files changed, 119 insertions(+), 45 deletions(-) create mode 100644 tests/Concerns/InteractsWithCache.php diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index fae608cb..b2cc90c0 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -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 diff --git a/tests/AuthControllerTest.php b/tests/AuthControllerTest.php index 38b3f760..35f065ea 100644 --- a/tests/AuthControllerTest.php +++ b/tests/AuthControllerTest.php @@ -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(); diff --git a/tests/Concerns/InteractsWithCache.php b/tests/Concerns/InteractsWithCache.php new file mode 100644 index 00000000..635ee457 --- /dev/null +++ b/tests/Concerns/InteractsWithCache.php @@ -0,0 +1,115 @@ +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"); + } + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 09843394..96c8b882 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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 () {