TableRegistryTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Datasource\ConnectionManager;
  19. use Cake\ORM\Table;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Used to test correct class is instantiated when using TableRegistry::get();
  24. */
  25. class MyUsersTable extends Table {
  26. /**
  27. * Overrides default table name
  28. *
  29. * @var string
  30. */
  31. protected $_table = 'users';
  32. }
  33. /**
  34. * Test case for TableRegistry
  35. */
  36. class TableRegistryTest extends TestCase {
  37. /**
  38. * setup
  39. *
  40. * @return void
  41. */
  42. public function setUp() {
  43. parent::setUp();
  44. Configure::write('App.namespace', 'TestApp');
  45. }
  46. /**
  47. * tear down
  48. *
  49. * @return void
  50. */
  51. public function tearDown() {
  52. parent::tearDown();
  53. TableRegistry::clear();
  54. }
  55. /**
  56. * Test config() method.
  57. *
  58. * @return void
  59. */
  60. public function testConfig() {
  61. $this->assertEquals([], TableRegistry::config('Tests'));
  62. $data = [
  63. 'connection' => 'testing',
  64. 'entityClass' => 'TestApp\Model\Entity\Article',
  65. ];
  66. $result = TableRegistry::config('Tests', $data);
  67. $this->assertEquals($data, $result, 'Returns config data.');
  68. $result = TableRegistry::config();
  69. $expected = ['Tests' => $data];
  70. $this->assertEquals($expected, $result);
  71. }
  72. /**
  73. * Test calling config() on existing instances throws an error.
  74. *
  75. * @expectedException RuntimeException
  76. * @expectedExceptionMessage You cannot configure "Users", it has already been constructed.
  77. * @return void
  78. */
  79. public function testConfigOnDefinedInstance() {
  80. $users = TableRegistry::get('Users');
  81. TableRegistry::config('Users', ['table' => 'my_users']);
  82. }
  83. /**
  84. * Test the exists() method.
  85. *
  86. * @return void
  87. */
  88. public function testExists() {
  89. $this->assertFalse(TableRegistry::exists('Articles'));
  90. TableRegistry::config('Articles', ['table' => 'articles']);
  91. $this->assertFalse(TableRegistry::exists('Articles'));
  92. TableRegistry::get('Articles', ['table' => 'articles']);
  93. $this->assertTrue(TableRegistry::exists('Articles'));
  94. }
  95. /**
  96. * Test getting instances from the registry.
  97. *
  98. * @return void
  99. */
  100. public function testGet() {
  101. $result = TableRegistry::get('Articles', [
  102. 'table' => 'my_articles',
  103. ]);
  104. $this->assertInstanceOf('Cake\ORM\Table', $result);
  105. $this->assertEquals('my_articles', $result->table());
  106. $result2 = TableRegistry::get('Articles');
  107. $this->assertSame($result, $result2);
  108. $this->assertEquals('my_articles', $result->table());
  109. }
  110. /**
  111. * Test that get() uses config data set with config()
  112. *
  113. * @return void
  114. */
  115. public function testGetWithConfig() {
  116. TableRegistry::config('Articles', [
  117. 'table' => 'my_articles',
  118. ]);
  119. $result = TableRegistry::get('Articles');
  120. $this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
  121. }
  122. /**
  123. * Test get with config throws an exception if the alias exists already.
  124. *
  125. * @expectedException RuntimeException
  126. * @expectedExceptionMessage You cannot configure "Users", it already exists in the registry.
  127. * @return void
  128. */
  129. public function testGetExistingWithConfigData() {
  130. $users = TableRegistry::get('Users');
  131. TableRegistry::get('Users', ['table' => 'my_users']);
  132. }
  133. /**
  134. * Tests that tables can be instantiated based on conventions
  135. * and using plugin notation
  136. *
  137. * @return void
  138. */
  139. public function testGetWithConventions() {
  140. $table = TableRegistry::get('articles');
  141. $this->assertInstanceOf('\TestApp\Model\Table\ArticlesTable', $table);
  142. $table = TableRegistry::get('Articles');
  143. $this->assertInstanceOf('\TestApp\Model\Table\ArticlesTable', $table);
  144. $table = TableRegistry::get('authors');
  145. $this->assertInstanceOf('\TestApp\Model\Table\AuthorsTable', $table);
  146. $table = TableRegistry::get('Authors');
  147. $this->assertInstanceOf('\TestApp\Model\Table\AuthorsTable', $table);
  148. }
  149. /**
  150. * Test get() with plugin syntax aliases
  151. *
  152. * @return void
  153. */
  154. public function testGetPlugin() {
  155. Plugin::load('TestPlugin');
  156. $table = TableRegistry::get('TestPlugin.TestPluginComments', ['connection' => 'test']);
  157. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  158. $this->assertInstanceOf($class, $table);
  159. $this->assertTrue(
  160. TableRegistry::exists('TestPluginComments'),
  161. 'Short form should exist'
  162. );
  163. $this->assertTrue(
  164. TableRegistry::exists('TestPlugin.TestPluginComments'),
  165. 'Long form should exist'
  166. );
  167. $second = TableRegistry::get('TestPlugin.TestPluginComments');
  168. $this->assertSame($table, $second, 'Can fetch long form');
  169. $second = TableRegistry::get('TestPluginComments');
  170. $this->assertSame($table, $second);
  171. }
  172. /**
  173. * Test get() with plugin aliases + className option.
  174. *
  175. * @return void
  176. */
  177. public function testGetPluginWithClassNameOption() {
  178. Plugin::load('TestPlugin');
  179. $table = TableRegistry::get('Comments', [
  180. 'className' => 'TestPlugin.TestPluginComments',
  181. 'connection' => 'test'
  182. ]);
  183. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  184. $this->assertInstanceOf($class, $table);
  185. $this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
  186. $this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
  187. $second = TableRegistry::get('Comments');
  188. $this->assertSame($table, $second);
  189. }
  190. /**
  191. * Test get() with full namespaced classname
  192. *
  193. * @return void
  194. */
  195. public function testGetPluginWithFullNamespaceName() {
  196. Plugin::load('TestPlugin');
  197. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  198. $table = TableRegistry::get('Comments', [
  199. 'className' => $class,
  200. 'connection' => 'test'
  201. ]);
  202. $this->assertInstanceOf($class, $table);
  203. $this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
  204. $this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
  205. }
  206. /**
  207. * Tests that table options can be pre-configured for the factory method
  208. *
  209. * @return void
  210. */
  211. public function testConfigAndBuild() {
  212. TableRegistry::clear();
  213. $map = TableRegistry::config();
  214. $this->assertEquals([], $map);
  215. $connection = ConnectionManager::get('test', false);
  216. $options = ['connection' => $connection];
  217. TableRegistry::config('users', $options);
  218. $map = TableRegistry::config();
  219. $this->assertEquals(['users' => $options], $map);
  220. $this->assertEquals($options, TableRegistry::config('users'));
  221. $schema = ['id' => ['type' => 'rubbish']];
  222. $options += ['schema' => $schema];
  223. TableRegistry::config('users', $options);
  224. $table = TableRegistry::get('users', ['table' => 'users']);
  225. $this->assertInstanceOf('Cake\ORM\Table', $table);
  226. $this->assertEquals('users', $table->table());
  227. $this->assertEquals('users', $table->alias());
  228. $this->assertSame($connection, $table->connection());
  229. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  230. $this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
  231. TableRegistry::clear();
  232. $this->assertEmpty(TableRegistry::config());
  233. TableRegistry::config('users', $options);
  234. $table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
  235. $this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
  236. $this->assertEquals('users', $table->table());
  237. $this->assertEquals('users', $table->alias());
  238. $this->assertSame($connection, $table->connection());
  239. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  240. }
  241. /**
  242. * Test setting an instance.
  243. *
  244. * @return void
  245. */
  246. public function testSet() {
  247. $mock = $this->getMock('Cake\ORM\Table');
  248. $this->assertSame($mock, TableRegistry::set('Articles', $mock));
  249. $this->assertSame($mock, TableRegistry::get('Articles'));
  250. }
  251. }