SchemaCacheShellTest.php 5.3 KB

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