CacheShellTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license http://www.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\Shell\CacheShell;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * CacheShell tests.
  22. */
  23. class CacheShellTest extends TestCase
  24. {
  25. /**
  26. * setup method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  34. $this->shell = new CacheShell($this->io);
  35. Cache::config('test', ['engine' => 'File', 'path' => CACHE]);
  36. }
  37. /**
  38. * Teardown
  39. *
  40. * @return void
  41. */
  42. public function tearDown()
  43. {
  44. parent::tearDown();
  45. unset($this->io);
  46. unset($this->shell);
  47. Cache::drop('test');
  48. }
  49. /**
  50. * Test that getOptionParser() returns an instance of \Cake\Console\ConsoleOptionParser
  51. *
  52. * @return void
  53. */
  54. public function testGetOptionParser()
  55. {
  56. $this->assertInstanceOf('Cake\Console\ConsoleOptionParser', $this->shell->getOptionParser());
  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->expectException(StopException::class);
  66. $this->shell->clear('foo');
  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->shell->clear('test');
  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->shell->clear('_cake_core_');
  88. $this->assertEquals('value', Cache::read('key', 'test'));
  89. }
  90. /**
  91. * Test that clearAll() clears values from all defined caches
  92. *
  93. * @return void
  94. */
  95. public function testClearAll()
  96. {
  97. Cache::add('key', 'value1', 'test');
  98. Cache::add('key', 'value3', '_cake_core_');
  99. $this->shell->clearAll();
  100. $this->assertFalse(Cache::read('key', 'test'));
  101. $this->assertFalse(Cache::read('key', '_cake_core_'));
  102. }
  103. }