TableRegistryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. /**
  28. * Overrides default table name
  29. *
  30. * @var string
  31. */
  32. protected $_table = 'users';
  33. }
  34. /**
  35. * Test case for TableRegistry
  36. */
  37. class TableRegistryTest extends TestCase
  38. {
  39. /**
  40. * setup
  41. *
  42. * @return void
  43. */
  44. public function setUp()
  45. {
  46. parent::setUp();
  47. Configure::write('App.namespace', 'TestApp');
  48. }
  49. /**
  50. * tear down
  51. *
  52. * @return void
  53. */
  54. public function tearDown()
  55. {
  56. parent::tearDown();
  57. TableRegistry::clear();
  58. }
  59. /**
  60. * Test config() method.
  61. *
  62. * @return void
  63. */
  64. public function testConfig()
  65. {
  66. $this->assertEquals([], TableRegistry::config('Tests'));
  67. $data = [
  68. 'connection' => 'testing',
  69. 'entityClass' => 'TestApp\Model\Entity\Article',
  70. ];
  71. $result = TableRegistry::config('Tests', $data);
  72. $this->assertEquals($data, $result, 'Returns config data.');
  73. $result = TableRegistry::config();
  74. $expected = ['Tests' => $data];
  75. $this->assertEquals($expected, $result);
  76. }
  77. /**
  78. * Test config() method with plugin syntax aliases
  79. *
  80. * @return void
  81. */
  82. public function testConfigPlugin()
  83. {
  84. Plugin::load('TestPlugin');
  85. $data = [
  86. 'connection' => 'testing',
  87. 'entityClass' => 'TestPlugin\Model\Entity\Comment',
  88. ];
  89. $result = TableRegistry::config('TestPlugin.TestPluginComments', $data);
  90. $this->assertEquals($data, $result, 'Returns config data.');
  91. $result = TableRegistry::config();
  92. $expected = ['TestPluginComments' => $data];
  93. $this->assertEquals($expected, $result);
  94. }
  95. /**
  96. * Test calling config() on existing instances throws an error.
  97. *
  98. * @expectedException \RuntimeException
  99. * @expectedExceptionMessage You cannot configure "Users", it has already been constructed.
  100. * @return void
  101. */
  102. public function testConfigOnDefinedInstance()
  103. {
  104. $users = TableRegistry::get('Users');
  105. TableRegistry::config('Users', ['table' => 'my_users']);
  106. }
  107. /**
  108. * Test the exists() method.
  109. *
  110. * @return void
  111. */
  112. public function testExists()
  113. {
  114. $this->assertFalse(TableRegistry::exists('Articles'));
  115. TableRegistry::config('Articles', ['table' => 'articles']);
  116. $this->assertFalse(TableRegistry::exists('Articles'));
  117. TableRegistry::get('Articles', ['table' => 'articles']);
  118. $this->assertTrue(TableRegistry::exists('Articles'));
  119. }
  120. /**
  121. * Test getting instances from the registry.
  122. *
  123. * @return void
  124. */
  125. public function testGet()
  126. {
  127. $result = TableRegistry::get('Articles', [
  128. 'table' => 'my_articles',
  129. ]);
  130. $this->assertInstanceOf('Cake\ORM\Table', $result);
  131. $this->assertEquals('my_articles', $result->table());
  132. $result2 = TableRegistry::get('Articles');
  133. $this->assertSame($result, $result2);
  134. $this->assertEquals('my_articles', $result->table());
  135. }
  136. /**
  137. * Test that get() uses config data set with config()
  138. *
  139. * @return void
  140. */
  141. public function testGetWithConfig()
  142. {
  143. TableRegistry::config('Articles', [
  144. 'table' => 'my_articles',
  145. ]);
  146. $result = TableRegistry::get('Articles');
  147. $this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
  148. }
  149. /**
  150. * Test get with config throws an exception if the alias exists already.
  151. *
  152. * @expectedException \RuntimeException
  153. * @expectedExceptionMessage You cannot configure "Users", it already exists in the registry.
  154. * @return void
  155. */
  156. public function testGetExistingWithConfigData()
  157. {
  158. $users = TableRegistry::get('Users');
  159. TableRegistry::get('Users', ['table' => 'my_users']);
  160. }
  161. /**
  162. * Test get() can be called several times with the same option without
  163. * throwing an exception.
  164. *
  165. * @return void
  166. */
  167. public function testGetWithSameOption()
  168. {
  169. $result = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
  170. $result2 = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
  171. $this->assertEquals($result, $result2);
  172. }
  173. /**
  174. * Tests that tables can be instantiated based on conventions
  175. * and using plugin notation
  176. *
  177. * @return void
  178. */
  179. public function testGetWithConventions()
  180. {
  181. $table = TableRegistry::get('articles');
  182. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  183. $table = TableRegistry::get('Articles');
  184. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  185. $table = TableRegistry::get('authors');
  186. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  187. $table = TableRegistry::get('Authors');
  188. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  189. }
  190. /**
  191. * Test get() with plugin syntax aliases
  192. *
  193. * @return void
  194. */
  195. public function testGetPlugin()
  196. {
  197. Plugin::load('TestPlugin');
  198. $table = TableRegistry::get('TestPlugin.TestPluginComments', ['connection' => 'test']);
  199. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  200. $this->assertInstanceOf($class, $table);
  201. $this->assertTrue(
  202. TableRegistry::exists('TestPluginComments'),
  203. 'Short form should exist'
  204. );
  205. $this->assertTrue(
  206. TableRegistry::exists('TestPlugin.TestPluginComments'),
  207. 'Long form should exist'
  208. );
  209. $second = TableRegistry::get('TestPlugin.TestPluginComments');
  210. $this->assertSame($table, $second, 'Can fetch long form');
  211. $second = TableRegistry::get('TestPluginComments');
  212. $this->assertSame($table, $second);
  213. }
  214. /**
  215. * Test get() with plugin aliases + className option.
  216. *
  217. * @return void
  218. */
  219. public function testGetPluginWithClassNameOption()
  220. {
  221. Plugin::load('TestPlugin');
  222. $table = TableRegistry::get('Comments', [
  223. 'className' => 'TestPlugin.TestPluginComments',
  224. 'connection' => 'test'
  225. ]);
  226. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  227. $this->assertInstanceOf($class, $table);
  228. $this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
  229. $this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
  230. $second = TableRegistry::get('Comments');
  231. $this->assertSame($table, $second);
  232. }
  233. /**
  234. * Test get() with full namespaced classname
  235. *
  236. * @return void
  237. */
  238. public function testGetPluginWithFullNamespaceName()
  239. {
  240. Plugin::load('TestPlugin');
  241. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  242. $table = TableRegistry::get('Comments', [
  243. 'className' => $class,
  244. 'connection' => 'test'
  245. ]);
  246. $this->assertInstanceOf($class, $table);
  247. $this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
  248. $this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
  249. }
  250. /**
  251. * Tests that table options can be pre-configured for the factory method
  252. *
  253. * @return void
  254. */
  255. public function testConfigAndBuild()
  256. {
  257. TableRegistry::clear();
  258. $map = TableRegistry::config();
  259. $this->assertEquals([], $map);
  260. $connection = ConnectionManager::get('test', false);
  261. $options = ['connection' => $connection];
  262. TableRegistry::config('users', $options);
  263. $map = TableRegistry::config();
  264. $this->assertEquals(['users' => $options], $map);
  265. $this->assertEquals($options, TableRegistry::config('users'));
  266. $schema = ['id' => ['type' => 'rubbish']];
  267. $options += ['schema' => $schema];
  268. TableRegistry::config('users', $options);
  269. $table = TableRegistry::get('users', ['table' => 'users']);
  270. $this->assertInstanceOf('Cake\ORM\Table', $table);
  271. $this->assertEquals('users', $table->table());
  272. $this->assertEquals('users', $table->alias());
  273. $this->assertSame($connection, $table->connection());
  274. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  275. $this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
  276. TableRegistry::clear();
  277. $this->assertEmpty(TableRegistry::config());
  278. TableRegistry::config('users', $options);
  279. $table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
  280. $this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
  281. $this->assertEquals('users', $table->table());
  282. $this->assertEquals('users', $table->alias());
  283. $this->assertSame($connection, $table->connection());
  284. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  285. }
  286. /**
  287. * Test setting an instance.
  288. *
  289. * @return void
  290. */
  291. public function testSet()
  292. {
  293. $mock = $this->getMock('Cake\ORM\Table');
  294. $this->assertSame($mock, TableRegistry::set('Articles', $mock));
  295. $this->assertSame($mock, TableRegistry::get('Articles'));
  296. }
  297. /**
  298. * Test setting an instance with plugin syntax aliases
  299. *
  300. * @return void
  301. */
  302. public function testSetPlugin()
  303. {
  304. Plugin::load('TestPlugin');
  305. $mock = $this->getMock('TestPlugin\Model\Table\CommentsTable');
  306. $this->assertSame($mock, TableRegistry::set('TestPlugin.Comments', $mock));
  307. $this->assertSame($mock, TableRegistry::get('TestPlugin.Comments'));
  308. $this->assertSame($mock, TableRegistry::get('Comments'));
  309. }
  310. /**
  311. * Tests genericInstances
  312. *
  313. * @return void
  314. */
  315. public function testGenericInstances()
  316. {
  317. $foos = TableRegistry::get('Foos');
  318. $bars = TableRegistry::get('Bars');
  319. TableRegistry::get('Articles');
  320. $expected = ['Foos' => $foos, 'Bars' => $bars];
  321. $this->assertEquals($expected, TableRegistry::genericInstances());
  322. }
  323. /**
  324. * Tests remove an instance
  325. *
  326. * @return void
  327. */
  328. public function testRemove()
  329. {
  330. Plugin::load('TestPlugin');
  331. $pluginTable = TableRegistry::get('TestPlugin.Comments');
  332. $cachedTable = TableRegistry::get('Comments');
  333. $this->assertTrue(TableRegistry::exists('Comments'));
  334. $this->assertSame($pluginTable, $cachedTable);
  335. TableRegistry::remove('Comments');
  336. $this->assertFalse(TableRegistry::exists('Comments'));
  337. $appTable = TableRegistry::get('Comments');
  338. $this->assertTrue(TableRegistry::exists('Comments'));
  339. $this->assertNotSame($pluginTable, $appTable);
  340. }
  341. }