Skip to content

Commit

Permalink
Move tests into TableRegistry suite as they belong there.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 20, 2013
1 parent 95e77e5 commit 9a7e621
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 78 deletions.
3 changes: 2 additions & 1 deletion Cake/ORM/Table.php
Expand Up @@ -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 {

Expand Down
1 change: 0 additions & 1 deletion Cake/ORM/TableRegistry.php
Expand Up @@ -100,7 +100,6 @@ public static function get($alias, $options = []) {
return static::$_instances[$alias];
}


list($plugin, $baseClass) = pluginSplit($alias);
$options = ['alias' => $baseClass] + $options;

Expand Down
95 changes: 95 additions & 0 deletions Cake/Test/TestCase/ORM/TableRegistryTest.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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.
*
Expand Down
76 changes: 0 additions & 76 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -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
*
Expand All @@ -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
Expand Down

0 comments on commit 9a7e621

Please sign in to comment.