TableRegistryTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\TableRegistry;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Test case for TableRegistry
  20. */
  21. class TableRegistryTest extends TestCase
  22. {
  23. /**
  24. * Original TableLocator.
  25. *
  26. * @var \Cake\ORM\Locator\LocatorInterface
  27. */
  28. protected $_originalLocator;
  29. /**
  30. * Remember original instance to set it back on tearDown() just to make sure
  31. * other tests are not broken.
  32. *
  33. * @return void
  34. */
  35. public function setUp()
  36. {
  37. parent::setUp();
  38. $this->_originalLocator = TableRegistry::getTableLocator();
  39. }
  40. /**
  41. * tear down
  42. *
  43. * @return void
  44. */
  45. public function tearDown()
  46. {
  47. parent::tearDown();
  48. TableRegistry::setTableLocator($this->_originalLocator);
  49. }
  50. /**
  51. * Sets and returns mock LocatorInterface instance.
  52. *
  53. * @return \Cake\ORM\Locator\LocatorInterface|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected function _setMockLocator()
  56. {
  57. $locator = $this->getMockBuilder('Cake\ORM\Locator\LocatorInterface')->getMock();
  58. TableRegistry::setTableLocator($locator);
  59. return $locator;
  60. }
  61. /**
  62. * Test locator() method.
  63. *
  64. * @group deprecated
  65. * @return void
  66. */
  67. public function testLocator()
  68. {
  69. $this->deprecated(function () {
  70. $this->assertInstanceOf('Cake\ORM\Locator\LocatorInterface', TableRegistry::locator());
  71. $locator = $this->getMockBuilder('Cake\ORM\Locator\LocatorInterface')->getMock();
  72. TableRegistry::locator($locator);
  73. $this->assertSame($locator, TableRegistry::locator());
  74. });
  75. }
  76. /**
  77. * Test testSetLocator() method.
  78. *
  79. * @return void
  80. */
  81. public function testSetLocator()
  82. {
  83. $locator = $this->_setMockLocator();
  84. $this->assertSame($locator, TableRegistry::getTableLocator());
  85. }
  86. /**
  87. * Test testSetLocator() method.
  88. *
  89. * @return void
  90. */
  91. public function testGetLocator()
  92. {
  93. $this->assertInstanceOf('Cake\ORM\Locator\LocatorInterface', TableRegistry::getTableLocator());
  94. }
  95. /**
  96. * Test that locator() method is returning TableLocator by default.
  97. *
  98. * @return void
  99. */
  100. public function testLocatorDefault()
  101. {
  102. $locator = TableRegistry::getTableLocator();
  103. $this->assertInstanceOf('Cake\ORM\Locator\TableLocator', $locator);
  104. }
  105. /**
  106. * Test config() method.
  107. *
  108. * @return void
  109. */
  110. public function testConfig()
  111. {
  112. $this->deprecated(function () {
  113. $locator = $this->_setMockLocator();
  114. $locator->expects($this->once())->method('config')->with('Test', []);
  115. TableRegistry::config('Test', []);
  116. });
  117. }
  118. /**
  119. * Test the get() method.
  120. *
  121. * @return void
  122. */
  123. public function testGet()
  124. {
  125. $this->deprecated(function () {
  126. $locator = $this->_setMockLocator();
  127. $locator->expects($this->once())->method('get')->with('Test', []);
  128. TableRegistry::get('Test', []);
  129. });
  130. }
  131. /**
  132. * Test the get() method.
  133. *
  134. * @return void
  135. */
  136. public function testSet()
  137. {
  138. $this->deprecated(function () {
  139. $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  140. $locator = $this->_setMockLocator();
  141. $locator->expects($this->once())->method('set')->with('Test', $table);
  142. TableRegistry::set('Test', $table);
  143. });
  144. }
  145. /**
  146. * Test the remove() method.
  147. *
  148. * @return void
  149. */
  150. public function testRemove()
  151. {
  152. $this->deprecated(function () {
  153. $locator = $this->_setMockLocator();
  154. $locator->expects($this->once())->method('remove')->with('Test');
  155. TableRegistry::remove('Test');
  156. });
  157. }
  158. /**
  159. * Test the clear() method.
  160. *
  161. * @return void
  162. */
  163. public function testClear()
  164. {
  165. $this->deprecated(function () {
  166. $locator = $this->_setMockLocator();
  167. $locator->expects($this->once())->method('clear');
  168. TableRegistry::clear();
  169. });
  170. }
  171. }