TableRegistryTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Core\Configure;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\ORM\Table;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Used to test correct class is instantiated when using TableRegistry::get();
  23. */
  24. class MyUsersTable extends Table {
  25. /**
  26. * Overrides default table name
  27. *
  28. * @var string
  29. */
  30. protected $_table = 'users';
  31. }
  32. /**
  33. * Test case for TableRegistry
  34. */
  35. class TableRegistryTest extends TestCase {
  36. /**
  37. * setup
  38. *
  39. * @return void
  40. */
  41. public function setUp() {
  42. parent::setUp();
  43. Configure::write('App.namespace', 'TestApp');
  44. }
  45. /**
  46. * tear down
  47. *
  48. * @return void
  49. */
  50. public function tearDown() {
  51. parent::tearDown();
  52. TableRegistry::clear();
  53. }
  54. /**
  55. * Test config() method.
  56. *
  57. * @return void
  58. */
  59. public function testConfig() {
  60. $this->assertEquals([], TableRegistry::config('Tests'));
  61. $data = [
  62. 'connection' => 'testing',
  63. 'entityClass' => 'TestApp\Model\Entity\Article',
  64. ];
  65. $result = TableRegistry::config('Tests', $data);
  66. $this->assertEquals($data, $result, 'Returns config data.');
  67. $result = TableRegistry::config();
  68. $expected = ['Tests' => $data];
  69. $this->assertEquals($expected, $result);
  70. }
  71. /**
  72. * Test calling config() on existing instances throws an error.
  73. *
  74. * @expectedException RuntimeException
  75. * @expectedExceptionMessage You cannot configure "Users", it has already been constructed.
  76. * @return void
  77. */
  78. public function testConfigOnDefinedInstance() {
  79. $users = TableRegistry::get('Users');
  80. TableRegistry::config('Users', ['table' => 'my_users']);
  81. }
  82. /**
  83. * Test the exists() method.
  84. *
  85. * @return void
  86. */
  87. public function testExists() {
  88. $this->assertFalse(TableRegistry::exists('Articles'));
  89. TableRegistry::config('Articles', ['table' => 'articles']);
  90. $this->assertFalse(TableRegistry::exists('Articles'));
  91. TableRegistry::get('Articles', ['table' => 'articles']);
  92. $this->assertTrue(TableRegistry::exists('Articles'));
  93. }
  94. /**
  95. * Test getting instances from the registry.
  96. *
  97. * @return void
  98. */
  99. public function testGet() {
  100. $result = TableRegistry::get('Articles', [
  101. 'table' => 'my_articles',
  102. ]);
  103. $this->assertInstanceOf('Cake\ORM\Table', $result);
  104. $this->assertEquals('my_articles', $result->table());
  105. $result2 = TableRegistry::get('Articles');
  106. $this->assertSame($result, $result2);
  107. $this->assertEquals('my_articles', $result->table());
  108. }
  109. /**
  110. * Test that get() uses config data set with config()
  111. *
  112. * @return void
  113. */
  114. public function testGetWithConfig() {
  115. TableRegistry::config('Articles', [
  116. 'table' => 'my_articles',
  117. ]);
  118. $result = TableRegistry::get('Articles');
  119. $this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
  120. }
  121. /**
  122. * Test get with config throws an exception if the alias exists already.
  123. *
  124. * @expectedException RuntimeException
  125. * @expectedExceptionMessage You cannot configure "Users", it already exists in the registry.
  126. * @return void
  127. */
  128. public function testGetExistingWithConfigData() {
  129. $users = TableRegistry::get('Users');
  130. TableRegistry::get('Users', ['table' => 'my_users']);
  131. }
  132. /**
  133. * Tests that tables can be instantiated based on conventions
  134. * and using plugin notation
  135. *
  136. * @return void
  137. */
  138. public function testBuildConvention() {
  139. $table = TableRegistry::get('articles');
  140. $this->assertInstanceOf('\TestApp\Model\Table\ArticlesTable', $table);
  141. $table = TableRegistry::get('Articles');
  142. $this->assertInstanceOf('\TestApp\Model\Table\ArticlesTable', $table);
  143. $table = TableRegistry::get('authors');
  144. $this->assertInstanceOf('\TestApp\Model\Table\AuthorsTable', $table);
  145. $table = TableRegistry::get('Authors');
  146. $this->assertInstanceOf('\TestApp\Model\Table\AuthorsTable', $table);
  147. $class = $this->getMockClass('\Cake\ORM\Table');
  148. if (!class_exists('MyPlugin\Model\Table\SuperTestsTable')) {
  149. class_alias($class, 'MyPlugin\Model\Table\SuperTestsTable');
  150. }
  151. $table = TableRegistry::get('MyPlugin.SuperTests', ['connection' => 'test']);
  152. $this->assertInstanceOf($class, $table);
  153. }
  154. /**
  155. * Tests that table options can be pre-configured for the factory method
  156. *
  157. * @return void
  158. */
  159. public function testConfigAndBuild() {
  160. TableRegistry::clear();
  161. $map = TableRegistry::config();
  162. $this->assertEquals([], $map);
  163. $connection = ConnectionManager::get('test', false);
  164. $options = ['connection' => $connection];
  165. TableRegistry::config('users', $options);
  166. $map = TableRegistry::config();
  167. $this->assertEquals(['users' => $options], $map);
  168. $this->assertEquals($options, TableRegistry::config('users'));
  169. $schema = ['id' => ['type' => 'rubbish']];
  170. $options += ['schema' => $schema];
  171. TableRegistry::config('users', $options);
  172. $table = TableRegistry::get('users', ['table' => 'users']);
  173. $this->assertInstanceOf('Cake\ORM\Table', $table);
  174. $this->assertEquals('users', $table->table());
  175. $this->assertEquals('users', $table->alias());
  176. $this->assertSame($connection, $table->connection());
  177. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  178. $this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
  179. TableRegistry::clear();
  180. $this->assertEmpty(TableRegistry::config());
  181. TableRegistry::config('users', $options);
  182. $table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
  183. $this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
  184. $this->assertEquals('users', $table->table());
  185. $this->assertEquals('users', $table->alias());
  186. $this->assertSame($connection, $table->connection());
  187. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  188. }
  189. /**
  190. * Test setting an instance.
  191. *
  192. * @return void
  193. */
  194. public function testSet() {
  195. $mock = $this->getMock('Cake\ORM\Table');
  196. $this->assertSame($mock, TableRegistry::set('Articles', $mock));
  197. $this->assertSame($mock, TableRegistry::get('Articles'));
  198. }
  199. }