TableRegistryTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Test\TestCase\ORM;
  18. use Cake\Core\Configure;
  19. use Cake\Database\ConnectionManager;
  20. use Cake\ORM\Table;
  21. use Cake\ORM\TableRegistry;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Used to test correct class is instantiated when using TableRegistry::get();
  25. */
  26. class MyUsersTable extends Table {
  27. /**
  28. * Overrides default table name
  29. *
  30. * @var string
  31. */
  32. protected $_table = 'users';
  33. }
  34. /**
  35. * Test case for TableRegistry
  36. */
  37. class TableRegistryTest extends TestCase {
  38. /**
  39. * setup
  40. *
  41. * @return void
  42. */
  43. public function setUp() {
  44. parent::setUp();
  45. Configure::write('App.namespace', 'TestApp');
  46. }
  47. /**
  48. * tear down
  49. *
  50. * @return void
  51. */
  52. public function tearDown() {
  53. parent::tearDown();
  54. TableRegistry::clear();
  55. }
  56. /**
  57. * Test config() method.
  58. *
  59. * @return void
  60. */
  61. public function testConfig() {
  62. $this->assertEquals([], TableRegistry::config('Tests'));
  63. $data = [
  64. 'connection' => 'testing',
  65. 'entityClass' => 'TestApp\Model\Entity\Article',
  66. ];
  67. $result = TableRegistry::config('Tests', $data);
  68. $this->assertEquals($data, $result, 'Returns config data.');
  69. $result = TableRegistry::config();
  70. $expected = ['Tests' => $data];
  71. $this->assertEquals($expected, $result);
  72. }
  73. /**
  74. * Test calling config() on existing instances throws an error.
  75. *
  76. * @expectedException RuntimeException
  77. * @expectedExceptionMessage You cannot configure "Users", it has already been constructed.
  78. * @return void
  79. */
  80. public function testConfigOnDefinedInstance() {
  81. $users = TableRegistry::get('Users');
  82. TableRegistry::config('Users', ['table' => 'my_users']);
  83. }
  84. /**
  85. * Test the exists() method.
  86. *
  87. * @return void
  88. */
  89. public function testExists() {
  90. $this->assertFalse(TableRegistry::exists('Articles'));
  91. TableRegistry::config('Articles', ['table' => 'articles']);
  92. $this->assertFalse(TableRegistry::exists('Articles'));
  93. TableRegistry::get('Articles', ['table' => 'articles']);
  94. $this->assertTrue(TableRegistry::exists('Articles'));
  95. }
  96. /**
  97. * Test getting instances from the registry.
  98. *
  99. * @return void
  100. */
  101. public function testGet() {
  102. $result = TableRegistry::get('Articles', [
  103. 'table' => 'my_articles',
  104. ]);
  105. $this->assertInstanceOf('Cake\ORM\Table', $result);
  106. $this->assertEquals('my_articles', $result->table());
  107. $result2 = TableRegistry::get('Articles');
  108. $this->assertSame($result, $result2);
  109. $this->assertEquals('my_articles', $result->table());
  110. }
  111. /**
  112. * Test that get() uses config data set with config()
  113. *
  114. * @return void
  115. */
  116. public function testGetWithConfig() {
  117. TableRegistry::config('Articles', [
  118. 'table' => 'my_articles',
  119. ]);
  120. $result = TableRegistry::get('Articles');
  121. $this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
  122. }
  123. /**
  124. * Test get with config throws an exception if the alias exists already.
  125. *
  126. * @expectedException RuntimeException
  127. * @expectedExceptionMessage You cannot configure "Users", it already exists in the registry.
  128. * @return void
  129. */
  130. public function testGetExistingWithConfigData() {
  131. $users = TableRegistry::get('Users');
  132. TableRegistry::get('Users', ['table' => 'my_users']);
  133. }
  134. /**
  135. * Tests that tables can be instantiated based on conventions
  136. * and using plugin notation
  137. *
  138. * @return void
  139. */
  140. public function testBuildConvention() {
  141. $table = TableRegistry::get('articles');
  142. $this->assertInstanceOf('\TestApp\Model\Table\ArticlesTable', $table);
  143. $table = TableRegistry::get('Articles');
  144. $this->assertInstanceOf('\TestApp\Model\Table\ArticlesTable', $table);
  145. $table = TableRegistry::get('authors');
  146. $this->assertInstanceOf('\TestApp\Model\Table\AuthorsTable', $table);
  147. $table = TableRegistry::get('Authors');
  148. $this->assertInstanceOf('\TestApp\Model\Table\AuthorsTable', $table);
  149. $class = $this->getMockClass('\Cake\ORM\Table');
  150. $class::staticExpects($this->once())
  151. ->method('defaultConnectionName')
  152. ->will($this->returnValue('test'));
  153. if (!class_exists('MyPlugin\Model\Table\SuperTestsTable')) {
  154. class_alias($class, 'MyPlugin\Model\Table\SuperTestsTable');
  155. }
  156. $table = TableRegistry::get('MyPlugin.SuperTests');
  157. $this->assertInstanceOf($class, $table);
  158. }
  159. /**
  160. * Tests that table options can be pre-configured for the factory method
  161. *
  162. * @return void
  163. */
  164. public function testConfigAndBuild() {
  165. TableRegistry::clear();
  166. $map = TableRegistry::config();
  167. $this->assertEquals([], $map);
  168. $connection = ConnectionManager::get('test', false);
  169. $options = ['connection' => $connection];
  170. TableRegistry::config('users', $options);
  171. $map = TableRegistry::config();
  172. $this->assertEquals(['users' => $options], $map);
  173. $this->assertEquals($options, TableRegistry::config('users'));
  174. $schema = ['id' => ['type' => 'rubbish']];
  175. $options += ['schema' => $schema];
  176. TableRegistry::config('users', $options);
  177. $table = TableRegistry::get('users', ['table' => 'users']);
  178. $this->assertInstanceOf('Cake\ORM\Table', $table);
  179. $this->assertEquals('users', $table->table());
  180. $this->assertEquals('users', $table->alias());
  181. $this->assertSame($connection, $table->connection());
  182. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  183. $this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
  184. TableRegistry::clear();
  185. $this->assertEmpty(TableRegistry::config());
  186. TableRegistry::config('users', $options);
  187. $table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
  188. $this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
  189. $this->assertEquals('users', $table->table());
  190. $this->assertEquals('users', $table->alias());
  191. $this->assertSame($connection, $table->connection());
  192. $this->assertEquals(array_keys($schema), $table->schema()->columns());
  193. }
  194. /**
  195. * Test setting an instance.
  196. *
  197. * @return void
  198. */
  199. public function testSet() {
  200. $mock = $this->getMock('Cake\ORM\Table');
  201. $this->assertSame($mock, TableRegistry::set('Articles', $mock));
  202. $this->assertSame($mock, TableRegistry::get('Articles'));
  203. }
  204. }