CacheShellTest.php 3.1 KB

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