ソースを参照

Fix up tests that mock cache.

Mark Story 7 年 前
コミット
de9276ea28

+ 0 - 5
src/Cache/Cache.php

@@ -449,11 +449,6 @@ class Cache
      */
     public static function clear(string $config = 'default'): bool
     {
-        if (func_num_args() > 1) {
-            deprecationWarning('The $check parameter has been removed from clear(). Update your usage.');
-            $config = func_get_arg(1);
-        }
-
         return static::pool($config)->clear();
     }
 

+ 1 - 1
src/Database/Schema/CachedCollection.php

@@ -55,7 +55,7 @@ class CachedCollection extends Collection
 
         if (!empty($cacheConfig) && !$options['forceRefresh']) {
             $cached = Cache::read($cacheKey, $cacheConfig);
-            if ($cached !== false) {
+            if ($cached !== null) {
                 return $cached;
             }
         }

+ 2 - 2
src/Shell/CacheShell.php

@@ -18,9 +18,9 @@ namespace Cake\Shell;
 use Cake\Cache\Cache;
 use Cake\Cache\Engine\ApcuEngine;
 use Cake\Cache\Engine\WincacheEngine;
+use Cake\Cache\InvalidArgumentException;
 use Cake\Console\ConsoleOptionParser;
 use Cake\Console\Shell;
-use InvalidArgumentException;
 
 /**
  * Cache Shell.
@@ -76,7 +76,7 @@ class CacheShell extends Shell
     {
         try {
             $engine = Cache::engine($prefix);
-            Cache::clear(false, $prefix);
+            Cache::clear($prefix);
             if ($engine instanceof ApcuEngine) {
                 $this->warn("ApcuEngine detected: Cleared $prefix CLI cache successfully " .
                 "but $prefix web cache must be cleared separately.");

+ 10 - 7
tests/TestCase/Database/SchemaCacheTest.php

@@ -114,10 +114,10 @@ class SchemaCacheTest extends TestCase
     public function testBuildNoArgs()
     {
         $ds = ConnectionManager::get('test');
-        $this->cache->method('write')
+        $this->cache->method('set')
             ->will($this->returnValue(true));
         $this->cache->expects($this->at(3))
-            ->method('write')
+            ->method('set')
             ->with('test_articles')
             ->will($this->returnValue(true));
 
@@ -135,7 +135,7 @@ class SchemaCacheTest extends TestCase
         $ds = ConnectionManager::get('test');
 
         $this->cache->expects($this->once())
-            ->method('write')
+            ->method('set')
             ->with('test_articles')
             ->will($this->returnValue(true));
         $this->cache->expects($this->never())
@@ -155,11 +155,11 @@ class SchemaCacheTest extends TestCase
         $ds = ConnectionManager::get('test');
 
         $this->cache->expects($this->once())
-            ->method('write')
+            ->method('set')
             ->with('test_articles')
             ->will($this->returnValue(true));
         $this->cache->expects($this->never())
-            ->method('read');
+            ->method('get');
         $this->cache->expects($this->never())
             ->method('delete');
 
@@ -175,6 +175,8 @@ class SchemaCacheTest extends TestCase
     public function testClearNoArgs()
     {
         $ds = ConnectionManager::get('test');
+        $this->cache->method('delete')
+            ->will($this->returnValue(true));
 
         $this->cache->expects($this->at(3))
             ->method('delete')
@@ -194,10 +196,11 @@ class SchemaCacheTest extends TestCase
         $ds = ConnectionManager::get('test');
 
         $this->cache->expects($this->never())
-            ->method('write');
+            ->method('set');
         $this->cache->expects($this->once())
             ->method('delete')
-            ->with('test_articles');
+            ->with('test_articles')
+            ->will($this->returnValue(true));
 
         $ormCache = new SchemaCache($ds);
         $ormCache->clear('articles');

+ 1 - 1
tests/TestCase/Datasource/QueryCacherTest.php

@@ -139,7 +139,7 @@ class QueryCacherTest extends TestCase
     protected function _mockRead($key, $value = false)
     {
         $this->engine->expects($this->any())
-            ->method('read')
+            ->method('get')
             ->with($key)
             ->will($this->returnValue($value));
     }

+ 1 - 1
tests/TestCase/Http/Session/CacheSessionTest.php

@@ -95,7 +95,7 @@ class CacheSessionTest extends TestCase
         $this->storage->write('test_one', 'Some other value');
         $this->assertTrue($this->storage->destroy('test_one'), 'Value was not deleted.');
 
-        $this->assertFalse(Cache::read('test_one', 'session_test'), 'Value stuck around.');
+        $this->assertNull(Cache::read('test_one', 'session_test'), 'Value stuck around.');
     }
 
     /**

+ 2 - 2
tests/TestCase/ORM/QueryTest.php

@@ -1945,7 +1945,7 @@ class QueryTest extends TestCase
 
         $cacher = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
         $cacher->expects($this->once())
-            ->method('read')
+            ->method('get')
             ->with('my_key')
             ->will($this->returnValue($resultSet));
 
@@ -1970,7 +1970,7 @@ class QueryTest extends TestCase
 
         $cacher = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
         $cacher->expects($this->once())
-            ->method('write')
+            ->method('set')
             ->with(
                 'my_key',
                 $this->isInstanceOf('Cake\Datasource\ResultSetInterface')

+ 3 - 2
tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php

@@ -16,6 +16,7 @@ declare(strict_types=1);
 namespace Cake\Test\TestCase\Routing\Middleware;
 
 use Cake\Cache\Cache;
+use Cake\Cache\InvalidArgumentException as CacheInvalidArgumentException;
 use Cake\Routing\Middleware\RoutingMiddleware;
 use Cake\Routing\RouteBuilder;
 use Cake\Routing\RouteCollection;
@@ -512,7 +513,7 @@ class RoutingMiddlewareTest extends TestCase
         $response = new Response();
         $next = function ($req, $res) use ($cacheConfigName) {
             $routeCollection = Cache::read('routeCollection', $cacheConfigName);
-            $this->assertFalse($routeCollection);
+            $this->assertNull($routeCollection);
 
             return $res;
         };
@@ -532,7 +533,7 @@ class RoutingMiddlewareTest extends TestCase
      */
     public function testCacheConfigNotFound()
     {
-        $this->expectException(\InvalidArgumentException::class);
+        $this->expectException(CacheInvalidArgumentException::class);
         $this->expectExceptionMessage('The "notfound" cache configuration does not exist.');
 
         Cache::setConfig('_cake_router_', [

+ 3 - 3
tests/TestCase/Shell/CacheShellTest.php

@@ -83,7 +83,7 @@ class CacheShellTest extends ConsoleIntegrationTestCase
         $this->exec('cache clear test');
 
         $this->assertExitCode(Shell::CODE_SUCCESS);
-        $this->assertFalse(Cache::read('key', 'test'));
+        $this->assertNull(Cache::read('key', 'test'));
     }
 
     /**
@@ -112,7 +112,7 @@ class CacheShellTest extends ConsoleIntegrationTestCase
         $this->exec('cache clear_all');
 
         $this->assertExitCode(Shell::CODE_SUCCESS);
-        $this->assertFalse(Cache::read('key', 'test'));
-        $this->assertFalse(Cache::read('key', '_cake_core_'));
+        $this->assertNull(Cache::read('key', 'test'));
+        $this->assertNull(Cache::read('key', '_cake_core_'));
     }
 }

+ 15 - 11
tests/TestCase/Shell/SchemaCacheShellTest.php

@@ -106,10 +106,10 @@ class SchemaCacheShellTest extends TestCase
     public function testBuildNoArgs()
     {
         $this->cache->expects($this->any())
-            ->method('write')
+            ->method('set')
             ->will($this->returnValue(true));
         $this->cache->expects($this->at(3))
-            ->method('write')
+            ->method('set')
             ->with('test_articles')
             ->will($this->returnValue(true));
 
@@ -125,11 +125,12 @@ class SchemaCacheShellTest extends TestCase
     public function testBuildNamedModel()
     {
         $this->cache->expects($this->once())
-            ->method('write')
+            ->method('set')
             ->with('test_articles')
             ->will($this->returnValue(true));
         $this->cache->expects($this->never())
-            ->method('delete');
+            ->method('delete')
+            ->will($this->returnValue(false));
 
         $this->shell->params['connection'] = 'test';
         $this->shell->build('articles');
@@ -143,13 +144,14 @@ class SchemaCacheShellTest extends TestCase
     public function testBuildOverwritesExistingData()
     {
         $this->cache->expects($this->once())
-            ->method('write')
+            ->method('set')
             ->with('test_articles')
             ->will($this->returnValue(true));
         $this->cache->expects($this->never())
-            ->method('read');
+            ->method('get');
         $this->cache->expects($this->never())
-            ->method('delete');
+            ->method('delete')
+            ->will($this->returnValue(false));
 
         $this->shell->params['connection'] = 'test';
         $this->shell->build('articles');
@@ -186,10 +188,11 @@ class SchemaCacheShellTest extends TestCase
      */
     public function testClearNoArgs()
     {
+        $this->cache->method('delete')
+            ->will($this->returnValue(true));
         $this->cache->expects($this->at(3))
             ->method('delete')
-            ->with('test_articles')
-            ->will($this->returnValue(true));
+            ->with('test_articles');
 
         $this->shell->params['connection'] = 'test';
         $this->shell->clear();
@@ -203,11 +206,12 @@ class SchemaCacheShellTest extends TestCase
     public function testClearNamedModel()
     {
         $this->cache->expects($this->never())
-            ->method('write')
+            ->method('set')
             ->will($this->returnValue(true));
         $this->cache->expects($this->once())
             ->method('delete')
-            ->with('test_articles');
+            ->with('test_articles')
+            ->will($this->returnValue(false));
 
         $this->shell->params['connection'] = 'test';
         $this->shell->clear('articles');

+ 11 - 11
tests/TestCase/View/CellTest.php

@@ -403,10 +403,10 @@ class CellTest extends TestCase
         $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
         $mock->method('init')
             ->will($this->returnValue(true));
-        $mock->method('read')
-            ->will($this->returnValue(false));
+        $mock->method('get')
+            ->will($this->returnValue(null));
         $mock->expects($this->once())
-            ->method('write')
+            ->method('set')
             ->with('cell_test_app_view_cell_articles_cell_display_default', "dummy\n")
             ->will($this->returnValue(true));
         Cache::setConfig('default', $mock);
@@ -427,10 +427,10 @@ class CellTest extends TestCase
         $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
         $mock->method('init')
             ->will($this->returnValue(true));
-        $mock->method('read')
+        $mock->method('get')
             ->will($this->returnValue("dummy\n"));
         $mock->expects($this->never())
-            ->method('write');
+            ->method('set');
         Cache::setConfig('default', $mock);
 
         $cell = $this->View->cell('Articles', [], ['cache' => true]);
@@ -449,10 +449,10 @@ class CellTest extends TestCase
         $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
         $mock->method('init')
             ->will($this->returnValue(true));
-        $mock->method('read')
-            ->will($this->returnValue(false));
+        $mock->method('get')
+            ->will($this->returnValue(null));
         $mock->expects($this->once())
-            ->method('write')
+            ->method('set')
             ->with('my_key', "dummy\n")
             ->will($this->returnValue(true));
         Cache::setConfig('cell', $mock);
@@ -475,10 +475,10 @@ class CellTest extends TestCase
         $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
         $mock->method('init')
             ->will($this->returnValue(true));
-        $mock->method('read')
-            ->will($this->returnValue(false));
+        $mock->method('get')
+            ->will($this->returnValue(null));
         $mock->expects($this->once())
-            ->method('write')
+            ->method('set')
             ->with('cell_test_app_view_cell_articles_cell_customTemplateViewBuilder_default', "<h1>This is the alternate template</h1>\n")
             ->will($this->returnValue(true));
         Cache::setConfig('default', $mock);