Browse Source

Adding a test for OrmCache

Florian Krämer 8 years ago
parent
commit
a38d3b7770
2 changed files with 23 additions and 16 deletions
  1. 11 16
      src/ORM/OrmCache.php
  2. 12 0
      tests/TestCase/ORM/OrmCacheTest.php

+ 11 - 16
src/ORM/OrmCache.php

@@ -17,6 +17,7 @@ namespace Cake\ORM;
 use Cake\Cache\Cache;
 use Cake\Datasource\ConnectionInterface;
 use Cake\Datasource\ConnectionManager;
+use InvalidArgumentException;
 use RuntimeException;
 
 /**
@@ -98,34 +99,28 @@ class OrmCache
     public function getSchema($connection)
     {
         if (is_string($connection)) {
-            /* @var \Cake\Database\Connection $source */
-            $source = ConnectionManager::get($connection);
+            $connection = ConnectionManager::get($connection);
         } elseif (!$connection instanceof ConnectionInterface) {
-            throw new RuntimeException(sprtinf(
-                'OrmCache::getSchema() expects `%s`, `%s` given.',
+            throw new InvalidArgumentException(sprintf(
+                'SchemaCache::getSchema() expects `%s`, `%s` given.',
                 ConnectionInterface::class,
                 is_object($connection) ? get_class($connection) : gettype($connection)
             ));
         }
 
-        if ($connection instanceof ConnectionInterface) {
-            $source = $connection;
-            $connection = $source->configName();
-        }
-
-        if (!method_exists($source, 'schemaCollection')) {
+        if (!method_exists($connection, 'schemaCollection')) {
             throw new RuntimeException(sprintf(
-                'The "%s" connection is not compatible with schema ' .
-                'caching, as it does not implement a "schemaCollection()" method.',
-                $connection
+                'The "%s" connection is not compatible with schema caching, ' .
+                'as it does not implement a "schemaCollection()" method.',
+                $connection->configName()
             ));
         }
 
-        $config = $source->config();
+        $config = $connection->config();
         if (empty($config['cacheMetadata'])) {
-            $source->cacheMetadata(true);
+            $connection->cacheMetadata(true);
         }
 
-        return $source->getSchemaCollection();
+        return $connection->getSchemaCollection();
     }
 }

+ 12 - 0
tests/TestCase/ORM/OrmCacheTest.php

@@ -214,4 +214,16 @@ class OrmCacheTest extends TestCase
 
         $this->assertInstanceOf(CachedCollection::class, $result);
     }
+
+    /**
+     * Test passing invalid object
+     *
+     * @expectedException \InvalidArgumentException
+     * @expectedExceptionMessage SchemaCache::getSchema() expects `Cake\Datasource\ConnectionInterface`, `stdClass` given.
+     * @return void
+     */
+    public function testPassingInvalidObject()
+    {
+        new OrmCache(new \stdClass());
+    }
 }