CacheShellTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell;
  16. use Cake\Cache\Cache;
  17. use Cake\Console\Exception\StopException;
  18. use Cake\Console\Shell;
  19. use Cake\Shell\CacheShell;
  20. use Cake\TestSuite\ConsoleIntegrationTestCase;
  21. /**
  22. * CacheShell tests.
  23. */
  24. class CacheShellTest extends ConsoleIntegrationTestCase
  25. {
  26. /**
  27. * setup method
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. Cache::config('test', ['engine' => 'File', 'path' => CACHE]);
  35. }
  36. /**
  37. * Teardown
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. parent::tearDown();
  44. Cache::drop('test');
  45. }
  46. /**
  47. * Test that getOptionParser() returns an instance of \Cake\Console\ConsoleOptionParser
  48. *
  49. * @return void
  50. */
  51. public function testGetOptionParser()
  52. {
  53. $this->exec('cache -h');
  54. $this->assertExitCode(Shell::CODE_SUCCESS);
  55. $this->assertOutputContains('list_prefixes');
  56. $this->assertOutputContains('clear_all');
  57. }
  58. /**
  59. * Test that clear() throws \Cake\Console\Exception\StopException if cache prefix is invalid
  60. *
  61. * @return void
  62. */
  63. public function testClearInvalidPrefix()
  64. {
  65. $this->exec('cache clear foo');
  66. $this->assertExitCode(Shell::CODE_ERROR);
  67. $this->assertErrorContains('The "foo" cache configuration does not exist');
  68. }
  69. /**
  70. * Test that clear() clears the specified cache when a valid prefix is used
  71. *
  72. * @return void
  73. */
  74. public function testClearValidPrefix()
  75. {
  76. Cache::add('key', 'value', 'test');
  77. $this->exec('cache clear test');
  78. $this->assertExitCode(Shell::CODE_SUCCESS);
  79. $this->assertFalse(Cache::read('key', 'test'));
  80. }
  81. /**
  82. * Test that clear() only clears the specified cache
  83. *
  84. * @return void
  85. */
  86. public function testClearIgnoresOtherCaches()
  87. {
  88. Cache::add('key', 'value', 'test');
  89. $this->exec('cache clear _cake_core_');
  90. $this->assertExitCode(Shell::CODE_SUCCESS);
  91. $this->assertEquals('value', Cache::read('key', 'test'));
  92. }
  93. /**
  94. * Test that clearAll() clears values from all defined caches
  95. *
  96. * @return void
  97. */
  98. public function testClearAll()
  99. {
  100. Cache::add('key', 'value1', 'test');
  101. Cache::add('key', 'value3', '_cake_core_');
  102. $this->exec('cache clear_all');
  103. $this->assertExitCode(Shell::CODE_SUCCESS);
  104. $this->assertFalse(Cache::read('key', 'test'));
  105. $this->assertFalse(Cache::read('key', '_cake_core_'));
  106. }
  107. }