Skip to content

Commit 9a24084

Browse files
committed
Fixing tests for SchemaCache to work with php 7.x and 5.6
1 parent 24c85fb commit 9a24084

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/Database/SchemaCache.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,7 @@ public function clear($name = null)
9999
public function getSchema(ConnectionInterface $connection)
100100
{
101101
if (!method_exists($connection, 'schemaCollection')) {
102-
throw new RuntimeException(sprintf(
103-
'The "%s" connection is not compatible with schema caching, ' .
104-
'as it does not implement a "schemaCollection()" method.',
105-
$connection->configName()
106-
));
102+
throw new RuntimeException('The given connection object is not compatible with schema caching, as it does not implement a "schemaCollection()" method.');
107103
}
108104

109105
$config = $connection->config();

tests/TestCase/Database/SchemaCacheTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Cake\Database\SchemaCache;
2020
use Cake\Database\Schema\CachedCollection;
2121
use Cake\Datasource\ConnectionManager;
22+
use Cake\ORM\Entity;
2223
use Cake\TestSuite\TestCase;
2324

2425
/**
@@ -217,11 +218,35 @@ public function testGetSchemaWithConnectionInstance()
217218
* Test passing invalid object
218219
*
219220
* @expectedException \TypeError
220-
* @expectedExceptionMessage Cake\Database\SchemaCache::getSchema() must implement interface Cake\Datasource\ConnectionInterface
221221
* @return void
222222
*/
223-
public function testPassingInvalidObject()
223+
public function testPassingInvalidObjectPhp7()
224224
{
225-
new SchemaCache(new \SimpleXMLElement(''));
225+
$this->skipIf(phpversion() < 7.0);
226+
new SchemaCache(new Entity());
227+
}
228+
229+
/**
230+
* Test passing invalid object
231+
*
232+
* @return void
233+
*/
234+
public function testPassingInvalidObjectPhp56()
235+
{
236+
$this->skipIf(phpversion() > 7.0);
237+
238+
$capture = function ($errno, $msg) {
239+
restore_error_handler();
240+
$this->assertEquals(E_RECOVERABLE_ERROR, $errno);
241+
$this->assertContains('Argument 1 passed to Cake\Database\SchemaCache::getSchema() must implement interface Cake\Datasource\ConnectionInterface, instance of Cake\ORM\Entity given', $msg);
242+
};
243+
set_error_handler($capture);
244+
245+
try {
246+
new SchemaCache(new Entity());
247+
$this->fail('Exception not thrown');
248+
} catch (\RuntimeException $e) {
249+
$this->assertEquals('The given connection object is not compatible with schema caching, as it does not implement a "schemaCollection()" method.', $e->getMessage());
250+
}
226251
}
227252
}

0 commit comments

Comments
 (0)