TableRegistryTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. * Are auto-models instanciated correctly? How about when they have an alias?
  152. *
  153. * @return void
  154. */
  155. public function testGetFallbacks()
  156. {
  157. $result = TableRegistry::get('Droids');
  158. $this->assertInstanceOf('Cake\ORM\Table', $result);
  159. $this->assertEquals('droids', $result->table());
  160. $this->assertEquals('Droids', $result->alias());
  161. $result = TableRegistry::get('R2D2', ['className' => 'Droids']);
  162. $this->assertInstanceOf('Cake\ORM\Table', $result);
  163. $this->assertEquals('droids', $result->table(), 'The table should be derived from the className');
  164. $this->assertEquals('R2D2', $result->alias());
  165. $result = TableRegistry::get('C3P0', ['className' => 'Droids', 'table' => 'rebels']);
  166. $this->assertInstanceOf('Cake\ORM\Table', $result);
  167. $this->assertEquals('rebels', $result->table(), 'The table should be taken from options');
  168. $this->assertEquals('C3P0', $result->alias());
  169. $result = TableRegistry::get('Funky.Chipmunks');
  170. $this->assertInstanceOf('Cake\ORM\Table', $result);
  171. $this->assertEquals('chipmunks', $result->table(), 'The table should be derived from the alias');
  172. $this->assertEquals('Chipmunks', $result->alias());
  173. $result = TableRegistry::get('Awesome', ['className' => 'Funky.Monkies']);
  174. $this->assertInstanceOf('Cake\ORM\Table', $result);
  175. $this->assertEquals('monkies', $result->table(), 'The table should be derived from the classname');
  176. $this->assertEquals('Awesome', $result->alias());
  177. $result = TableRegistry::get('Stuff', ['className' => 'Cake\ORM\Table']);
  178. $this->assertInstanceOf('Cake\ORM\Table', $result);
  179. $this->assertEquals('stuff', $result->table(), 'The table should be derived from the alias');
  180. $this->assertEquals('Stuff', $result->alias());
  181. }
  182. /**
  183. * Test that get() uses config data set with config()
  184. *
  185. * @return void
  186. */
  187. public function testGetWithConfig()
  188. {
  189. TableRegistry::config('Articles', [
  190. 'table' => 'my_articles',
  191. ]);
  192. $result = TableRegistry::get('Articles');
  193. $this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
  194. }
  195. /**
  196. * Test that get() uses config data `className` set with config()
  197. *
  198. * @return void
  199. */
  200. public function testGetWithConfigClassName()
  201. {
  202. TableRegistry::config('MyUsersTableAlias', [
  203. 'className' => '\Cake\Test\TestCase\ORM\MyUsersTable',
  204. ]);
  205. $result = TableRegistry::get('MyUsersTableAlias');
  206. $this->assertInstanceOf('\Cake\Test\TestCase\ORM\MyUsersTable', $result, 'Should use config() data className option.');
  207. }
  208. /**
  209. * Test get with config throws an exception if the alias exists already.
  210. *
  211. * @expectedException \RuntimeException
  212. * @expectedExceptionMessage You cannot configure "Users", it already exists in the registry.
  213. * @return void
  214. */
  215. public function testGetExistingWithConfigData()
  216. {
  217. $users = TableRegistry::get('Users');
  218. TableRegistry::get('Users', ['table' => 'my_users']);
  219. }
  220. /**
  221. * Test get() can be called several times with the same option without
  222. * throwing an exception.
  223. *
  224. * @return void
  225. */
  226. public function testGetWithSameOption()
  227. {
  228. $result = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
  229. $result2 = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
  230. $this->assertEquals($result, $result2);
  231. }
  232. /**
  233. * Tests that tables can be instantiated based on conventions
  234. * and using plugin notation
  235. *
  236. * @return void
  237. */
  238. public function testGetWithConventions()
  239. {
  240. $table = TableRegistry::get('articles');
  241. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  242. $table = TableRegistry::get('Articles');
  243. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  244. $table = TableRegistry::get('authors');
  245. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  246. $table = TableRegistry::get('Authors');
  247. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  248. }
  249. /**
  250. * Test get() with plugin syntax aliases
  251. *
  252. * @return void
  253. */
  254. public function testGetPlugin()
  255. {
  256. Plugin::load('TestPlugin');
  257. $table = TableRegistry::get('TestPlugin.TestPluginComments');
  258. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $table);
  259. $this->assertFalse(
  260. TableRegistry::exists('TestPluginComments'),
  261. 'Short form should NOT exist'
  262. );
  263. $this->assertTrue(
  264. TableRegistry::exists('TestPlugin.TestPluginComments'),
  265. 'Long form should exist'
  266. );
  267. $second = TableRegistry::get('TestPlugin.TestPluginComments');
  268. $this->assertSame($table, $second, 'Can fetch long form');
  269. }
  270. /**
  271. * Test get() with same-alias models in different plugins
  272. *
  273. * There should be no internal cache-confusion
  274. *
  275. * @return void
  276. */
  277. public function testGetMultiplePlugins()
  278. {
  279. Plugin::load('TestPlugin');
  280. Plugin::load('TestPluginTwo');
  281. $app = TableRegistry::get('Comments');
  282. $plugin1 = TableRegistry::get('TestPlugin.Comments');
  283. $plugin2 = TableRegistry::get('TestPluginTwo.Comments');
  284. $this->assertInstanceOf('Cake\ORM\Table', $app, 'Should be an app table instance');
  285. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $plugin1, 'Should be a plugin 1 table instance');
  286. $this->assertInstanceOf('TestPluginTwo\Model\Table\CommentsTable', $plugin2, 'Should be a plugin 2 table instance');
  287. $plugin2 = TableRegistry::get('TestPluginTwo.Comments');
  288. $plugin1 = TableRegistry::get('TestPlugin.Comments');
  289. $app = TableRegistry::get('Comments');
  290. $this->assertInstanceOf('Cake\ORM\Table', $app, 'Should still be an app table instance');
  291. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $plugin1, 'Should still be a plugin 1 table instance');
  292. $this->assertInstanceOf('TestPluginTwo\Model\Table\CommentsTable', $plugin2, 'Should still be a plugin 2 table instance');
  293. }
  294. /**
  295. * Test get() with plugin aliases + className option.
  296. *
  297. * @return void
  298. */
  299. public function testGetPluginWithClassNameOption()
  300. {
  301. Plugin::load('TestPlugin');
  302. $table = TableRegistry::get('Comments', [
  303. 'className' => 'TestPlugin.TestPluginComments',
  304. ]);
  305. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  306. $this->assertInstanceOf($class, $table);
  307. $this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
  308. $this->assertFalse(TableRegistry::exists('TestPlugin.TestPluginComments'), 'Full class alias should not exist');
  309. $this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
  310. $second = TableRegistry::get('Comments');
  311. $this->assertSame($table, $second);
  312. }
  313. /**
  314. * Test get() with full namespaced classname
  315. *
  316. * @return void
  317. */
  318. public function testGetPluginWithFullNamespaceName()
  319. {
  320. Plugin::load('TestPlugin');
  321. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  322. $table = TableRegistry::get('Comments', [
  323. 'className' => $class,
  324. ]);
  325. $this->assertInstanceOf($class, $table);
  326. $this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
  327. $this->assertFalse(TableRegistry::exists('TestPlugin.TestPluginComments'), 'Full class alias should not exist');
  328. $this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
  329. }
  330. /**
  331. * Tests that table options can be pre-configured for the factory method
  332. *
  333. * @return void
  334. */
  335. public function testConfigAndBuild()
  336. {
  337. TableRegistry::clear();
  338. $map = TableRegistry::config();
  339. $this->assertEquals([], $map);
  340. $connection = ConnectionManager::get('test', false);
  341. $options = ['connection' => $connection];
  342. TableRegistry::config('users', $options);
  343. $map = TableRegistry::config();
  344. $this->assertEquals(['users' => $options], $map);
  345. $this->assertEquals($options, TableRegistry::config('users'));
  346. $schema = ['id' => ['type' => 'rubbish']];
  347. $options += ['schema' => $schema];
  348. TableRegistry::config('users', $options);
  349. $table = TableRegistry::get('users', ['table' => 'users']);
  350. $this->assertInstanceOf('Cake\ORM\Table', $table);
  351. $this->assertEquals('users', $table->table());
  352. $this->assertEquals('users', $table->alias());
  353. $this->assertSame($connection, $table->connection());
  354. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  355. $this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
  356. TableRegistry::clear();
  357. $this->assertEmpty(TableRegistry::config());
  358. TableRegistry::config('users', $options);
  359. $table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
  360. $this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
  361. $this->assertEquals('users', $table->table());
  362. $this->assertEquals('users', $table->alias());
  363. $this->assertSame($connection, $table->connection());
  364. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  365. $this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
  366. }
  367. /**
  368. * Tests that table options can be pre-configured with a single validator
  369. *
  370. * @return void
  371. */
  372. public function testConfigWithSingleValidator()
  373. {
  374. $validator = new Validator();
  375. TableRegistry::config('users', ['validator' => $validator]);
  376. $table = TableRegistry::get('users');
  377. $this->assertSame($table->validator('default'), $validator);
  378. }
  379. /**
  380. * Tests that table options can be pre-configured with multiple validators
  381. *
  382. * @return void
  383. */
  384. public function testConfigWithMultipleValidators()
  385. {
  386. $validator1 = new Validator();
  387. $validator2 = new Validator();
  388. $validator3 = new Validator();
  389. TableRegistry::config('users', [
  390. 'validator' => [
  391. 'default' => $validator1,
  392. 'secondary' => $validator2,
  393. 'tertiary' => $validator3,
  394. ]
  395. ]);
  396. $table = TableRegistry::get('users');
  397. $this->assertSame($table->validator('default'), $validator1);
  398. $this->assertSame($table->validator('secondary'), $validator2);
  399. $this->assertSame($table->validator('tertiary'), $validator3);
  400. }
  401. /**
  402. * Test setting an instance.
  403. *
  404. * @return void
  405. */
  406. public function testSet()
  407. {
  408. $mock = $this->getMock('Cake\ORM\Table');
  409. $this->assertSame($mock, TableRegistry::set('Articles', $mock));
  410. $this->assertSame($mock, TableRegistry::get('Articles'));
  411. }
  412. /**
  413. * Test setting an instance with plugin syntax aliases
  414. *
  415. * @return void
  416. */
  417. public function testSetPlugin()
  418. {
  419. Plugin::load('TestPlugin');
  420. $mock = $this->getMock('TestPlugin\Model\Table\CommentsTable');
  421. $this->assertSame($mock, TableRegistry::set('TestPlugin.Comments', $mock));
  422. $this->assertSame($mock, TableRegistry::get('TestPlugin.Comments'));
  423. }
  424. /**
  425. * Tests genericInstances
  426. *
  427. * @return void
  428. */
  429. public function testGenericInstances()
  430. {
  431. $foos = TableRegistry::get('Foos');
  432. $bars = TableRegistry::get('Bars');
  433. TableRegistry::get('Articles');
  434. $expected = ['Foos' => $foos, 'Bars' => $bars];
  435. $this->assertEquals($expected, TableRegistry::genericInstances());
  436. }
  437. /**
  438. * Tests remove an instance
  439. *
  440. * @return void
  441. */
  442. public function testRemove()
  443. {
  444. $first = TableRegistry::get('Comments');
  445. $this->assertTrue(TableRegistry::exists('Comments'));
  446. TableRegistry::remove('Comments');
  447. $this->assertFalse(TableRegistry::exists('Comments'));
  448. $second = TableRegistry::get('Comments');
  449. $this->assertNotSame($first, $second, 'Should be different objects, as the reference to the first was destroyed');
  450. $this->assertTrue(TableRegistry::exists('Comments'));
  451. }
  452. /**
  453. * testRemovePlugin
  454. *
  455. * Removing a plugin-prefixed model should not affect any other
  456. * plugin-prefixed model, or app model.
  457. * Removing an app model should not affect any other
  458. * plugin-prefixed model.
  459. *
  460. * @return void
  461. */
  462. public function testRemovePlugin()
  463. {
  464. Plugin::load('TestPlugin');
  465. Plugin::load('TestPluginTwo');
  466. $app = TableRegistry::get('Comments');
  467. TableRegistry::get('TestPlugin.Comments');
  468. $plugin = TableRegistry::get('TestPluginTwo.Comments');
  469. $this->assertTrue(TableRegistry::exists('Comments'));
  470. $this->assertTrue(TableRegistry::exists('TestPlugin.Comments'));
  471. $this->assertTrue(TableRegistry::exists('TestPluginTwo.Comments'));
  472. TableRegistry::remove('TestPlugin.Comments');
  473. $this->assertTrue(TableRegistry::exists('Comments'));
  474. $this->assertFalse(TableRegistry::exists('TestPlugin.Comments'));
  475. $this->assertTrue(TableRegistry::exists('TestPluginTwo.Comments'));
  476. $app2 = TableRegistry::get('Comments');
  477. $plugin2 = TableRegistry::get('TestPluginTwo.Comments');
  478. $this->assertSame($app, $app2, 'Should be the same Comments object');
  479. $this->assertSame($plugin, $plugin2, 'Should be the same TestPluginTwo.Comments object');
  480. TableRegistry::remove('Comments');
  481. $this->assertFalse(TableRegistry::exists('Comments'));
  482. $this->assertFalse(TableRegistry::exists('TestPlugin.Comments'));
  483. $this->assertTrue(TableRegistry::exists('TestPluginTwo.Comments'));
  484. $plugin3 = TableRegistry::get('TestPluginTwo.Comments');
  485. $this->assertSame($plugin, $plugin3, 'Should be the same TestPluginTwo.Comments object');
  486. }
  487. }