Browse Source

Changing SchemaCache to accept only a connection object

Florian Krämer 8 years ago
parent
commit
aede954d52

+ 2 - 12
src/Database/SchemaCache.php

@@ -93,21 +93,11 @@ class SchemaCache
     /**
      * Helper method to get the schema collection.
      *
-     * @param string|\Cake\Datasource\ConnectionInterface $connection Connection name to get the schema for or a connection instance
+     * @param \Cake\Datasource\ConnectionInterface $connection Connection object
      * @return \Cake\Database\Schema\Collection|\Cake\Database\Schema\CachedCollection
      */
-    public function getSchema($connection)
+    public function getSchema(ConnectionInterface $connection)
     {
-        if (is_string($connection)) {
-            $connection = ConnectionManager::get($connection);
-        } elseif (!$connection instanceof ConnectionInterface) {
-            throw new InvalidArgumentException(sprintf(
-                'SchemaCache::getSchema() expects `%s`, `%s` given.',
-                ConnectionInterface::class,
-                is_object($connection) ? get_class($connection) : gettype($connection)
-            ));
-        }
-
         if (!method_exists($connection, 'schemaCollection')) {
             throw new RuntimeException(sprintf(
                 'The "%s" connection is not compatible with schema caching, ' .

+ 3 - 8
src/Shell/SchemaCacheShell.php

@@ -16,6 +16,7 @@ namespace Cake\Shell;
 
 use Cake\Console\Shell;
 use Cake\Database\SchemaCache;
+use Cake\Datasource\ConnectionManager;
 use RuntimeException;
 
 /**
@@ -31,13 +32,6 @@ class SchemaCacheShell extends Shell
 {
 
     /**
-     * Schema Cache
-     *
-     * @var \Cake\Database\SchemaCache
-     */
-    protected $_schemaCache;
-
-    /**
      * Build metadata.
      *
      * @param string|null $name The name of the table to build cache data for.
@@ -85,7 +79,8 @@ class SchemaCacheShell extends Shell
     protected function _getSchemaCache()
     {
         try {
-            return new SchemaCache($this->params['connection']);
+            $connection = ConnectionManager::get($this->params['connection']);
+            return new SchemaCache($connection);
         } catch (RuntimeException $e) {
             $this->abort($e->getMessage());
         }

+ 18 - 20
tests/TestCase/Database/SchemaCacheTest.php

@@ -84,7 +84,7 @@ class SchemaCacheTest extends TestCase
         $ds = ConnectionManager::get('test');
         $ds->cacheMetadata(false);
 
-        $ormCache = new SchemaCache('test');
+        $ormCache = new SchemaCache($ds);
         $ormCache->clear();
 
         $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->schemaCollection());
@@ -100,7 +100,7 @@ class SchemaCacheTest extends TestCase
         $ds = ConnectionManager::get('test');
         $ds->cacheMetadata(false);
 
-        $ormCache = new SchemaCache('test');
+        $ormCache = new SchemaCache($ds);
         $ormCache->build();
 
         $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->schemaCollection());
@@ -113,11 +113,12 @@ class SchemaCacheTest extends TestCase
      */
     public function testBuildNoArgs()
     {
+        $ds = ConnectionManager::get('test');
         $this->cache->expects($this->at(3))
             ->method('write')
             ->with('test_articles');
 
-        $ormCache = new SchemaCache('test');
+        $ormCache = new SchemaCache($ds);
         $ormCache->build();
     }
 
@@ -128,13 +129,15 @@ class SchemaCacheTest extends TestCase
      */
     public function testBuildNamedModel()
     {
+        $ds = ConnectionManager::get('test');
+
         $this->cache->expects($this->once())
             ->method('write')
             ->with('test_articles');
         $this->cache->expects($this->never())
             ->method('delete');
 
-        $ormCache = new SchemaCache('test');
+        $ormCache = new SchemaCache($ds);
         $ormCache->build('articles');
     }
 
@@ -145,6 +148,8 @@ class SchemaCacheTest extends TestCase
      */
     public function testBuildOverwritesExistingData()
     {
+        $ds = ConnectionManager::get('test');
+
         $this->cache->expects($this->once())
             ->method('write')
             ->with('test_articles');
@@ -153,33 +158,24 @@ class SchemaCacheTest extends TestCase
         $this->cache->expects($this->never())
             ->method('delete');
 
-        $ormCache = new SchemaCache('test');
+        $ormCache = new SchemaCache($ds);
         $ormCache->build('articles');
     }
 
     /**
-     * Test getting an instance with an invalid connection name.
-     *
-     * @expectedException \Cake\Datasource\Exception\MissingDatasourceConfigException
-     * @return void
-     */
-    public function testInvalidConnection()
-    {
-        new SchemaCache('invalid-connection');
-    }
-
-    /**
      * Test clear() with no args.
      *
      * @return void
      */
     public function testClearNoArgs()
     {
+        $ds = ConnectionManager::get('test');
+
         $this->cache->expects($this->at(3))
             ->method('delete')
             ->with('test_articles');
 
-        $ormCache = new SchemaCache('test');
+        $ormCache = new SchemaCache($ds);
         $ormCache->clear();
     }
 
@@ -190,13 +186,15 @@ class SchemaCacheTest extends TestCase
      */
     public function testClearNamedModel()
     {
+        $ds = ConnectionManager::get('test');
+
         $this->cache->expects($this->never())
             ->method('write');
         $this->cache->expects($this->once())
             ->method('delete')
             ->with('test_articles');
 
-        $ormCache = new SchemaCache('test');
+        $ormCache = new SchemaCache($ds);
         $ormCache->clear('articles');
     }
 
@@ -218,8 +216,8 @@ class SchemaCacheTest extends TestCase
     /**
      * Test passing invalid object
      *
-     * @expectedException \InvalidArgumentException
-     * @expectedExceptionMessage SchemaCache::getSchema() expects `Cake\Datasource\ConnectionInterface`, `stdClass` given.
+     * @expectedException \TypeError
+     * @expectedExceptionMessage Cake\Database\SchemaCache::getSchema() must implement interface Cake\Datasource\ConnectionInterface
      * @return void
      */
     public function testPassingInvalidObject()