SchemaCacheCommandsTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\Command;
  17. use Cake\Cache\Cache;
  18. use Cake\Cache\Engine\NullEngine;
  19. use Cake\Console\ConsoleIo;
  20. use Cake\Database\SchemaCache;
  21. use Cake\Datasource\ConnectionManager;
  22. use Cake\TestSuite\ConsoleIntegrationTestTrait;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * SchemaCacheCommands test.
  26. */
  27. class SchemaCacheCommandsTest extends TestCase
  28. {
  29. use ConsoleIntegrationTestTrait;
  30. /**
  31. * Fixtures.
  32. *
  33. * @var array
  34. */
  35. protected $fixtures = ['core.Articles', 'core.Tags'];
  36. /**
  37. * @var \Cake\Datasource\ConnectionInterface
  38. */
  39. protected $connection;
  40. /**
  41. * @var \Cake\Cache\Engine\NullEngine|\PHPUnit\Framework\MockObject\MockObject
  42. */
  43. protected $cache;
  44. /**
  45. * setup method
  46. *
  47. * @return void
  48. */
  49. public function setUp(): void
  50. {
  51. parent::setUp();
  52. $this->setAppNamespace();
  53. $this->useCommandRunner();
  54. $this->cache = $this->getMockBuilder(NullEngine::class)
  55. ->setMethods(['set', 'get', 'delete'])
  56. ->getMock();
  57. Cache::setConfig('orm_cache', $this->cache);
  58. $this->connection = ConnectionManager::get('test');
  59. $this->connection->cacheMetadata('orm_cache');
  60. }
  61. /**
  62. * Teardown
  63. *
  64. * @return void
  65. */
  66. public function tearDown(): void
  67. {
  68. parent::tearDown();
  69. $this->connection->cacheMetadata('_cake_model_');
  70. unset($this->connection);
  71. Cache::drop('orm_cache');
  72. }
  73. protected function getShell()
  74. {
  75. $io = $this->getMockBuilder(ConsoleIo::class)->getMock();
  76. $shell = $this->getMockBuilder(SchemaCacheShell::class)
  77. ->setConstructorArgs([$io])
  78. ->setMethods(['_getSchemaCache'])
  79. ->getMock();
  80. $schemaCache = new SchemaCache($this->connection);
  81. $shell->expects($this->once())
  82. ->method('_getSchemaCache')
  83. ->willReturn($schemaCache);
  84. return $shell;
  85. }
  86. /**
  87. * Test that clear enables the cache if it was disabled.
  88. *
  89. * @return void
  90. */
  91. public function testClearEnablesMetadataCache()
  92. {
  93. $this->connection->cacheMetadata(false);
  94. $this->exec('schema_cache clear --connection test');
  95. $this->assertExitSuccess();
  96. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $this->connection->getSchemaCollection());
  97. }
  98. /**
  99. * Test that build enables the cache if it was disabled.
  100. *
  101. * @return void
  102. */
  103. public function testBuildEnablesMetadataCache()
  104. {
  105. $this->connection->cacheMetadata(false);
  106. $this->exec('schema_cache build --connection test');
  107. $this->assertExitSuccess();
  108. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $this->connection->getSchemaCollection());
  109. }
  110. /**
  111. * Test build() with no args.
  112. *
  113. * @return void
  114. */
  115. public function testBuildNoArgs()
  116. {
  117. $this->cache->expects($this->at(0))
  118. ->method('set')
  119. ->with('test_articles')
  120. ->will($this->returnValue(true));
  121. $this->exec('schema_cache build --connection test');
  122. $this->assertExitSuccess();
  123. }
  124. /**
  125. * Test build() with one arg.
  126. *
  127. * @return void
  128. */
  129. public function testBuildNamedModel()
  130. {
  131. $this->cache->expects($this->once())
  132. ->method('set')
  133. ->with('test_articles')
  134. ->will($this->returnValue(true));
  135. $this->cache->expects($this->never())
  136. ->method('delete')
  137. ->will($this->returnValue(false));
  138. $this->exec('schema_cache build --connection test articles');
  139. $this->assertExitSuccess();
  140. }
  141. /**
  142. * Test build() overwrites cached data.
  143. *
  144. * @return void
  145. */
  146. public function testBuildOverwritesExistingData()
  147. {
  148. $this->cache->expects($this->once())
  149. ->method('set')
  150. ->with('test_articles')
  151. ->will($this->returnValue(true));
  152. $this->cache->expects($this->never())
  153. ->method('get');
  154. $this->cache->expects($this->never())
  155. ->method('delete')
  156. ->will($this->returnValue(false));
  157. $this->exec('schema_cache build --connection test articles');
  158. $this->assertExitSuccess();
  159. }
  160. /**
  161. * Test build() with a non-existing connection name.
  162. *
  163. * @return void
  164. */
  165. public function testBuildInvalidConnection()
  166. {
  167. $this->exec('schema_cache build --connection derpy-derp articles');
  168. $this->assertExitError();
  169. }
  170. /**
  171. * Test clear() with an invalid connection name.
  172. *
  173. * @return void
  174. */
  175. public function testClearInvalidConnection()
  176. {
  177. $this->exec('schema_cache clear --connection derpy-derp articles');
  178. $this->assertExitError();
  179. }
  180. /**
  181. * Test clear() with no args.
  182. *
  183. * @return void
  184. */
  185. public function testClearNoArgs()
  186. {
  187. $this->cache->expects($this->at(0))
  188. ->method('delete')
  189. ->with('test_articles')
  190. ->will($this->returnValue(true));
  191. $this->exec('schema_cache clear --connection test');
  192. $this->assertExitSuccess();
  193. }
  194. /**
  195. * Test clear() with a model name.
  196. *
  197. * @return void
  198. */
  199. public function testClearNamedModel()
  200. {
  201. $this->cache->expects($this->never())
  202. ->method('set')
  203. ->will($this->returnValue(true));
  204. $this->cache->expects($this->once())
  205. ->method('delete')
  206. ->with('test_articles')
  207. ->will($this->returnValue(false));
  208. $this->exec('schema_cache clear --connection test articles');
  209. $this->assertExitSuccess();
  210. }
  211. }