TableLocatorTest.php 20 KB

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