Browse Source

Fixing tests for SchemaCache to work with php 7.x and 5.6

Florian Krämer 8 years ago
parent
commit
9a24084ed4
2 changed files with 29 additions and 8 deletions
  1. 1 5
      src/Database/SchemaCache.php
  2. 28 3
      tests/TestCase/Database/SchemaCacheTest.php

+ 1 - 5
src/Database/SchemaCache.php

@@ -99,11 +99,7 @@ class SchemaCache
     public function getSchema(ConnectionInterface $connection)
     {
         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->configName()
-            ));
+            throw new RuntimeException('The given connection object is not compatible with schema caching, as it does not implement a "schemaCollection()" method.');
         }
 
         $config = $connection->config();

+ 28 - 3
tests/TestCase/Database/SchemaCacheTest.php

@@ -19,6 +19,7 @@ use Cake\Cache\CacheEngine;
 use Cake\Database\SchemaCache;
 use Cake\Database\Schema\CachedCollection;
 use Cake\Datasource\ConnectionManager;
+use Cake\ORM\Entity;
 use Cake\TestSuite\TestCase;
 
 /**
@@ -217,11 +218,35 @@ class SchemaCacheTest extends TestCase
      * Test passing invalid object
      *
      * @expectedException \TypeError
-     * @expectedExceptionMessage Cake\Database\SchemaCache::getSchema() must implement interface Cake\Datasource\ConnectionInterface
      * @return void
      */
-    public function testPassingInvalidObject()
+    public function testPassingInvalidObjectPhp7()
     {
-        new SchemaCache(new \SimpleXMLElement(''));
+        $this->skipIf(phpversion() < 7.0);
+        new SchemaCache(new Entity());
+    }
+
+    /**
+     * Test passing invalid object
+     *
+     * @return void
+     */
+    public function testPassingInvalidObjectPhp56()
+    {
+        $this->skipIf(phpversion() > 7.0);
+
+        $capture = function ($errno, $msg) {
+            restore_error_handler();
+            $this->assertEquals(E_RECOVERABLE_ERROR, $errno);
+            $this->assertContains('Argument 1 passed to Cake\Database\SchemaCache::getSchema() must implement interface Cake\Datasource\ConnectionInterface, instance of Cake\ORM\Entity given', $msg);
+        };
+        set_error_handler($capture);
+
+        try {
+            new SchemaCache(new Entity());
+            $this->fail('Exception not thrown');
+        } catch (\RuntimeException $e) {
+            $this->assertEquals('The given connection object is not compatible with schema caching, as it does not implement a "schemaCollection()" method.', $e->getMessage());
+        }
     }
 }