CacheShellTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Shell;
  17. use Cake\Cache\Cache;
  18. use Cake\Console\Shell;
  19. use Cake\TestSuite\ConsoleIntegrationTestCase;
  20. /**
  21. * CacheShell tests.
  22. */
  23. class CacheShellTest extends ConsoleIntegrationTestCase
  24. {
  25. /**
  26. * setup method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. Cache::setConfig('test', ['engine' => 'File', 'path' => CACHE]);
  34. }
  35. /**
  36. * Teardown
  37. *
  38. * @return void
  39. */
  40. public function tearDown()
  41. {
  42. parent::tearDown();
  43. Cache::drop('test');
  44. }
  45. /**
  46. * Test that getOptionParser() returns an instance of \Cake\Console\ConsoleOptionParser
  47. *
  48. * @return void
  49. */
  50. public function testGetOptionParser()
  51. {
  52. $this->exec('cache -h');
  53. $this->assertExitCode(Shell::CODE_SUCCESS);
  54. $this->assertOutputContains('list_prefixes');
  55. $this->assertOutputContains('clear_all');
  56. }
  57. /**
  58. * Test that clear() throws \Cake\Console\Exception\StopException if cache prefix is invalid
  59. *
  60. * @return void
  61. */
  62. public function testClearInvalidPrefix()
  63. {
  64. $this->exec('cache clear foo');
  65. $this->assertExitCode(Shell::CODE_ERROR);
  66. $this->assertErrorContains('The "foo" cache configuration does not exist');
  67. }
  68. /**
  69. * Test that clear() clears the specified cache when a valid prefix is used
  70. *
  71. * @return void
  72. */
  73. public function testClearValidPrefix()
  74. {
  75. Cache::add('key', 'value', 'test');
  76. $this->exec('cache clear test');
  77. $this->assertExitCode(Shell::CODE_SUCCESS);
  78. $this->assertNull(Cache::read('key', 'test'));
  79. }
  80. /**
  81. * Test that clear() only clears the specified cache
  82. *
  83. * @return void
  84. */
  85. public function testClearIgnoresOtherCaches()
  86. {
  87. Cache::add('key', 'value', 'test');
  88. $this->exec('cache clear _cake_core_');
  89. $this->assertExitCode(Shell::CODE_SUCCESS);
  90. $this->assertEquals('value', Cache::read('key', 'test'));
  91. }
  92. /**
  93. * Test that clearAll() clears values from all defined caches
  94. *
  95. * @return void
  96. */
  97. public function testClearAll()
  98. {
  99. Cache::add('key', 'value1', 'test');
  100. Cache::add('key', 'value3', '_cake_core_');
  101. $this->exec('cache clear_all');
  102. $this->assertExitCode(Shell::CODE_SUCCESS);
  103. $this->assertNull(Cache::read('key', 'test'));
  104. $this->assertNull(Cache::read('key', '_cake_core_'));
  105. }
  106. }