SchemaCacheCommandsTest.php 5.6 KB

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