SchemaCacheCommandsTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 list<string>
  32. */
  33. protected array $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->cache = $this->getMockBuilder(NullEngine::class)
  50. ->onlyMethods(['set', 'get', 'delete'])
  51. ->getMock();
  52. Cache::setConfig('orm_cache', $this->cache);
  53. $this->connection = ConnectionManager::get('test');
  54. $this->connection->cacheMetadata('orm_cache');
  55. }
  56. /**
  57. * Teardown
  58. */
  59. public function tearDown(): void
  60. {
  61. $this->connection->cacheMetadata(false);
  62. parent::tearDown();
  63. unset($this->connection);
  64. Cache::drop('orm_cache');
  65. }
  66. /**
  67. * Test that clear enables the cache if it was disabled.
  68. */
  69. public function testClearEnablesMetadataCache(): void
  70. {
  71. $this->connection->cacheMetadata(false);
  72. $this->exec('schema_cache clear --connection test');
  73. $this->assertExitSuccess();
  74. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $this->connection->getSchemaCollection());
  75. }
  76. /**
  77. * Test that build enables the cache if it was disabled.
  78. */
  79. public function testBuildEnablesMetadataCache(): void
  80. {
  81. $this->connection->cacheMetadata(false);
  82. $this->exec('schema_cache build --connection test');
  83. $this->assertExitSuccess();
  84. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $this->connection->getSchemaCollection());
  85. }
  86. /**
  87. * Test build() with no args.
  88. */
  89. public function testBuildNoArgs(): void
  90. {
  91. $this->cache->expects($this->atLeastOnce())
  92. ->method('set')
  93. ->willReturn(true);
  94. $this->exec('schema_cache build --connection test');
  95. $this->assertExitSuccess();
  96. }
  97. /**
  98. * Test build() with one arg.
  99. */
  100. public function testBuildNamedModel(): void
  101. {
  102. $this->cache->expects($this->once())
  103. ->method('set')
  104. ->with('test_articles')
  105. ->willReturn(true);
  106. $this->cache->expects($this->never())
  107. ->method('delete')
  108. ->willReturn(false);
  109. $this->exec('schema_cache build --connection test articles');
  110. $this->assertExitSuccess();
  111. }
  112. /**
  113. * Test build() overwrites cached data.
  114. */
  115. public function testBuildOverwritesExistingData(): void
  116. {
  117. $this->cache->expects($this->once())
  118. ->method('set')
  119. ->with('test_articles')
  120. ->willReturn(true);
  121. $this->cache->expects($this->never())
  122. ->method('get');
  123. $this->cache->expects($this->never())
  124. ->method('delete')
  125. ->willReturn(false);
  126. $this->exec('schema_cache build --connection test articles');
  127. $this->assertExitSuccess();
  128. }
  129. /**
  130. * Test build() with a nonexistent connection name.
  131. */
  132. public function testBuildInvalidConnection(): void
  133. {
  134. $this->exec('schema_cache build --connection derpy-derp articles');
  135. $this->assertExitError();
  136. }
  137. /**
  138. * Test clear() with an invalid connection name.
  139. */
  140. public function testClearInvalidConnection(): void
  141. {
  142. $this->exec('schema_cache clear --connection derpy-derp articles');
  143. $this->assertExitError();
  144. }
  145. /**
  146. * Test clear() with no args.
  147. */
  148. public function testClearNoArgs(): void
  149. {
  150. $this->cache->expects($this->atLeastOnce())
  151. ->method('delete')
  152. ->willReturn(true);
  153. $this->exec('schema_cache clear --connection test');
  154. $this->assertExitSuccess();
  155. }
  156. /**
  157. * Test clear() with a model name.
  158. */
  159. public function testClearNamedModel(): void
  160. {
  161. $this->cache->expects($this->never())
  162. ->method('set')
  163. ->willReturn(true);
  164. $this->cache->expects($this->once())
  165. ->method('delete')
  166. ->with('test_articles')
  167. ->willReturn(false);
  168. $this->exec('schema_cache clear --connection test articles');
  169. $this->assertExitSuccess();
  170. }
  171. }