Skip to content
Permalink
Browse files

Move tests into TableRegistry suite as they belong there.

  • Loading branch information...
markstory committed Oct 19, 2013
1 parent 95e77e5 commit 9a7e621f17377cd42eda78d6927831a77e1428fe
@@ -36,12 +36,13 @@
* ### Callbacks/events
*
* Table objects provide a few callbacks/events you can hook into to augment/replace
* find operations. Each event uses the standard Event subsystem in CakePHP
* find operations. Each event uses the standard event subsystem in CakePHP
*
* - beforeFind($event, $query, $options) - Fired before each find operation. By stopping
* the event and supplying a return value you can bypass the find operation entirely. Any
* changes done to the $query instance will be retained for the rest of the find.
*
* @see Cake\Event\EventManager for reference on the events system.
*/
class Table {
@@ -100,7 +100,6 @@ public static function get($alias, $options = []) {
return static::$_instances[$alias];
}
list($plugin, $baseClass) = pluginSplit($alias);
$options = ['alias' => $baseClass] + $options;
@@ -16,15 +16,42 @@
*/
namespace Cake\Test\TestCase\ORM;
use Cake\Core\Configure;
use Cake\Database\ConnectionManager;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* Used to test correct class is instantiated when using TableRegistry::get();
*/
class MyUsersTable extends Table {
/**
* Overrides default table name
*
* @var string
*/
protected $_table = 'users';
}
/**
* Test case for TableRegistry
*/
class TableRegistryTest extends TestCase {
/**
* setup
*
* @return void
*/
public function setUp() {
parent::setUp();
Configure::write('App.namespace', 'TestApp');
}
/**
* tear down
*
@@ -87,6 +114,74 @@ public function testGetWithConfig() {
$this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
}
/**
* Tests that tables can be instantiated based on conventions
* and using plugin notation
*
* @return void
*/
public function testBuildConvention() {
$table = TableRegistry::get('article');
$this->assertInstanceOf('\TestApp\Model\Repository\ArticleTable', $table);
$table = TableRegistry::get('Article');
$this->assertInstanceOf('\TestApp\Model\Repository\ArticleTable', $table);
$table = TableRegistry::get('author');
$this->assertInstanceOf('\TestApp\Model\Repository\AuthorTable', $table);
$table = TableRegistry::get('Author');
$this->assertInstanceOf('\TestApp\Model\Repository\AuthorTable', $table);
$class = $this->getMockClass('\Cake\ORM\Table');
$class::staticExpects($this->once())
->method('defaultConnectionName')
->will($this->returnValue('test'));
class_alias($class, 'MyPlugin\Model\Repository\SuperTestTable');
$table = TableRegistry::get('MyPlugin.SuperTest');
$this->assertInstanceOf($class, $table);
}
/**
* Tests that table options can be pre-configured for the factory method
*
* @return void
*/
public function testConfigAndBuild() {
TableRegistry::clear();
$map = TableRegistry::config();
$this->assertEquals([], $map);
$connection = ConnectionManager::get('test', false);
$options = ['connection' => $connection];
TableRegistry::config('users', $options);
$map = TableRegistry::config();
$this->assertEquals(['users' => $options], $map);
$this->assertEquals($options, TableRegistry::config('users'));
$schema = ['id' => ['type' => 'rubbish']];
$options += ['schema' => $schema];
TableRegistry::config('users', $options);
$table = TableRegistry::get('users', ['table' => 'users']);
$this->assertInstanceOf('Cake\ORM\Table', $table);
$this->assertEquals('users', $table->table());
$this->assertEquals('users', $table->alias());
$this->assertSame($connection, $table->connection());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
$this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
TableRegistry::clear();
$this->assertEmpty(TableRegistry::config());
TableRegistry::config('users', $options);
$table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
$this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
$this->assertEquals('users', $table->table());
$this->assertEquals('users', $table->alias());
$this->assertSame($connection, $table->connection());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
}
/**
* Test setting an instance.
*
@@ -28,20 +28,6 @@ class UsersTable extends Table {
}
/**
* Used to test correct class is instantiated when using TableRegistry::get();
*/
class MyUsersTable extends Table {
/**
* Overrides default table name
*
* @var string
*/
protected $_table = 'users';
}
/**
* Tests Table class
*
@@ -63,68 +49,6 @@ public function tearDown() {
parent::tearDown();
TableRegistry::clear();
}
/**
* Tests that table options can be pre-configured for the factory method
*
* @return void
*/
public function testConfigAndBuild() {
TableRegistry::clear();
$map = TableRegistry::config();
$this->assertEquals([], $map);
$options = ['connection' => $this->connection];
TableRegistry::config('users', $options);
$map = TableRegistry::config();
$this->assertEquals(['users' => $options], $map);
$this->assertEquals($options, TableRegistry::config('users'));
$schema = ['id' => ['type' => 'rubbish']];
$options += ['schema' => $schema];
TableRegistry::config('users', $options);
$table = TableRegistry::get('users', ['table' => 'users']);
$this->assertInstanceOf('Cake\ORM\Table', $table);
$this->assertEquals('users', $table->table());
$this->assertEquals('users', $table->alias());
$this->assertSame($this->connection, $table->connection());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
$this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
TableRegistry::clear();
$this->assertEmpty(TableRegistry::config());
TableRegistry::config('users', $options);
$table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
$this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
$this->assertEquals('users', $table->table());
$this->assertEquals('users', $table->alias());
$this->assertSame($this->connection, $table->connection());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
}
/**
* Tests that tables can be instantiated based on conventions
* and using plugin notation
*
* @return void
*/
public function testBuildConvention() {
$table = TableRegistry::get('article');
$this->assertInstanceOf('\TestApp\Model\Repository\ArticleTable', $table);
$table = TableRegistry::get('Article');
$this->assertInstanceOf('\TestApp\Model\Repository\ArticleTable', $table);
$table = TableRegistry::get('author');
$this->assertInstanceOf('\TestApp\Model\Repository\AuthorTable', $table);
$table = TableRegistry::get('Author');
$this->assertInstanceOf('\TestApp\Model\Repository\AuthorTable', $table);
$class = $this->getMockClass('\Cake\ORM\Table');
class_alias($class, 'MyPlugin\Model\Repository\SuperTestTable');
$table = TableRegistry::get('MyPlugin.SuperTest');
$this->assertInstanceOf($class, $table);
}
/**
* Tests the table method

0 comments on commit 9a7e621

Please sign in to comment.
You can’t perform that action at this time.