CacheCommandsTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 clearall -h');
  66. $this->assertExitCode(Shell::CODE_SUCCESS);
  67. $this->assertOutputContains('Clear all');
  68. }
  69. /**
  70. * Test help output
  71. *
  72. * @return void
  73. */
  74. public function testListHelp()
  75. {
  76. $this->exec('cache list -h');
  77. $this->assertExitCode(Shell::CODE_SUCCESS);
  78. $this->assertOutputContains('Show a list');
  79. }
  80. /**
  81. * Test that clear() throws \Cake\Console\Exception\StopException if cache prefix is invalid
  82. *
  83. * @return void
  84. */
  85. public function testClearInvalidPrefix()
  86. {
  87. $this->exec('cache clear foo');
  88. $this->assertExitCode(Shell::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. * @return void
  95. */
  96. public function testClearValidPrefix()
  97. {
  98. Cache::add('key', 'value', 'test');
  99. $this->exec('cache clear test');
  100. $this->assertExitCode(Shell::CODE_SUCCESS);
  101. $this->assertNull(Cache::read('key', 'test'));
  102. }
  103. /**
  104. * Test that clear() only clears the specified cache
  105. *
  106. * @return void
  107. */
  108. public function testClearIgnoresOtherCaches()
  109. {
  110. Cache::add('key', 'value', 'test');
  111. $this->exec('cache clear _cake_core_');
  112. $this->assertExitCode(Shell::CODE_SUCCESS);
  113. $this->assertSame('value', Cache::read('key', 'test'));
  114. }
  115. /**
  116. * Test that clearAll() clears values from all defined caches
  117. *
  118. * @return void
  119. */
  120. public function testClearAll()
  121. {
  122. Cache::add('key', 'value1', 'test');
  123. Cache::add('key', 'value3', '_cake_core_');
  124. $this->exec('cache clearall');
  125. $this->assertExitCode(Shell::CODE_SUCCESS);
  126. $this->assertNull(Cache::read('key', 'test'));
  127. $this->assertNull(Cache::read('key', '_cake_core_'));
  128. }
  129. }