SchemaCacheShellTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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->any())
  96. ->method('write')
  97. ->will($this->returnValue(true));
  98. $this->cache->expects($this->at(3))
  99. ->method('write')
  100. ->with('test_articles');
  101. $this->shell->params['connection'] = 'test';
  102. $this->shell->build();
  103. }
  104. /**
  105. * Test build() with one arg.
  106. *
  107. * @return void
  108. */
  109. public function testBuildNamedModel()
  110. {
  111. $this->cache->expects($this->once())
  112. ->method('write')
  113. ->with('test_articles')
  114. ->will($this->returnValue(true));
  115. $this->cache->expects($this->never())
  116. ->method('delete');
  117. $this->shell->params['connection'] = 'test';
  118. $this->shell->build('articles');
  119. }
  120. /**
  121. * Test build() overwrites cached data.
  122. *
  123. * @return void
  124. */
  125. public function testBuildOverwritesExistingData()
  126. {
  127. $this->cache->expects($this->once())
  128. ->method('write')
  129. ->with('test_articles')
  130. ->will($this->returnValue(true));
  131. $this->cache->expects($this->never())
  132. ->method('read');
  133. $this->cache->expects($this->never())
  134. ->method('delete');
  135. $this->shell->params['connection'] = 'test';
  136. $this->shell->build('articles');
  137. }
  138. /**
  139. * Test build() with a non-existing connection name.
  140. *
  141. * @return void
  142. */
  143. public function testBuildInvalidConnection()
  144. {
  145. $this->expectException(StopException::class);
  146. $this->shell->params['connection'] = 'derpy-derp';
  147. $this->shell->build('articles');
  148. }
  149. /**
  150. * Test clear() with an invalid connection name.
  151. *
  152. * @return void
  153. */
  154. public function testClearInvalidConnection()
  155. {
  156. $this->expectException(StopException::class);
  157. $this->shell->params['connection'] = 'derpy-derp';
  158. $this->shell->clear('articles');
  159. }
  160. /**
  161. * Test clear() with no args.
  162. *
  163. * @return void
  164. */
  165. public function testClearNoArgs()
  166. {
  167. $this->cache->expects($this->at(3))
  168. ->method('delete')
  169. ->with('test_articles')
  170. ->will($this->returnValue(true));
  171. $this->shell->params['connection'] = 'test';
  172. $this->shell->clear();
  173. }
  174. /**
  175. * Test clear() with a model name.
  176. *
  177. * @return void
  178. */
  179. public function testClearNamedModel()
  180. {
  181. $this->cache->expects($this->never())
  182. ->method('write');
  183. $this->cache->expects($this->once())
  184. ->method('delete')
  185. ->with('test_articles');
  186. $this->shell->params['connection'] = 'test';
  187. $this->shell->clear('articles');
  188. }
  189. }