TableRegistryTest.php 16 KB

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