Skip to content
Permalink
Browse files

Adding support for Table::build to load classes based on conventions

  • Loading branch information...
lorenzo committed Oct 19, 2013
1 parent b234f51 commit a978197da9c139928ca7caf849287fad781c3edf
Showing with 38 additions and 4 deletions.
  1. +10 −1 Cake/ORM/Table.php
  2. +28 −3 Cake/Test/TestCase/ORM/TableTest.php
@@ -218,11 +218,20 @@ public static function build($alias, array $options = []) {
return static::$_instances[$alias];
}
$options = ['alias' => $alias] + $options;
list($plugin, $baseClass) = pluginSplit($alias);
$options = ['alias' => $baseClass] + $options;
if (empty($options['className'])) {
$options['className'] = get_called_class();
}
if ($options['className'] === __CLASS__ || $plugin) {
$class = $options['className'];
$classified = Inflector::classify($alias);
$class = App::classname($classified, 'Model\Repository', 'Table') ?: $class;
$options['className'] = $class;
}
return static::$_instances[$alias] = new $options['className']($options);
}
@@ -54,16 +54,18 @@ class MyUsersTable extends Table {
*/
class TableTest extends \Cake\TestSuite\TestCase {
public $fixtures = ['core.user', 'core.category'];
public $fixtures = ['core.user', 'core.category', 'core.article', 'core.author'];
public function setUp() {
parent::setUp();
$this->connection = ConnectionManager::get('test');
Configure::write('App.namespace', 'TestApp');
}
public function tearDown() {
parent::tearDown();
Table::clearRegistry();
Configure::write('App.namespace', 'App');
}
/**
* Tests that table options can be pre-configured for the factory method
@@ -105,6 +107,29 @@ public function testConfigAndBuild() {
$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 = Table::build('article');
$this->assertInstanceOf('\TestApp\Model\Repository\ArticleTable', $table);
$table = Table::build('Article');
$this->assertInstanceOf('\TestApp\Model\Repository\ArticleTable', $table);
$table = Table::build('author');
$this->assertInstanceOf('\TestApp\Model\Repository\AuthorTable', $table);
$table = Table::build('Author');
$this->assertInstanceOf('\TestApp\Model\Repository\AuthorTable', $table);
$class = $this->getMockClass('\Cake\ORM\Table');
class_alias($class, 'MyPlugin\Model\Repository\SuperTestTable');
$table = Table::build('MyPlugin.SuperTest');
$this->assertInstanceOf($class, $table);
}
/**
* Tests getting and setting a Table instance in the registry
*
@@ -812,9 +837,9 @@ public function testEntityClassDefault() {
*/
public function testRepositoryClassInAPP() {
$class = $this->getMockClass('\Cake\ORM\Entity');
class_alias($class, 'App\Model\Entity\TestUser');
class_alias($class, 'TestApp\Model\Entity\TestUser');
$table = new Table();
$this->assertEquals('App\Model\Entity\TestUser', $table->entityClass('TestUser'));
$this->assertEquals('TestApp\Model\Entity\TestUser', $table->entityClass('TestUser'));
}
/**

0 comments on commit a978197

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