TableRegistryTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * @return void
  65. */
  66. public function testLocator()
  67. {
  68. $this->assertInstanceOf('Cake\ORM\Locator\LocatorInterface', TableRegistry::locator());
  69. $locator = $this->getMockBuilder('Cake\ORM\Locator\LocatorInterface')->getMock();
  70. TableRegistry::locator($locator);
  71. $this->assertSame($locator, TableRegistry::locator());
  72. }
  73. /**
  74. * Test testSetLocator() method.
  75. *
  76. * @return void
  77. */
  78. public function testSetLocator()
  79. {
  80. $locator = $this->_setMockLocator();
  81. $this->assertSame($locator, TableRegistry::getTableLocator());
  82. }
  83. /**
  84. * Test testSetLocator() method.
  85. *
  86. * @return void
  87. */
  88. public function testGetLocator()
  89. {
  90. $this->assertInstanceOf('Cake\ORM\Locator\LocatorInterface', TableRegistry::getTableLocator());
  91. }
  92. /**
  93. * Test that locator() method is returning TableLocator by default.
  94. *
  95. * @return void
  96. */
  97. public function testLocatorDefault()
  98. {
  99. $locator = TableRegistry::getTableLocator();
  100. $this->assertInstanceOf('Cake\ORM\Locator\TableLocator', $locator);
  101. }
  102. /**
  103. * Test config() method.
  104. *
  105. * @return void
  106. */
  107. public function testConfig()
  108. {
  109. $locator = $this->_setMockLocator();
  110. $locator->expects($this->once())->method('config')->with('Test', []);
  111. TableRegistry::config('Test', []);
  112. }
  113. /**
  114. * Test the get() method.
  115. *
  116. * @return void
  117. */
  118. public function testGet()
  119. {
  120. $locator = $this->_setMockLocator();
  121. $locator->expects($this->once())->method('get')->with('Test', []);
  122. TableRegistry::get('Test', []);
  123. }
  124. /**
  125. * Test the get() method.
  126. *
  127. * @return void
  128. */
  129. public function testSet()
  130. {
  131. $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  132. $locator = $this->_setMockLocator();
  133. $locator->expects($this->once())->method('set')->with('Test', $table);
  134. TableRegistry::set('Test', $table);
  135. }
  136. /**
  137. * Test the remove() method.
  138. *
  139. * @return void
  140. */
  141. public function testRemove()
  142. {
  143. $locator = $this->_setMockLocator();
  144. $locator->expects($this->once())->method('remove')->with('Test');
  145. TableRegistry::remove('Test');
  146. }
  147. /**
  148. * Test the clear() method.
  149. *
  150. * @return void
  151. */
  152. public function testClear()
  153. {
  154. $locator = $this->_setMockLocator();
  155. $locator->expects($this->once())->method('clear');
  156. TableRegistry::clear();
  157. }
  158. }