Skip to content

Commit

Permalink
Changing SchemaCache to accept only a connection object
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum committed Aug 5, 2017
1 parent 2c65edb commit aede954
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 40 deletions.
14 changes: 2 additions & 12 deletions src/Database/SchemaCache.php
Expand Up @@ -93,21 +93,11 @@ public function clear($name = null)
/**
* 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, ' .
Expand Down
11 changes: 3 additions & 8 deletions src/Shell/SchemaCacheShell.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Console\Shell;
use Cake\Database\SchemaCache;
use Cake\Datasource\ConnectionManager;
use RuntimeException;

/**
Expand All @@ -30,13 +31,6 @@
class SchemaCacheShell extends Shell
{

/**
* Schema Cache
*
* @var \Cake\Database\SchemaCache
*/
protected $_schemaCache;

/**
* Build metadata.
*
Expand Down Expand Up @@ -85,7 +79,8 @@ public function clear($name = null)
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());
}
Expand Down
38 changes: 18 additions & 20 deletions tests/TestCase/Database/SchemaCacheTest.php
Expand Up @@ -84,7 +84,7 @@ public function testClearEnablesMetadataCache()
$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());
Expand All @@ -100,7 +100,7 @@ public function testBuildEnablesMetadataCache()
$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());
Expand All @@ -113,11 +113,12 @@ public function testBuildEnablesMetadataCache()
*/
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();
}

Expand All @@ -128,13 +129,15 @@ public function testBuildNoArgs()
*/
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');
}

Expand All @@ -145,6 +148,8 @@ public function testBuildNamedModel()
*/
public function testBuildOverwritesExistingData()
{
$ds = ConnectionManager::get('test');

$this->cache->expects($this->once())
->method('write')
->with('test_articles');
Expand All @@ -153,33 +158,24 @@ public function testBuildOverwritesExistingData()
$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();
}

Expand All @@ -190,13 +186,15 @@ public function testClearNoArgs()
*/
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');
}

Expand All @@ -218,8 +216,8 @@ public function testGetSchemaWithConnectionInstance()
/**
* 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()
Expand Down

0 comments on commit aede954

Please sign in to comment.