CacheCommandsTest.php 3.8 KB

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