TableRegistryTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Locator\LocatorInterface;
  17. use Cake\ORM\TableRegistry;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Test case for TableRegistry
  21. */
  22. class TableRegistryTest extends TestCase
  23. {
  24. /**
  25. * Original TableLocator.
  26. *
  27. * @var Cake\ORM\Locator\LocatorInterface
  28. */
  29. protected $_originalLocator;
  30. /**
  31. * Remember original instance to set it back on tearDown() just to make sure
  32. * other tests are not broken.
  33. *
  34. * @return void
  35. */
  36. public function setUp()
  37. {
  38. parent::setUp();
  39. $this->_originalLocator = TableRegistry::locator();
  40. }
  41. /**
  42. * tear down
  43. *
  44. * @return void
  45. */
  46. public function tearDown()
  47. {
  48. parent::tearDown();
  49. TableRegistry::locator($this->_originalLocator);
  50. }
  51. /**
  52. * Sets and returns mock LocatorInterface instance.
  53. *
  54. * @return Cake\ORM\Locator\LocatorInterface
  55. */
  56. protected function _setMockLocator()
  57. {
  58. $locator = $this->getMock('Cake\ORM\Locator\LocatorInterface');
  59. TableRegistry::locator($locator);
  60. return $locator;
  61. }
  62. /**
  63. * Test locator() method.
  64. *
  65. * @return void
  66. */
  67. public function testLocator()
  68. {
  69. $this->assertInstanceOf('Cake\ORM\Locator\LocatorInterface', TableRegistry::locator());
  70. $locator = $this->_setMockLocator();
  71. $this->assertSame($locator, TableRegistry::locator());
  72. }
  73. /**
  74. * Test that locator() method is returing TableLocator by default.
  75. *
  76. * @return void
  77. */
  78. public function testLocatorDefault()
  79. {
  80. $locator = TableRegistry::locator();
  81. $this->assertInstanceOf('Cake\ORM\Locator\TableLocator', $locator);
  82. }
  83. /**
  84. * Test config() method.
  85. *
  86. * @return void
  87. */
  88. public function testConfig()
  89. {
  90. $locator = $this->_setMockLocator();
  91. $locator->expects($this->once())->method('config')->with('Test', []);
  92. TableRegistry::config('Test', []);
  93. }
  94. /**
  95. * Test the get() method.
  96. *
  97. * @return void
  98. */
  99. public function testGet()
  100. {
  101. $locator = $this->_setMockLocator();
  102. $locator->expects($this->once())->method('get')->with('Test', []);
  103. TableRegistry::get('Test', []);
  104. }
  105. /**
  106. * Test the get() method.
  107. *
  108. * @return void
  109. */
  110. public function testSet()
  111. {
  112. $table = $this->getMock('Cake\ORM\Table');
  113. $locator = $this->_setMockLocator();
  114. $locator->expects($this->once())->method('set')->with('Test', $table);
  115. TableRegistry::set('Test', $table);
  116. }
  117. /**
  118. * Test the remove() method.
  119. *
  120. * @return void
  121. */
  122. public function testRemove()
  123. {
  124. $locator = $this->_setMockLocator();
  125. $locator->expects($this->once())->method('remove')->with('Test');
  126. TableRegistry::remove('Test');
  127. }
  128. /**
  129. * Test the clear() method.
  130. *
  131. * @return void
  132. */
  133. public function testClear()
  134. {
  135. $locator = $this->_setMockLocator();
  136. $locator->expects($this->once())->method('clear');
  137. TableRegistry::clear();
  138. }
  139. }