CacheCommandsTest.php 4.5 KB

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