TableRegistryTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\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::locator();
  39. }
  40. /**
  41. * tear down
  42. *
  43. * @return void
  44. */
  45. public function tearDown()
  46. {
  47. parent::tearDown();
  48. TableRegistry::locator($this->_originalLocator);
  49. }
  50. /**
  51. * Sets and returns mock LocatorInterface instance.
  52. *
  53. * @return Cake\ORM\Locator\LocatorInterface
  54. */
  55. protected function _setMockLocator()
  56. {
  57. $locator = $this->getMockBuilder('Cake\ORM\Locator\LocatorInterface')->getMock();
  58. TableRegistry::locator($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->_setMockLocator();
  70. $this->assertSame($locator, TableRegistry::locator());
  71. }
  72. /**
  73. * Test that locator() method is returing TableLocator by default.
  74. *
  75. * @return void
  76. */
  77. public function testLocatorDefault()
  78. {
  79. $locator = TableRegistry::locator();
  80. $this->assertInstanceOf('Cake\ORM\Locator\TableLocator', $locator);
  81. }
  82. /**
  83. * Test config() method.
  84. *
  85. * @return void
  86. */
  87. public function testConfig()
  88. {
  89. $locator = $this->_setMockLocator();
  90. $locator->expects($this->once())->method('config')->with('Test', []);
  91. TableRegistry::config('Test', []);
  92. }
  93. /**
  94. * Test the get() method.
  95. *
  96. * @return void
  97. */
  98. public function testGet()
  99. {
  100. $locator = $this->_setMockLocator();
  101. $locator->expects($this->once())->method('get')->with('Test', []);
  102. TableRegistry::get('Test', []);
  103. }
  104. /**
  105. * Test the get() method.
  106. *
  107. * @return void
  108. */
  109. public function testSet()
  110. {
  111. $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  112. $locator = $this->_setMockLocator();
  113. $locator->expects($this->once())->method('set')->with('Test', $table);
  114. TableRegistry::set('Test', $table);
  115. }
  116. /**
  117. * Test the remove() method.
  118. *
  119. * @return void
  120. */
  121. public function testRemove()
  122. {
  123. $locator = $this->_setMockLocator();
  124. $locator->expects($this->once())->method('remove')->with('Test');
  125. TableRegistry::remove('Test');
  126. }
  127. /**
  128. * Test the clear() method.
  129. *
  130. * @return void
  131. */
  132. public function testClear()
  133. {
  134. $locator = $this->_setMockLocator();
  135. $locator->expects($this->once())->method('clear');
  136. TableRegistry::clear();
  137. }
  138. }