TableRegistryTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. /**
  23. * Used to test correct class is instantiated when using TableRegistry::get();
  24. */
  25. class MyUsersTable extends Table {
  26. /**
  27. * Overrides default table name
  28. *
  29. * @var string
  30. */
  31. protected $_table = 'users';
  32. }
  33. /**
  34. * Test case for TableRegistry
  35. */
  36. class TableRegistryTest extends TestCase {
  37. /**
  38. * setup
  39. *
  40. * @return void
  41. */
  42. public function setUp() {
  43. parent::setUp();
  44. Configure::write('App.namespace', 'TestApp');
  45. }
  46. /**
  47. * tear down
  48. *
  49. * @return void
  50. */
  51. public function tearDown() {
  52. parent::tearDown();
  53. TableRegistry::clear();
  54. }
  55. /**
  56. * Test config() method.
  57. *
  58. * @return void
  59. */
  60. public function testConfig() {
  61. $this->assertEquals([], TableRegistry::config('Tests'));
  62. $data = [
  63. 'connection' => 'testing',
  64. 'entityClass' => 'TestApp\Model\Entity\Article',
  65. ];
  66. $result = TableRegistry::config('Tests', $data);
  67. $this->assertEquals($data, $result, 'Returns config data.');
  68. $result = TableRegistry::config();
  69. $expected = ['Tests' => $data];
  70. $this->assertEquals($expected, $result);
  71. }
  72. /**
  73. * Test config() method with plugin syntax aliases
  74. *
  75. * @return void
  76. */
  77. public function testConfigPlugin() {
  78. Plugin::load('TestPlugin');
  79. $data = [
  80. 'connection' => 'testing',
  81. 'entityClass' => 'TestPlugin\Model\Entity\Comment',
  82. ];
  83. $result = TableRegistry::config('TestPlugin.TestPluginComments', $data);
  84. $this->assertEquals($data, $result, 'Returns config data.');
  85. $result = TableRegistry::config();
  86. $expected = ['TestPluginComments' => $data];
  87. $this->assertEquals($expected, $result);
  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. $users = TableRegistry::get('Users');
  98. TableRegistry::config('Users', ['table' => 'my_users']);
  99. }
  100. /**
  101. * Test the exists() method.
  102. *
  103. * @return void
  104. */
  105. public function testExists() {
  106. $this->assertFalse(TableRegistry::exists('Articles'));
  107. TableRegistry::config('Articles', ['table' => 'articles']);
  108. $this->assertFalse(TableRegistry::exists('Articles'));
  109. TableRegistry::get('Articles', ['table' => 'articles']);
  110. $this->assertTrue(TableRegistry::exists('Articles'));
  111. }
  112. /**
  113. * Test getting instances from the registry.
  114. *
  115. * @return void
  116. */
  117. public function testGet() {
  118. $result = TableRegistry::get('Articles', [
  119. 'table' => 'my_articles',
  120. ]);
  121. $this->assertInstanceOf('Cake\ORM\Table', $result);
  122. $this->assertEquals('my_articles', $result->table());
  123. $result2 = TableRegistry::get('Articles');
  124. $this->assertSame($result, $result2);
  125. $this->assertEquals('my_articles', $result->table());
  126. }
  127. /**
  128. * Test that get() uses config data set with config()
  129. *
  130. * @return void
  131. */
  132. public function testGetWithConfig() {
  133. TableRegistry::config('Articles', [
  134. 'table' => 'my_articles',
  135. ]);
  136. $result = TableRegistry::get('Articles');
  137. $this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
  138. }
  139. /**
  140. * Test get with config throws an exception if the alias exists already.
  141. *
  142. * @expectedException \RuntimeException
  143. * @expectedExceptionMessage You cannot configure "Users", it already exists in the registry.
  144. * @return void
  145. */
  146. public function testGetExistingWithConfigData() {
  147. $users = TableRegistry::get('Users');
  148. TableRegistry::get('Users', ['table' => 'my_users']);
  149. }
  150. /**
  151. * Test get() can be called several times with the same option without
  152. * throwing an exception.
  153. *
  154. * @return void
  155. */
  156. public function testGetWithSameOption() {
  157. $result = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
  158. $result2 = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
  159. $this->assertEquals($result, $result2);
  160. }
  161. /**
  162. * Tests that tables can be instantiated based on conventions
  163. * and using plugin notation
  164. *
  165. * @return void
  166. */
  167. public function testGetWithConventions() {
  168. $table = TableRegistry::get('articles');
  169. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  170. $table = TableRegistry::get('Articles');
  171. $this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
  172. $table = TableRegistry::get('authors');
  173. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  174. $table = TableRegistry::get('Authors');
  175. $this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
  176. }
  177. /**
  178. * Test get() with plugin syntax aliases
  179. *
  180. * @return void
  181. */
  182. public function testGetPlugin() {
  183. Plugin::load('TestPlugin');
  184. $table = TableRegistry::get('TestPlugin.TestPluginComments', ['connection' => 'test']);
  185. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  186. $this->assertInstanceOf($class, $table);
  187. $this->assertTrue(
  188. TableRegistry::exists('TestPluginComments'),
  189. 'Short form should exist'
  190. );
  191. $this->assertTrue(
  192. TableRegistry::exists('TestPlugin.TestPluginComments'),
  193. 'Long form should exist'
  194. );
  195. $second = TableRegistry::get('TestPlugin.TestPluginComments');
  196. $this->assertSame($table, $second, 'Can fetch long form');
  197. $second = TableRegistry::get('TestPluginComments');
  198. $this->assertSame($table, $second);
  199. }
  200. /**
  201. * Test get() with plugin aliases + className option.
  202. *
  203. * @return void
  204. */
  205. public function testGetPluginWithClassNameOption() {
  206. Plugin::load('TestPlugin');
  207. $table = TableRegistry::get('Comments', [
  208. 'className' => 'TestPlugin.TestPluginComments',
  209. 'connection' => 'test'
  210. ]);
  211. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  212. $this->assertInstanceOf($class, $table);
  213. $this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
  214. $this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
  215. $second = TableRegistry::get('Comments');
  216. $this->assertSame($table, $second);
  217. }
  218. /**
  219. * Test get() with full namespaced classname
  220. *
  221. * @return void
  222. */
  223. public function testGetPluginWithFullNamespaceName() {
  224. Plugin::load('TestPlugin');
  225. $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
  226. $table = TableRegistry::get('Comments', [
  227. 'className' => $class,
  228. 'connection' => 'test'
  229. ]);
  230. $this->assertInstanceOf($class, $table);
  231. $this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
  232. $this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
  233. }
  234. /**
  235. * Tests that table options can be pre-configured for the factory method
  236. *
  237. * @return void
  238. */
  239. public function testConfigAndBuild() {
  240. TableRegistry::clear();
  241. $map = TableRegistry::config();
  242. $this->assertEquals([], $map);
  243. $connection = ConnectionManager::get('test', false);
  244. $options = ['connection' => $connection];
  245. TableRegistry::config('users', $options);
  246. $map = TableRegistry::config();
  247. $this->assertEquals(['users' => $options], $map);
  248. $this->assertEquals($options, TableRegistry::config('users'));
  249. $schema = ['id' => ['type' => 'rubbish']];
  250. $options += ['schema' => $schema];
  251. TableRegistry::config('users', $options);
  252. $table = TableRegistry::get('users', ['table' => 'users']);
  253. $this->assertInstanceOf('Cake\ORM\Table', $table);
  254. $this->assertEquals('users', $table->table());
  255. $this->assertEquals('users', $table->alias());
  256. $this->assertSame($connection, $table->connection());
  257. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  258. $this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
  259. TableRegistry::clear();
  260. $this->assertEmpty(TableRegistry::config());
  261. TableRegistry::config('users', $options);
  262. $table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
  263. $this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
  264. $this->assertEquals('users', $table->table());
  265. $this->assertEquals('users', $table->alias());
  266. $this->assertSame($connection, $table->connection());
  267. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  268. }
  269. /**
  270. * Test setting an instance.
  271. *
  272. * @return void
  273. */
  274. public function testSet() {
  275. $mock = $this->getMock('Cake\ORM\Table');
  276. $this->assertSame($mock, TableRegistry::set('Articles', $mock));
  277. $this->assertSame($mock, TableRegistry::get('Articles'));
  278. }
  279. /**
  280. * Test setting an instance with plugin syntax aliases
  281. *
  282. * @return void
  283. */
  284. public function testSetPlugin() {
  285. Plugin::load('TestPlugin');
  286. $mock = $this->getMock('TestPlugin\Model\Table\CommentsTable');
  287. $this->assertSame($mock, TableRegistry::set('TestPlugin.Comments', $mock));
  288. $this->assertSame($mock, TableRegistry::get('TestPlugin.Comments'));
  289. $this->assertSame($mock, TableRegistry::get('Comments'));
  290. }
  291. /**
  292. * Tests genericInstances
  293. *
  294. * @return void
  295. */
  296. public function testGenericInstances() {
  297. $foos = TableRegistry::get('Foos');
  298. $bars = TableRegistry::get('Bars');
  299. TableRegistry::get('Articles');
  300. $expected = ['Foos' => $foos, 'Bars' => $bars];
  301. $this->assertEquals($expected, TableRegistry::genericInstances());
  302. }
  303. /**
  304. * Tests deleting an instance
  305. *
  306. * @return void
  307. */
  308. public function testDelete() {
  309. Plugin::load('TestPlugin');
  310. $pluginTable = TableRegistry::get('TestPlugin.Comments');
  311. $cachedTable = TableRegistry::get('Comments');
  312. $this->assertTrue(TableRegistry::exists('Comments'));
  313. $this->assertSame($pluginTable, $cachedTable);
  314. TableRegistry::delete('Comments');
  315. $this->assertFalse(TableRegistry::exists('Comments'));
  316. $appTable = TableRegistry::get('Comments');
  317. $this->assertTrue(TableRegistry::exists('Comments'));
  318. $this->assertNotSame($pluginTable, $appTable);
  319. }
  320. }