Skip to content

Commit

Permalink
Update docs + add tests for TableRegistry.
Browse files Browse the repository at this point in the history
Fix some documentation and start adding tests for TableRegistry.
  • Loading branch information
markstory committed Oct 20, 2013
1 parent d1f06dd commit 1322322
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 14 deletions.
29 changes: 15 additions & 14 deletions Cake/ORM/TableRegistry.php
Expand Up @@ -42,30 +42,31 @@ class TableRegistry {
protected static $_instances = [];

/**
* Stores a list of options to be used when instantiating an object for the table
* with the same name as $table. The options that can be stored are those that
* are recognized by `build()`
* Stores a list of options to be used when instantiating an object
* with a matching alias.
*
* If second argument is omitted, it will return the current settings for $table
* The options that can be stored are those that are recognized by `build()`
* If second argument is omitted, it will return the current settings
* for $alias.
*
* If no arguments are passed it will return the full configuration array for
* all tables
* all aliases
*
* @param string $table name of the table
* @param null|array $options list of options for the table
* @return array
* @param string $alias Name of the alias
* @param null|array $options list of options for the alias
* @return array The config data.
*/
public static function config($table = null, $options = null) {
if ($table === null) {
public static function config($alias = null, $options = null) {
if ($alias === null) {
return static::$_config;
}
if (!is_string($table)) {
return static::$_config = $table;
if (!is_string($alias)) {
return static::$_config = $alias;
}
if ($options === null) {
return isset(static::$_config[$table]) ? static::$_config[$table] : [];
return isset(static::$_config[$alias]) ? static::$_config[$alias] : [];
}
return static::$_config[$table] = $options;
return static::$_config[$alias] = $options;
}

/**
Expand Down
95 changes: 95 additions & 0 deletions Cake/Test/TestCase/ORM/TableRegistryTest.php
@@ -0,0 +1,95 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\ORM;

use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
* Test case for TableRegistry
*/
class TableRegistryTest extends TestCase {

/**
* tear down
*
* @return void
*/
public function tearDown() {
parent::tearDown();
TableRegistry::clear();
}

/**
* Test config() method.
*
* @return void
*/
public function testConfig() {
$this->assertEquals([], TableRegistry::config('Test'));

$data = [
'connection' => 'testing',
'entityClass' => 'TestApp\Model\Entity\Article',
];
$result = TableRegistry::config('Test', $data);
$this->assertEquals($data, $result, 'Returns config data.');

$result = TableRegistry::config();
$expected = ['Test' => $data];
$this->assertEquals($expected, $result);
}

/**
* Test getting instances from the registry.
*
* @return void
*/
public function testGet() {
$result = TableRegistry::get('Article', ['table' => 'my_articles']);
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertEquals('my_articles', $result->table());

$result2 = TableRegistry::get('Article', ['table' => 'herp_derp']);
$this->assertSame($result, $result2);
$this->assertEquals('my_articles', $result->table());
}

/**
* Test that get() uses config data set with config()
*
* @return void
*/
public function testGetWithConfig() {
TableRegistry::config('Article', ['table' => 'my_articles']);
$result = TableRegistry::get('Article');
$this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
}

/**
* Test setting an instance.
*
* @return void
*/
public function testSet() {
$mock = $this->getMock('Cake\ORM\Table');
$this->assertSame($mock, TableRegistry::set('Article', $mock));
$this->assertSame($mock, TableRegistry::get('Article'));
}

}

0 comments on commit 1322322

Please sign in to comment.