SchemaCacheShellTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.6.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell;
  16. use Cake\Cache\Cache;
  17. use Cake\Console\Exception\StopException;
  18. use Cake\Datasource\ConnectionManager;
  19. use Cake\Shell\SchemaCacheShell;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * SchemaCacheShell test.
  23. */
  24. class SchemaCacheShellTest extends TestCase
  25. {
  26. /**
  27. * Fixtures.
  28. *
  29. * @var array
  30. */
  31. public $fixtures = ['core.articles', 'core.tags'];
  32. /**
  33. * setup method
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. parent::setUp();
  40. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  41. $this->shell = new SchemaCacheShell($this->io);
  42. $this->cache = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  43. $this->cache->expects($this->any())
  44. ->method('init')
  45. ->will($this->returnValue(true));
  46. Cache::setConfig('orm_cache', $this->cache);
  47. $ds = ConnectionManager::get('test');
  48. $ds->cacheMetadata('orm_cache');
  49. }
  50. /**
  51. * Teardown
  52. *
  53. * @return void
  54. */
  55. public function tearDown()
  56. {
  57. parent::tearDown();
  58. Cache::drop('orm_cache');
  59. $ds = ConnectionManager::get('test');
  60. $ds->cacheMetadata(false);
  61. }
  62. /**
  63. * Test that clear enables the cache if it was disabled.
  64. *
  65. * @return void
  66. */
  67. public function testClearEnablesMetadataCache()
  68. {
  69. $ds = ConnectionManager::get('test');
  70. $ds->cacheMetadata(false);
  71. $this->shell->params['connection'] = 'test';
  72. $this->shell->clear();
  73. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->getSchemaCollection());
  74. }
  75. /**
  76. * Test that build enables the cache if it was disabled.
  77. *
  78. * @return void
  79. */
  80. public function testBuildEnablesMetadataCache()
  81. {
  82. $ds = ConnectionManager::get('test');
  83. $ds->cacheMetadata(false);
  84. $this->shell->params['connection'] = 'test';
  85. $this->shell->build();
  86. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->getSchemaCollection());
  87. }
  88. /**
  89. * Test build() with no args.
  90. *
  91. * @return void
  92. */
  93. public function testBuildNoArgs()
  94. {
  95. $this->cache->expects($this->at(3))
  96. ->method('write')
  97. ->with('test_articles');
  98. $this->shell->params['connection'] = 'test';
  99. $this->shell->build();
  100. }
  101. /**
  102. * Test build() with one arg.
  103. *
  104. * @return void
  105. */
  106. public function testBuildNamedModel()
  107. {
  108. $this->cache->expects($this->once())
  109. ->method('write')
  110. ->with('test_articles');
  111. $this->cache->expects($this->never())
  112. ->method('delete');
  113. $this->shell->params['connection'] = 'test';
  114. $this->shell->build('articles');
  115. }
  116. /**
  117. * Test build() overwrites cached data.
  118. *
  119. * @return void
  120. */
  121. public function testBuildOverwritesExistingData()
  122. {
  123. $this->cache->expects($this->once())
  124. ->method('write')
  125. ->with('test_articles');
  126. $this->cache->expects($this->never())
  127. ->method('read');
  128. $this->cache->expects($this->never())
  129. ->method('delete');
  130. $this->shell->params['connection'] = 'test';
  131. $this->shell->build('articles');
  132. }
  133. /**
  134. * Test build() with a non-existing connection name.
  135. *
  136. * @return void
  137. */
  138. public function testBuildInvalidConnection()
  139. {
  140. $this->expectException(StopException::class);
  141. $this->shell->params['connection'] = 'derpy-derp';
  142. $this->shell->build('articles');
  143. }
  144. /**
  145. * Test clear() with an invalid connection name.
  146. *
  147. * @return void
  148. */
  149. public function testClearInvalidConnection()
  150. {
  151. $this->expectException(StopException::class);
  152. $this->shell->params['connection'] = 'derpy-derp';
  153. $this->shell->clear('articles');
  154. }
  155. /**
  156. * Test clear() with no args.
  157. *
  158. * @return void
  159. */
  160. public function testClearNoArgs()
  161. {
  162. $this->cache->expects($this->at(3))
  163. ->method('delete')
  164. ->with('test_articles');
  165. $this->shell->params['connection'] = 'test';
  166. $this->shell->clear();
  167. }
  168. /**
  169. * Test clear() with a model name.
  170. *
  171. * @return void
  172. */
  173. public function testClearNamedModel()
  174. {
  175. $this->cache->expects($this->never())
  176. ->method('write');
  177. $this->cache->expects($this->once())
  178. ->method('delete')
  179. ->with('test_articles');
  180. $this->shell->params['connection'] = 'test';
  181. $this->shell->clear('articles');
  182. }
  183. }