CacheCommandsTest.php 5.0 KB

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