TableLocatorTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. use TestApp\Infrastructure\Table\AddressesTable;
  23. use TestApp\Model\Table\ArticlesTable;
  24. use TestPlugin\Infrastructure\Table\AddressesTable as PluginAddressesTable;
  25. /**
  26. * Used to test correct class is instantiated when using $this->_locator->get();
  27. */
  28. class MyUsersTable extends Table
  29. {
  30. /**
  31. * Overrides default table name
  32. *
  33. * @var string
  34. */
  35. protected $_table = 'users';
  36. }
  37. /**
  38. * Test case for TableLocator
  39. */
  40. class TableLocatorTest extends TestCase
  41. {
  42. /**
  43. * TableLocator instance.
  44. *
  45. * @var \Cake\ORM\Locator\TableLocator
  46. */
  47. protected $_locator;
  48. /**
  49. * setup
  50. *
  51. * @return void
  52. */
  53. public function setUp()
  54. {
  55. parent::setUp();
  56. static::setAppNamespace();
  57. $this->_locator = new TableLocator();
  58. }
  59. /**
  60. * tearDown
  61. *
  62. * @return void
  63. */
  64. public function tearDown()
  65. {
  66. $this->clearPlugins();
  67. parent::tearDown();
  68. }
  69. /**
  70. * Test config() method.
  71. *
  72. * @group deprecated
  73. * @return void
  74. */
  75. public function testConfigDeprecated()
  76. {
  77. $this->deprecated(function () {
  78. $this->assertEquals([], $this->_locator->config('Tests'));
  79. $data = [
  80. 'connection' => 'testing',
  81. 'entityClass' => 'TestApp\Model\Entity\Article',
  82. ];
  83. $result = $this->_locator->config('Tests', $data);
  84. $this->assertEquals($data, $result, 'Returns config data.');
  85. $result = $this->_locator->config();
  86. $expected = ['Tests' => $data];
  87. $this->assertEquals($expected, $result);
  88. });
  89. }
  90. /**
  91. * Test getConfig() method.
  92. *
  93. * @return void
  94. */
  95. public function testGetConfig()
  96. {
  97. $this->assertEquals([], $this->_locator->getConfig('Tests'));
  98. $data = [
  99. 'connection' => 'testing',
  100. 'entityClass' => 'TestApp\Model\Entity\Article',
  101. ];
  102. $result = $this->_locator->setConfig('Tests', $data);
  103. $this->assertSame($this->_locator, $result, 'Returns locator');
  104. $result = $this->_locator->getConfig();
  105. $expected = ['Tests' => $data];
  106. $this->assertEquals($expected, $result);
  107. }
  108. /**
  109. * Test getConfig() method with plugin syntax aliases
  110. *
  111. * @return void
  112. */
  113. public function testConfigPlugin()
  114. {
  115. $this->loadPlugins(['TestPlugin']);
  116. $data = [
  117. 'connection' => 'testing',
  118. 'entityClass' => 'TestPlugin\Model\Entity\Comment',
  119. ];
  120. $result = $this->_locator->setConfig('TestPlugin.TestPluginComments', $data);
  121. $this->assertSame($this->_locator, $result, 'Returns locator');
  122. }
  123. /**
  124. * Test calling getConfig() on existing instances throws an error.
  125. *
  126. * @return void
  127. */
  128. public function testConfigOnDefinedInstance()
  129. {
  130. $this->expectException(\RuntimeException::class);
  131. $this->expectExceptionMessage('You cannot configure "Users", it has already been constructed.');
  132. $users = $this->_locator->get('Users');
  133. $this->_locator->setConfig('Users', ['table' => 'my_users']);
  134. }
  135. /**
  136. * Test the exists() method.
  137. *
  138. * @return void
  139. */
  140. public function testExists()
  141. {
  142. $this->assertFalse($this->_locator->exists('Articles'));
  143. $this->_locator->setConfig('Articles', ['table' => 'articles']);
  144. $this->assertFalse($this->_locator->exists('Articles'));
  145. $this->_locator->get('Articles', ['table' => 'articles']);
  146. $this->assertTrue($this->_locator->exists('Articles'));
  147. }
  148. /**
  149. * Test the exists() method with plugin-prefixed models.
  150. *
  151. * @return void
  152. */
  153. public function testExistsPlugin()
  154. {
  155. $this->assertFalse($this->_locator->exists('Comments'));
  156. $this->assertFalse($this->_locator->exists('TestPlugin.Comments'));
  157. $this->_locator->setConfig('TestPlugin.Comments', ['table' => 'comments']);
  158. $this->assertFalse($this->_locator->exists('Comments'), 'The Comments key should not be populated');
  159. $this->assertFalse($this->_locator->exists('TestPlugin.Comments'), 'The plugin.alias key should not be populated');
  160. $this->_locator->get('TestPlugin.Comments', ['table' => 'comments']);
  161. $this->assertFalse($this->_locator->exists('Comments'), 'The Comments key should not be populated');
  162. $this->assertTrue($this->_locator->exists('TestPlugin.Comments'), 'The plugin.alias key should now be populated');
  163. }
  164. /**
  165. * Test getting instances from the registry.
  166. *
  167. * @return void
  168. */
  169. public function testGet()
  170. {
  171. $result = $this->_locator->get('Articles', [
  172. 'table' => 'my_articles',
  173. ]);
  174. $this->assertInstanceOf('Cake\ORM\Table', $result);
  175. $this->assertEquals('my_articles', $result->getTable());
  176. $result2 = $this->_locator->get('Articles');
  177. $this->assertSame($result, $result2);
  178. $this->assertEquals('my_articles', $result->getTable());
  179. $this->assertSame($this->_locator, $result->associations()->getTableLocator());
  180. }
  181. /**
  182. * Are auto-models instantiated correctly? How about when they have an alias?
  183. *
  184. * @return void
  185. */
  186. public function testGetFallbacks()
  187. {
  188. $result = $this->_locator->get('Droids');
  189. $this->assertInstanceOf('Cake\ORM\Table', $result);
  190. $this->assertEquals('droids', $result->getTable());
  191. $this->assertEquals('Droids', $result->getAlias());
  192. $result = $this->_locator->get('R2D2', ['className' => 'Droids']);
  193. $this->assertInstanceOf('Cake\ORM\Table', $result);
  194. $this->assertEquals('droids', $result->getTable(), 'The table should be derived from the className');
  195. $this->assertEquals('R2D2', $result->getAlias());
  196. $result = $this->_locator->get('C3P0', ['className' => 'Droids', 'table' => 'rebels']);
  197. $this->assertInstanceOf('Cake\ORM\Table', $result);
  198. $this->assertEquals('rebels', $result->getTable(), 'The table should be taken from options');
  199. $this->assertEquals('C3P0', $result->getAlias());
  200. $result = $this->_locator->get('Funky.Chipmunks');
  201. $this->assertInstanceOf('Cake\ORM\Table', $result);
  202. $this->assertEquals('chipmunks', $result->getTable(), 'The table should be derived from the alias');
  203. $this->assertEquals('Chipmunks', $result->getAlias());
  204. $result = $this->_locator->get('Awesome', ['className' => 'Funky.Monkies']);
  205. $this->assertInstanceOf('Cake\ORM\Table', $result);
  206. $this->assertEquals('monkies', $result->getTable(), 'The table should be derived from the classname');
  207. $this->assertEquals('Awesome', $result->getAlias());
  208. $result = $this->_locator->get('Stuff', ['className' => 'Cake\ORM\Table']);
  209. $this->assertInstanceOf('Cake\ORM\Table', $result);
  210. $this->assertEquals('stuff', $result->getTable(), 'The table should be derived from the alias');
  211. $this->assertEquals('Stuff', $result->getAlias());
  212. }
  213. /**
  214. * Test that get() uses config data set with getConfig()
  215. *
  216. * @return void
  217. */
  218. public function testGetWithgetConfig()
  219. {
  220. $this->_locator->setConfig('Articles', [
  221. 'table' => 'my_articles',
  222. ]);
  223. $result = $this->_locator->get('Articles');
  224. $this->assertEquals('my_articles', $result->getTable(), 'Should use getConfig() data.');
  225. }
  226. /**
  227. * Test that get() uses config data set with getConfig()
  228. *
  229. * @return void
  230. */
  231. public function testGetWithConnectionName()
  232. {
  233. ConnectionManager::alias('test', 'testing');
  234. $result = $this->_locator->get('Articles', [
  235. 'connectionName' => 'testing',
  236. ]);
  237. $this->assertEquals('articles', $result->getTable());
  238. $this->assertEquals('test', $result->getConnection()->configName());
  239. }
  240. /**
  241. * Test that get() uses config data `className` set with getConfig()
  242. *
  243. * @return void
  244. */
  245. public function testGetWithConfigClassName()
  246. {
  247. $this->_locator->setConfig('MyUsersTableAlias', [
  248. 'className' => '\Cake\Test\TestCase\ORM\Locator\MyUsersTable',
  249. ]);
  250. $result = $this->_locator->get('MyUsersTableAlias');
  251. $this->assertInstanceOf('\Cake\Test\TestCase\ORM\Locator\MyUsersTable', $result, 'Should use getConfig() data className option.');
  252. }
  253. /**
  254. * Test get with config throws an exception if the alias exists already.
  255. *
  256. * @return void
  257. */
  258. public function testGetExistingWithConfigData()
  259. {
  260. $this->expectException(\RuntimeException::class);
  261. $this->expectExceptionMessage('You cannot configure "Users", it already exists in the registry.');
  262. $users = $this->_locator->get('Users');
  263. $this->_locator->get('Users', ['table' => 'my_users']);
  264. }
  265. /**
  266. * Test get() can be called several times with the same option without
  267. * throwing an exception.
  268. *
  269. * @return void
  270. */
  271. public function testGetWithSameOption()
  272. {
  273. $result = $this->_locator->get('Users', ['className' => 'Cake\Test\TestCase\ORM\Locator\MyUsersTable']);
  274. $result2 = $this->_locator->get('Users', ['className' => 'Cake\Test\TestCase\ORM\Locator\MyUsersTable']);
  275. $this->assertEquals($result, $result2);
  276. }
  277. /**
  278. * Tests that tables can be instantiated based on conventions
  279. * and using plugin notation
  280. *
  281. * @return void
  282. */
  283. public function testGetWithConventions()
  284. {
  285. $table = $this->_locator->get('articles');
  286. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  287. $table = $this->_locator->get('Articles');
  288. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  289. $table = $this->_locator->get('authors');
  290. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  291. $table = $this->_locator->get('Authors');
  292. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  293. }
  294. /**
  295. * Test get() with plugin syntax aliases
  296. *
  297. * @return void
  298. */
  299. public function testGetPlugin()
  300. {
  301. $this->loadPlugins(['TestPlugin']);
  302. $table = $this->_locator->get('TestPlugin.TestPluginComments');
  303. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $table);
  304. $this->assertFalse(
  305. $this->_locator->exists('TestPluginComments'),
  306. 'Short form should NOT exist'
  307. );
  308. $this->assertTrue(
  309. $this->_locator->exists('TestPlugin.TestPluginComments'),
  310. 'Long form should exist'
  311. );
  312. $second = $this->_locator->get('TestPlugin.TestPluginComments');
  313. $this->assertSame($table, $second, 'Can fetch long form');
  314. }
  315. /**
  316. * Test get() with same-alias models in different plugins
  317. *
  318. * There should be no internal cache-confusion
  319. *
  320. * @return void
  321. */
  322. public function testGetMultiplePlugins()
  323. {
  324. $this->loadPlugins(['TestPlugin', 'TestPluginTwo']);
  325. $app = $this->_locator->get('Comments');
  326. $plugin1 = $this->_locator->get('TestPlugin.Comments');
  327. $plugin2 = $this->_locator->get('TestPluginTwo.Comments');
  328. $this->assertInstanceOf('Cake\ORM\Table', $app, 'Should be an app table instance');
  329. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $plugin1, 'Should be a plugin 1 table instance');
  330. $this->assertInstanceOf('TestPluginTwo\Model\Table\CommentsTable', $plugin2, 'Should be a plugin 2 table instance');
  331. $plugin2 = $this->_locator->get('TestPluginTwo.Comments');
  332. $plugin1 = $this->_locator->get('TestPlugin.Comments');
  333. $app = $this->_locator->get('Comments');
  334. $this->assertInstanceOf('Cake\ORM\Table', $app, 'Should still be an app table instance');
  335. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $plugin1, 'Should still be a plugin 1 table instance');
  336. $this->assertInstanceOf('TestPluginTwo\Model\Table\CommentsTable', $plugin2, 'Should still be a plugin 2 table instance');
  337. }
  338. /**
  339. * Test get() with plugin aliases + className option.
  340. *
  341. * @return void
  342. */
  343. public function testGetPluginWithClassNameOption()
  344. {
  345. $this->loadPlugins(['TestPlugin']);
  346. $table = $this->_locator->get('Comments', [
  347. 'className' => 'TestPlugin.TestPluginComments',
  348. ]);
  349. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  350. $this->assertInstanceOf($class, $table);
  351. $this->assertFalse($this->_locator->exists('TestPluginComments'), 'Class name should not exist');
  352. $this->assertFalse($this->_locator->exists('TestPlugin.TestPluginComments'), 'Full class alias should not exist');
  353. $this->assertTrue($this->_locator->exists('Comments'), 'Class name should exist');
  354. $second = $this->_locator->get('Comments');
  355. $this->assertSame($table, $second);
  356. }
  357. /**
  358. * Test get() with full namespaced classname
  359. *
  360. * @return void
  361. */
  362. public function testGetPluginWithFullNamespaceName()
  363. {
  364. $this->loadPlugins(['TestPlugin']);
  365. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  366. $table = $this->_locator->get('Comments', [
  367. 'className' => $class,
  368. ]);
  369. $this->assertInstanceOf($class, $table);
  370. $this->assertFalse($this->_locator->exists('TestPluginComments'), 'Class name should not exist');
  371. $this->assertFalse($this->_locator->exists('TestPlugin.TestPluginComments'), 'Full class alias should not exist');
  372. $this->assertTrue($this->_locator->exists('Comments'), 'Class name should exist');
  373. }
  374. /**
  375. * Tests that table options can be pre-configured for the factory method
  376. *
  377. * @return void
  378. */
  379. public function testConfigAndBuild()
  380. {
  381. $this->_locator->clear();
  382. $map = $this->_locator->getConfig();
  383. $this->assertEquals([], $map);
  384. $connection = ConnectionManager::get('test', false);
  385. $options = ['connection' => $connection];
  386. $this->_locator->setConfig('users', $options);
  387. $map = $this->_locator->getConfig();
  388. $this->assertEquals(['users' => $options], $map);
  389. $this->assertEquals($options, $this->_locator->getConfig('users'));
  390. $schema = ['id' => ['type' => 'rubbish']];
  391. $options += ['schema' => $schema];
  392. $this->_locator->setConfig('users', $options);
  393. $table = $this->_locator->get('users', ['table' => 'users']);
  394. $this->assertInstanceOf('Cake\ORM\Table', $table);
  395. $this->assertEquals('users', $table->getTable());
  396. $this->assertEquals('users', $table->getAlias());
  397. $this->assertSame($connection, $table->getConnection());
  398. $this->assertEquals(array_keys($schema), $table->getSchema()->columns());
  399. $this->assertEquals($schema['id']['type'], $table->getSchema()->getColumnType('id'));
  400. $this->_locator->clear();
  401. $this->assertEmpty($this->_locator->getConfig());
  402. $this->_locator->setConfig('users', $options);
  403. $table = $this->_locator->get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
  404. $this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
  405. $this->assertEquals('users', $table->getTable());
  406. $this->assertEquals('users', $table->getAlias());
  407. $this->assertSame($connection, $table->getConnection());
  408. $this->assertEquals(array_keys($schema), $table->getSchema()->columns());
  409. $this->assertEquals($schema['id']['type'], $table->getSchema()->getColumnType('id'));
  410. }
  411. /**
  412. * Tests that table options can be pre-configured with a single validator
  413. *
  414. * @return void
  415. */
  416. public function testConfigWithSingleValidator()
  417. {
  418. $validator = new Validator();
  419. $this->_locator->setConfig('users', ['validator' => $validator]);
  420. $table = $this->_locator->get('users');
  421. $this->assertSame($table->getValidator('default'), $validator);
  422. }
  423. /**
  424. * Tests that table options can be pre-configured with multiple validators
  425. *
  426. * @return void
  427. */
  428. public function testConfigWithMultipleValidators()
  429. {
  430. $validator1 = new Validator();
  431. $validator2 = new Validator();
  432. $validator3 = new Validator();
  433. $this->_locator->setConfig('users', [
  434. 'validator' => [
  435. 'default' => $validator1,
  436. 'secondary' => $validator2,
  437. 'tertiary' => $validator3,
  438. ],
  439. ]);
  440. $table = $this->_locator->get('users');
  441. $this->assertSame($table->getValidator('default'), $validator1);
  442. $this->assertSame($table->getValidator('secondary'), $validator2);
  443. $this->assertSame($table->getValidator('tertiary'), $validator3);
  444. }
  445. /**
  446. * Test setting an instance.
  447. *
  448. * @return void
  449. */
  450. public function testSet()
  451. {
  452. $mock = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  453. $this->assertSame($mock, $this->_locator->set('Articles', $mock));
  454. $this->assertSame($mock, $this->_locator->get('Articles'));
  455. }
  456. /**
  457. * Test setting an instance with plugin syntax aliases
  458. *
  459. * @return void
  460. */
  461. public function testSetPlugin()
  462. {
  463. $this->loadPlugins(['TestPlugin']);
  464. $mock = $this->getMockBuilder('TestPlugin\Model\Table\CommentsTable')->getMock();
  465. $this->assertSame($mock, $this->_locator->set('TestPlugin.Comments', $mock));
  466. $this->assertSame($mock, $this->_locator->get('TestPlugin.Comments'));
  467. }
  468. /**
  469. * Tests genericInstances
  470. *
  471. * @return void
  472. */
  473. public function testGenericInstances()
  474. {
  475. $foos = $this->_locator->get('Foos');
  476. $bars = $this->_locator->get('Bars');
  477. $this->_locator->get('Articles');
  478. $expected = ['Foos' => $foos, 'Bars' => $bars];
  479. $this->assertEquals($expected, $this->_locator->genericInstances());
  480. }
  481. /**
  482. * Tests remove an instance
  483. *
  484. * @return void
  485. */
  486. public function testRemove()
  487. {
  488. $first = $this->_locator->get('Comments');
  489. $this->assertTrue($this->_locator->exists('Comments'));
  490. $this->_locator->remove('Comments');
  491. $this->assertFalse($this->_locator->exists('Comments'));
  492. $second = $this->_locator->get('Comments');
  493. $this->assertNotSame($first, $second, 'Should be different objects, as the reference to the first was destroyed');
  494. $this->assertTrue($this->_locator->exists('Comments'));
  495. }
  496. /**
  497. * testRemovePlugin
  498. *
  499. * Removing a plugin-prefixed model should not affect any other
  500. * plugin-prefixed model, or app model.
  501. * Removing an app model should not affect any other
  502. * plugin-prefixed model.
  503. *
  504. * @return void
  505. */
  506. public function testRemovePlugin()
  507. {
  508. $this->loadPlugins(['TestPlugin', 'TestPluginTwo']);
  509. $app = $this->_locator->get('Comments');
  510. $this->_locator->get('TestPlugin.Comments');
  511. $plugin = $this->_locator->get('TestPluginTwo.Comments');
  512. $this->assertTrue($this->_locator->exists('Comments'));
  513. $this->assertTrue($this->_locator->exists('TestPlugin.Comments'));
  514. $this->assertTrue($this->_locator->exists('TestPluginTwo.Comments'));
  515. $this->_locator->remove('TestPlugin.Comments');
  516. $this->assertTrue($this->_locator->exists('Comments'));
  517. $this->assertFalse($this->_locator->exists('TestPlugin.Comments'));
  518. $this->assertTrue($this->_locator->exists('TestPluginTwo.Comments'));
  519. $app2 = $this->_locator->get('Comments');
  520. $plugin2 = $this->_locator->get('TestPluginTwo.Comments');
  521. $this->assertSame($app, $app2, 'Should be the same Comments object');
  522. $this->assertSame($plugin, $plugin2, 'Should be the same TestPluginTwo.Comments object');
  523. $this->_locator->remove('Comments');
  524. $this->assertFalse($this->_locator->exists('Comments'));
  525. $this->assertFalse($this->_locator->exists('TestPlugin.Comments'));
  526. $this->assertTrue($this->_locator->exists('TestPluginTwo.Comments'));
  527. $plugin3 = $this->_locator->get('TestPluginTwo.Comments');
  528. $this->assertSame($plugin, $plugin3, 'Should be the same TestPluginTwo.Comments object');
  529. }
  530. /**
  531. * testCustomLocation
  532. *
  533. * Tests that the correct table is returned when non-standard namespace is defined.
  534. *
  535. * @return void
  536. */
  537. public function testCustomLocation()
  538. {
  539. $locator = new TableLocator(['Infrastructure/Table']);
  540. $table = $locator->get('Addresses');
  541. $this->assertInstanceOf(AddressesTable::class, $table);
  542. }
  543. /**
  544. * testCustomLocationPlugin
  545. *
  546. * Tests that the correct plugin table is returned when non-standard namespace is defined.
  547. *
  548. * @return void
  549. */
  550. public function testCustomLocationPlugin()
  551. {
  552. $locator = new TableLocator(['Infrastructure/Table']);
  553. $table = $locator->get('TestPlugin.Addresses');
  554. $this->assertInstanceOf(PluginAddressesTable::class, $table);
  555. }
  556. /**
  557. * testCustomLocationDefaultWhenNone
  558. *
  559. * Tests that the default table is returned when no namespace is defined.
  560. *
  561. * @return void
  562. */
  563. public function testCustomLocationDefaultWhenNone()
  564. {
  565. $locator = new TableLocator([]);
  566. $table = $locator->get('Addresses');
  567. $this->assertInstanceOf(Table::class, $table);
  568. }
  569. /**
  570. * testCustomLocationDefaultWhenMissing
  571. *
  572. * Tests that the default table is returned when the class cannot be found in a non-standard namespace.
  573. *
  574. * @return void
  575. */
  576. public function testCustomLocationDefaultWhenMissing()
  577. {
  578. $locator = new TableLocator(['Infrastructure/Table']);
  579. $table = $locator->get('Articles');
  580. $this->assertInstanceOf(Table::class, $table);
  581. }
  582. /**
  583. * testCustomLocationMultiple
  584. *
  585. * Tests that the correct table is returned when multiple namespaces are defined.
  586. *
  587. * @return void
  588. */
  589. public function testCustomLocationMultiple()
  590. {
  591. $locator = new TableLocator([
  592. 'Infrastructure/Table',
  593. 'Model/Table',
  594. ]);
  595. $table = $locator->get('Articles');
  596. $this->assertInstanceOf(Table::class, $table);
  597. }
  598. /**
  599. * testAddLocation
  600. *
  601. * Tests that adding a namespace takes effect.
  602. *
  603. * @return void
  604. */
  605. public function testAddLocation()
  606. {
  607. $locator = new TableLocator([]);
  608. $table = $locator->get('Addresses');
  609. $this->assertInstanceOf(Table::class, $table);
  610. $locator->clear();
  611. $locator->addLocation('Infrastructure/Table');
  612. $table = $locator->get('Addresses');
  613. $this->assertInstanceOf(AddressesTable::class, $table);
  614. }
  615. }