SchemaCacheCommandsTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\Database\Schema\CachedCollection;
  21. use Cake\Datasource\ConnectionManager;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * SchemaCacheCommands test.
  25. */
  26. class SchemaCacheCommandsTest extends TestCase
  27. {
  28. use ConsoleIntegrationTestTrait;
  29. /**
  30. * Fixtures.
  31. *
  32. * @var list<string>
  33. */
  34. protected array $fixtures = ['core.Articles', 'core.Tags'];
  35. /**
  36. * @var \Cake\Datasource\ConnectionInterface
  37. */
  38. protected $connection;
  39. /**
  40. * @var \Cake\Cache\Engine\NullEngine|\PHPUnit\Framework\MockObject\MockObject
  41. */
  42. protected $cache;
  43. /**
  44. * setup method
  45. */
  46. public function setUp(): void
  47. {
  48. parent::setUp();
  49. $this->setAppNamespace();
  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(CachedCollection::class, $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(CachedCollection::class, $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. ->willReturn(true);
  95. $this->exec('schema_cache build --connection test');
  96. $this->assertExitSuccess();
  97. }
  98. /**
  99. * Test build() with one arg.
  100. */
  101. public function testBuildNamedModel(): void
  102. {
  103. $this->cache->expects($this->once())
  104. ->method('set')
  105. ->with('test_articles')
  106. ->willReturn(true);
  107. $this->cache->expects($this->never())
  108. ->method('delete')
  109. ->willReturn(false);
  110. $this->exec('schema_cache build --connection test articles');
  111. $this->assertExitSuccess();
  112. }
  113. /**
  114. * Test build() overwrites cached data.
  115. */
  116. public function testBuildOverwritesExistingData(): void
  117. {
  118. $this->cache->expects($this->once())
  119. ->method('set')
  120. ->with('test_articles')
  121. ->willReturn(true);
  122. $this->cache->expects($this->never())
  123. ->method('get');
  124. $this->cache->expects($this->never())
  125. ->method('delete')
  126. ->willReturn(false);
  127. $this->exec('schema_cache build --connection test articles');
  128. $this->assertExitSuccess();
  129. }
  130. /**
  131. * Test build() with a nonexistent connection name.
  132. */
  133. public function testBuildInvalidConnection(): void
  134. {
  135. $this->exec('schema_cache build --connection derpy-derp articles');
  136. $this->assertExitError();
  137. }
  138. /**
  139. * Test clear() with an invalid connection name.
  140. */
  141. public function testClearInvalidConnection(): void
  142. {
  143. $this->exec('schema_cache clear --connection derpy-derp articles');
  144. $this->assertExitError();
  145. }
  146. /**
  147. * Test clear() with no args.
  148. */
  149. public function testClearNoArgs(): void
  150. {
  151. $this->cache->expects($this->atLeastOnce())
  152. ->method('delete')
  153. ->willReturn(true);
  154. $this->exec('schema_cache clear --connection test');
  155. $this->assertExitSuccess();
  156. }
  157. /**
  158. * Test clear() with a model name.
  159. */
  160. public function testClearNamedModel(): void
  161. {
  162. $this->cache->expects($this->never())
  163. ->method('set')
  164. ->willReturn(true);
  165. $this->cache->expects($this->once())
  166. ->method('delete')
  167. ->with('test_articles')
  168. ->willReturn(false);
  169. $this->exec('schema_cache clear --connection test articles');
  170. $this->assertExitSuccess();
  171. }
  172. }