CacheCommandsTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.3.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\Console\CommandInterface;
  19. use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Cache Commands tests.
  23. */
  24. class CacheCommandsTest extends TestCase
  25. {
  26. use ConsoleIntegrationTestTrait;
  27. /**
  28. * setup method
  29. */
  30. public function setUp(): void
  31. {
  32. parent::setUp();
  33. Cache::setConfig('test', ['engine' => 'File', 'path' => CACHE, 'groups' => ['test_group']]);
  34. Cache::setConfig('test2', ['engine' => 'File', 'path' => CACHE, 'groups' => ['test_group']]);
  35. $this->setAppNamespace();
  36. }
  37. /**
  38. * Teardown
  39. */
  40. public function tearDown(): void
  41. {
  42. parent::tearDown();
  43. Cache::drop('test');
  44. Cache::drop('test2');
  45. }
  46. /**
  47. * Test help output
  48. */
  49. public function testClearHelp(): void
  50. {
  51. $this->exec('cache clear -h');
  52. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  53. $this->assertOutputContains('engine to clear');
  54. }
  55. /**
  56. * Test help output
  57. */
  58. public function testClearAllHelp(): void
  59. {
  60. $this->exec('cache clear_all -h');
  61. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  62. $this->assertOutputContains('Clear all');
  63. }
  64. /**
  65. * Test list output
  66. */
  67. public function testList(): void
  68. {
  69. $this->exec('cache list');
  70. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  71. $this->assertOutputContains('- test');
  72. $this->assertOutputContains('- _cake_translations_');
  73. $this->assertOutputContains('- _cake_model_');
  74. }
  75. /**
  76. * Test help output
  77. */
  78. public function testListHelp(): void
  79. {
  80. $this->exec('cache list -h');
  81. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  82. $this->assertOutputContains('Show a list');
  83. }
  84. /**
  85. * Test that clear() throws \Cake\Console\Exception\StopException if cache prefix is invalid
  86. */
  87. public function testClearInvalidPrefix(): void
  88. {
  89. $this->exec('cache clear foo');
  90. $this->assertExitCode(CommandInterface::CODE_ERROR);
  91. $this->assertErrorContains('The `foo` cache configuration does not exist');
  92. }
  93. /**
  94. * Test that clear() clears the specified cache when a valid prefix is used
  95. */
  96. public function testClearValidPrefix(): void
  97. {
  98. Cache::add('key', 'value', 'test');
  99. $this->exec('cache clear test');
  100. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  101. $this->assertNull(Cache::read('key', 'test'));
  102. }
  103. /**
  104. * Test that clear() only clears the specified cache
  105. */
  106. public function testClearIgnoresOtherCaches(): void
  107. {
  108. Cache::add('key', 'value', 'test');
  109. $this->exec('cache clear _cake_translations_');
  110. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  111. $this->assertSame('value', Cache::read('key', 'test'));
  112. }
  113. /**
  114. * Test that clearAll() clears values from all defined caches
  115. */
  116. public function testClearAll(): void
  117. {
  118. Cache::add('key', 'value1', 'test');
  119. Cache::add('key', 'value3', '_cake_translations_');
  120. $this->exec('cache clear_all');
  121. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  122. $this->assertNull(Cache::read('key', 'test'));
  123. $this->assertNull(Cache::read('key', '_cake_translations_'));
  124. }
  125. public function testClearGroup(): void
  126. {
  127. Cache::add('key', 'value1', 'test');
  128. Cache::add('key', 'value1', 'test2');
  129. $this->exec('cache clear_group test_group');
  130. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  131. $this->assertNull(Cache::read('key', 'test'));
  132. $this->assertNull(Cache::read('key', 'test2'));
  133. }
  134. public function testClearGroupWithConfig(): void
  135. {
  136. Cache::add('key', 'value1', 'test');
  137. $this->exec('cache clear_group test_group test');
  138. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  139. $this->assertNull(Cache::read('key', 'test'));
  140. }
  141. public function testClearGroupInvalidConfig(): void
  142. {
  143. $this->exec('cache clear_group test_group does_not_exist');
  144. $this->assertExitCode(CommandInterface::CODE_ERROR);
  145. $this->assertErrorContains('Cache config "does_not_exist" not found');
  146. }
  147. public function testClearInvalidGroup(): void
  148. {
  149. $this->exec('cache clear_group does_not_exist');
  150. $this->assertExitCode(CommandInterface::CODE_ERROR);
  151. $this->assertErrorContains('Cache group "does_not_exist" not found');
  152. }
  153. }