TableRegistryTest.php 18 KB

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