TableLocatorTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.1.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM\Locator;
  16. use Cake\Core\Plugin;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\ORM\Locator\TableLocator;
  19. use Cake\ORM\Table;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Validation\Validator;
  22. /**
  23. * Used to test correct class is instantiated when using $this->_locator->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 TableLocator
  36. */
  37. class TableLocatorTest extends TestCase
  38. {
  39. /**
  40. * TableLocator instance.
  41. *
  42. * @var \Cake\ORM\Locator\TableLocator
  43. */
  44. protected $_locator;
  45. /**
  46. * setup
  47. *
  48. * @return void
  49. */
  50. public function setUp()
  51. {
  52. parent::setUp();
  53. static::setAppNamespace();
  54. $this->_locator = new TableLocator;
  55. }
  56. /**
  57. * Test config() method.
  58. *
  59. * @return void
  60. */
  61. public function testConfig()
  62. {
  63. $this->assertEquals([], $this->_locator->config('Tests'));
  64. $data = [
  65. 'connection' => 'testing',
  66. 'entityClass' => 'TestApp\Model\Entity\Article',
  67. ];
  68. $result = $this->_locator->config('Tests', $data);
  69. $this->assertEquals($data, $result, 'Returns config data.');
  70. $result = $this->_locator->config();
  71. $expected = ['Tests' => $data];
  72. $this->assertEquals($expected, $result);
  73. }
  74. /**
  75. * Test config() method with plugin syntax aliases
  76. *
  77. * @return void
  78. */
  79. public function testConfigPlugin()
  80. {
  81. Plugin::load('TestPlugin');
  82. $data = [
  83. 'connection' => 'testing',
  84. 'entityClass' => 'TestPlugin\Model\Entity\Comment',
  85. ];
  86. $result = $this->_locator->config('TestPlugin.TestPluginComments', $data);
  87. $this->assertEquals($data, $result, 'Returns config data.');
  88. }
  89. /**
  90. * Test calling config() on existing instances throws an error.
  91. *
  92. * @expectedException \RuntimeException
  93. * @expectedExceptionMessage You cannot configure "Users", it has already been constructed.
  94. * @return void
  95. */
  96. public function testConfigOnDefinedInstance()
  97. {
  98. $users = $this->_locator->get('Users');
  99. $this->_locator->config('Users', ['table' => 'my_users']);
  100. }
  101. /**
  102. * Test the exists() method.
  103. *
  104. * @return void
  105. */
  106. public function testExists()
  107. {
  108. $this->assertFalse($this->_locator->exists('Articles'));
  109. $this->_locator->config('Articles', ['table' => 'articles']);
  110. $this->assertFalse($this->_locator->exists('Articles'));
  111. $this->_locator->get('Articles', ['table' => 'articles']);
  112. $this->assertTrue($this->_locator->exists('Articles'));
  113. }
  114. /**
  115. * Test the exists() method with plugin-prefixed models.
  116. *
  117. * @return void
  118. */
  119. public function testExistsPlugin()
  120. {
  121. $this->assertFalse($this->_locator->exists('Comments'));
  122. $this->assertFalse($this->_locator->exists('TestPlugin.Comments'));
  123. $this->_locator->config('TestPlugin.Comments', ['table' => 'comments']);
  124. $this->assertFalse($this->_locator->exists('Comments'), 'The Comments key should not be populated');
  125. $this->assertFalse($this->_locator->exists('TestPlugin.Comments'), 'The plugin.alias key should not be populated');
  126. $this->_locator->get('TestPlugin.Comments', ['table' => 'comments']);
  127. $this->assertFalse($this->_locator->exists('Comments'), 'The Comments key should not be populated');
  128. $this->assertTrue($this->_locator->exists('TestPlugin.Comments'), 'The plugin.alias key should now be populated');
  129. }
  130. /**
  131. * Test getting instances from the registry.
  132. *
  133. * @return void
  134. */
  135. public function testGet()
  136. {
  137. $result = $this->_locator->get('Articles', [
  138. 'table' => 'my_articles',
  139. ]);
  140. $this->assertInstanceOf('Cake\ORM\Table', $result);
  141. $this->assertEquals('my_articles', $result->table());
  142. $result2 = $this->_locator->get('Articles');
  143. $this->assertSame($result, $result2);
  144. $this->assertEquals('my_articles', $result->table());
  145. }
  146. /**
  147. * Are auto-models instantiated correctly? How about when they have an alias?
  148. *
  149. * @return void
  150. */
  151. public function testGetFallbacks()
  152. {
  153. $result = $this->_locator->get('Droids');
  154. $this->assertInstanceOf('Cake\ORM\Table', $result);
  155. $this->assertEquals('droids', $result->table());
  156. $this->assertEquals('Droids', $result->alias());
  157. $result = $this->_locator->get('R2D2', ['className' => 'Droids']);
  158. $this->assertInstanceOf('Cake\ORM\Table', $result);
  159. $this->assertEquals('droids', $result->table(), 'The table should be derived from the className');
  160. $this->assertEquals('R2D2', $result->alias());
  161. $result = $this->_locator->get('C3P0', ['className' => 'Droids', 'table' => 'rebels']);
  162. $this->assertInstanceOf('Cake\ORM\Table', $result);
  163. $this->assertEquals('rebels', $result->table(), 'The table should be taken from options');
  164. $this->assertEquals('C3P0', $result->alias());
  165. $result = $this->_locator->get('Funky.Chipmunks');
  166. $this->assertInstanceOf('Cake\ORM\Table', $result);
  167. $this->assertEquals('chipmunks', $result->table(), 'The table should be derived from the alias');
  168. $this->assertEquals('Chipmunks', $result->alias());
  169. $result = $this->_locator->get('Awesome', ['className' => 'Funky.Monkies']);
  170. $this->assertInstanceOf('Cake\ORM\Table', $result);
  171. $this->assertEquals('monkies', $result->table(), 'The table should be derived from the classname');
  172. $this->assertEquals('Awesome', $result->alias());
  173. $result = $this->_locator->get('Stuff', ['className' => 'Cake\ORM\Table']);
  174. $this->assertInstanceOf('Cake\ORM\Table', $result);
  175. $this->assertEquals('stuff', $result->table(), 'The table should be derived from the alias');
  176. $this->assertEquals('Stuff', $result->alias());
  177. }
  178. /**
  179. * Test that get() uses config data set with config()
  180. *
  181. * @return void
  182. */
  183. public function testGetWithConfig()
  184. {
  185. $this->_locator->config('Articles', [
  186. 'table' => 'my_articles',
  187. ]);
  188. $result = $this->_locator->get('Articles');
  189. $this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
  190. }
  191. /**
  192. * Test that get() uses config data set with config()
  193. *
  194. * @return void
  195. */
  196. public function testGetWithConnectionName()
  197. {
  198. ConnectionManager::alias('test', 'testing');
  199. $result = $this->_locator->get('Articles', [
  200. 'connectionName' => 'testing'
  201. ]);
  202. $this->assertEquals('articles', $result->table());
  203. $this->assertEquals('test', $result->connection()->configName());
  204. }
  205. /**
  206. * Test that get() uses config data `className` set with config()
  207. *
  208. * @return void
  209. */
  210. public function testGetWithConfigClassName()
  211. {
  212. $this->_locator->config('MyUsersTableAlias', [
  213. 'className' => '\Cake\Test\TestCase\ORM\Locator\MyUsersTable',
  214. ]);
  215. $result = $this->_locator->get('MyUsersTableAlias');
  216. $this->assertInstanceOf('\Cake\Test\TestCase\ORM\Locator\MyUsersTable', $result, 'Should use config() data className option.');
  217. }
  218. /**
  219. * Test get with config throws an exception if the alias exists already.
  220. *
  221. * @expectedException \RuntimeException
  222. * @expectedExceptionMessage You cannot configure "Users", it already exists in the registry.
  223. * @return void
  224. */
  225. public function testGetExistingWithConfigData()
  226. {
  227. $users = $this->_locator->get('Users');
  228. $this->_locator->get('Users', ['table' => 'my_users']);
  229. }
  230. /**
  231. * Test get() can be called several times with the same option without
  232. * throwing an exception.
  233. *
  234. * @return void
  235. */
  236. public function testGetWithSameOption()
  237. {
  238. $result = $this->_locator->get('Users', ['className' => 'Cake\Test\TestCase\ORM\Locator\MyUsersTable']);
  239. $result2 = $this->_locator->get('Users', ['className' => 'Cake\Test\TestCase\ORM\Locator\MyUsersTable']);
  240. $this->assertEquals($result, $result2);
  241. }
  242. /**
  243. * Tests that tables can be instantiated based on conventions
  244. * and using plugin notation
  245. *
  246. * @return void
  247. */
  248. public function testGetWithConventions()
  249. {
  250. $table = $this->_locator->get('articles');
  251. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  252. $table = $this->_locator->get('Articles');
  253. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  254. $table = $this->_locator->get('authors');
  255. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  256. $table = $this->_locator->get('Authors');
  257. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  258. }
  259. /**
  260. * Test get() with plugin syntax aliases
  261. *
  262. * @return void
  263. */
  264. public function testGetPlugin()
  265. {
  266. Plugin::load('TestPlugin');
  267. $table = $this->_locator->get('TestPlugin.TestPluginComments');
  268. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $table);
  269. $this->assertFalse(
  270. $this->_locator->exists('TestPluginComments'),
  271. 'Short form should NOT exist'
  272. );
  273. $this->assertTrue(
  274. $this->_locator->exists('TestPlugin.TestPluginComments'),
  275. 'Long form should exist'
  276. );
  277. $second = $this->_locator->get('TestPlugin.TestPluginComments');
  278. $this->assertSame($table, $second, 'Can fetch long form');
  279. }
  280. /**
  281. * Test get() with same-alias models in different plugins
  282. *
  283. * There should be no internal cache-confusion
  284. *
  285. * @return void
  286. */
  287. public function testGetMultiplePlugins()
  288. {
  289. Plugin::load('TestPlugin');
  290. Plugin::load('TestPluginTwo');
  291. $app = $this->_locator->get('Comments');
  292. $plugin1 = $this->_locator->get('TestPlugin.Comments');
  293. $plugin2 = $this->_locator->get('TestPluginTwo.Comments');
  294. $this->assertInstanceOf('Cake\ORM\Table', $app, 'Should be an app table instance');
  295. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $plugin1, 'Should be a plugin 1 table instance');
  296. $this->assertInstanceOf('TestPluginTwo\Model\Table\CommentsTable', $plugin2, 'Should be a plugin 2 table instance');
  297. $plugin2 = $this->_locator->get('TestPluginTwo.Comments');
  298. $plugin1 = $this->_locator->get('TestPlugin.Comments');
  299. $app = $this->_locator->get('Comments');
  300. $this->assertInstanceOf('Cake\ORM\Table', $app, 'Should still be an app table instance');
  301. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $plugin1, 'Should still be a plugin 1 table instance');
  302. $this->assertInstanceOf('TestPluginTwo\Model\Table\CommentsTable', $plugin2, 'Should still be a plugin 2 table instance');
  303. }
  304. /**
  305. * Test get() with plugin aliases + className option.
  306. *
  307. * @return void
  308. */
  309. public function testGetPluginWithClassNameOption()
  310. {
  311. Plugin::load('TestPlugin');
  312. $table = $this->_locator->get('Comments', [
  313. 'className' => 'TestPlugin.TestPluginComments',
  314. ]);
  315. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  316. $this->assertInstanceOf($class, $table);
  317. $this->assertFalse($this->_locator->exists('TestPluginComments'), 'Class name should not exist');
  318. $this->assertFalse($this->_locator->exists('TestPlugin.TestPluginComments'), 'Full class alias should not exist');
  319. $this->assertTrue($this->_locator->exists('Comments'), 'Class name should exist');
  320. $second = $this->_locator->get('Comments');
  321. $this->assertSame($table, $second);
  322. }
  323. /**
  324. * Test get() with full namespaced classname
  325. *
  326. * @return void
  327. */
  328. public function testGetPluginWithFullNamespaceName()
  329. {
  330. Plugin::load('TestPlugin');
  331. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  332. $table = $this->_locator->get('Comments', [
  333. 'className' => $class,
  334. ]);
  335. $this->assertInstanceOf($class, $table);
  336. $this->assertFalse($this->_locator->exists('TestPluginComments'), 'Class name should not exist');
  337. $this->assertFalse($this->_locator->exists('TestPlugin.TestPluginComments'), 'Full class alias should not exist');
  338. $this->assertTrue($this->_locator->exists('Comments'), 'Class name should exist');
  339. }
  340. /**
  341. * Tests that table options can be pre-configured for the factory method
  342. *
  343. * @return void
  344. */
  345. public function testConfigAndBuild()
  346. {
  347. $this->_locator->clear();
  348. $map = $this->_locator->config();
  349. $this->assertEquals([], $map);
  350. $connection = ConnectionManager::get('test', false);
  351. $options = ['connection' => $connection];
  352. $this->_locator->config('users', $options);
  353. $map = $this->_locator->config();
  354. $this->assertEquals(['users' => $options], $map);
  355. $this->assertEquals($options, $this->_locator->config('users'));
  356. $schema = ['id' => ['type' => 'rubbish']];
  357. $options += ['schema' => $schema];
  358. $this->_locator->config('users', $options);
  359. $table = $this->_locator->get('users', ['table' => 'users']);
  360. $this->assertInstanceOf('Cake\ORM\Table', $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()->getColumnType('id'));
  366. $this->_locator->clear();
  367. $this->assertEmpty($this->_locator->config());
  368. $this->_locator->config('users', $options);
  369. $table = $this->_locator->get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
  370. $this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
  371. $this->assertEquals('users', $table->table());
  372. $this->assertEquals('users', $table->alias());
  373. $this->assertSame($connection, $table->connection());
  374. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  375. $this->assertEquals($schema['id']['type'], $table->schema()->getColumnType('id'));
  376. }
  377. /**
  378. * Tests that table options can be pre-configured with a single validator
  379. *
  380. * @return void
  381. */
  382. public function testConfigWithSingleValidator()
  383. {
  384. $validator = new Validator();
  385. $this->_locator->config('users', ['validator' => $validator]);
  386. $table = $this->_locator->get('users');
  387. $this->assertSame($table->validator('default'), $validator);
  388. }
  389. /**
  390. * Tests that table options can be pre-configured with multiple validators
  391. *
  392. * @return void
  393. */
  394. public function testConfigWithMultipleValidators()
  395. {
  396. $validator1 = new Validator();
  397. $validator2 = new Validator();
  398. $validator3 = new Validator();
  399. $this->_locator->config('users', [
  400. 'validator' => [
  401. 'default' => $validator1,
  402. 'secondary' => $validator2,
  403. 'tertiary' => $validator3,
  404. ]
  405. ]);
  406. $table = $this->_locator->get('users');
  407. $this->assertSame($table->validator('default'), $validator1);
  408. $this->assertSame($table->validator('secondary'), $validator2);
  409. $this->assertSame($table->validator('tertiary'), $validator3);
  410. }
  411. /**
  412. * Test setting an instance.
  413. *
  414. * @return void
  415. */
  416. public function testSet()
  417. {
  418. $mock = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  419. $this->assertSame($mock, $this->_locator->set('Articles', $mock));
  420. $this->assertSame($mock, $this->_locator->get('Articles'));
  421. }
  422. /**
  423. * Test setting an instance with plugin syntax aliases
  424. *
  425. * @return void
  426. */
  427. public function testSetPlugin()
  428. {
  429. Plugin::load('TestPlugin');
  430. $mock = $this->getMockBuilder('TestPlugin\Model\Table\CommentsTable')->getMock();
  431. $this->assertSame($mock, $this->_locator->set('TestPlugin.Comments', $mock));
  432. $this->assertSame($mock, $this->_locator->get('TestPlugin.Comments'));
  433. }
  434. /**
  435. * Tests genericInstances
  436. *
  437. * @return void
  438. */
  439. public function testGenericInstances()
  440. {
  441. $foos = $this->_locator->get('Foos');
  442. $bars = $this->_locator->get('Bars');
  443. $this->_locator->get('Articles');
  444. $expected = ['Foos' => $foos, 'Bars' => $bars];
  445. $this->assertEquals($expected, $this->_locator->genericInstances());
  446. }
  447. /**
  448. * Tests remove an instance
  449. *
  450. * @return void
  451. */
  452. public function testRemove()
  453. {
  454. $first = $this->_locator->get('Comments');
  455. $this->assertTrue($this->_locator->exists('Comments'));
  456. $this->_locator->remove('Comments');
  457. $this->assertFalse($this->_locator->exists('Comments'));
  458. $second = $this->_locator->get('Comments');
  459. $this->assertNotSame($first, $second, 'Should be different objects, as the reference to the first was destroyed');
  460. $this->assertTrue($this->_locator->exists('Comments'));
  461. }
  462. /**
  463. * testRemovePlugin
  464. *
  465. * Removing a plugin-prefixed model should not affect any other
  466. * plugin-prefixed model, or app model.
  467. * Removing an app model should not affect any other
  468. * plugin-prefixed model.
  469. *
  470. * @return void
  471. */
  472. public function testRemovePlugin()
  473. {
  474. Plugin::load('TestPlugin');
  475. Plugin::load('TestPluginTwo');
  476. $app = $this->_locator->get('Comments');
  477. $this->_locator->get('TestPlugin.Comments');
  478. $plugin = $this->_locator->get('TestPluginTwo.Comments');
  479. $this->assertTrue($this->_locator->exists('Comments'));
  480. $this->assertTrue($this->_locator->exists('TestPlugin.Comments'));
  481. $this->assertTrue($this->_locator->exists('TestPluginTwo.Comments'));
  482. $this->_locator->remove('TestPlugin.Comments');
  483. $this->assertTrue($this->_locator->exists('Comments'));
  484. $this->assertFalse($this->_locator->exists('TestPlugin.Comments'));
  485. $this->assertTrue($this->_locator->exists('TestPluginTwo.Comments'));
  486. $app2 = $this->_locator->get('Comments');
  487. $plugin2 = $this->_locator->get('TestPluginTwo.Comments');
  488. $this->assertSame($app, $app2, 'Should be the same Comments object');
  489. $this->assertSame($plugin, $plugin2, 'Should be the same TestPluginTwo.Comments object');
  490. $this->_locator->remove('Comments');
  491. $this->assertFalse($this->_locator->exists('Comments'));
  492. $this->assertFalse($this->_locator->exists('TestPlugin.Comments'));
  493. $this->assertTrue($this->_locator->exists('TestPluginTwo.Comments'));
  494. $plugin3 = $this->_locator->get('TestPluginTwo.Comments');
  495. $this->assertSame($plugin, $plugin3, 'Should be the same TestPluginTwo.Comments object');
  496. }
  497. }