CacheCommandsTest.php 3.9 KB

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