ManagerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\Cache;
  7. use \Magento\Framework\App\Cache\Manager;
  8. class ManagerTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\TypeListInterface
  12. */
  13. private $cacheTypeList;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\StateInterface
  16. */
  17. private $cacheState;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Console\Response
  20. */
  21. private $response;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\Type\FrontendPool
  24. */
  25. private $frontendPool;
  26. /**
  27. * @var Manager
  28. */
  29. private $model;
  30. protected function setUp()
  31. {
  32. $this->cacheTypeList = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\TypeListInterface::class);
  33. $this->cacheState = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\StateInterface::class);
  34. $this->response = $this->createMock(\Magento\Framework\App\Console\Response::class);
  35. $this->frontendPool = $this->createMock(\Magento\Framework\App\Cache\Type\FrontendPool::class);
  36. $this->model = new Manager($this->cacheTypeList, $this->cacheState, $this->frontendPool);
  37. }
  38. public function testEmptyRequest()
  39. {
  40. $this->cacheState->expects($this->never())->method('setEnabled');
  41. $this->cacheState->expects($this->never())->method('persist');
  42. $this->frontendPool->expects($this->never())->method('get');
  43. $this->model->setEnabled([], true);
  44. }
  45. /**
  46. * Test setting all cache types to true
  47. *
  48. * In this fixture, there are 2 of 3 cache types disabled, but will be enabled
  49. * so persist() should be invoked once, then clean() for each of those which changed
  50. */
  51. public function testSetEnabledTrueAll()
  52. {
  53. $caches = ['foo', 'bar', 'baz'];
  54. $cacheStatusMap = [['foo', true], ['bar', false], ['baz', false]];
  55. $this->cacheState->expects($this->exactly(3))
  56. ->method('isEnabled')
  57. ->will($this->returnValueMap($cacheStatusMap));
  58. $this->cacheState->expects($this->exactly(2))
  59. ->method('setEnabled')
  60. ->will($this->returnValueMap([['bar', true], ['baz', true]]));
  61. $this->cacheState->expects($this->once())->method('persist');
  62. $this->assertEquals(['bar', 'baz'], $this->model->setEnabled($caches, true));
  63. }
  64. /**
  65. * Test setting all cache types to true
  66. *
  67. * Fixture is the same as in previous test, but here the intent to disable all of them.
  68. * Since only one of them is enabled, the setter should be invoked only once.
  69. * Also the operation of deactivating cache does not need cleanup (it is relevant when you enable it).
  70. */
  71. public function testSetEnabledFalseAll()
  72. {
  73. $caches = ['foo', 'bar', 'baz'];
  74. $cacheStatusMap = [['foo', true], ['bar', false], ['baz', false]];
  75. $this->cacheState->expects($this->exactly(3))
  76. ->method('isEnabled')
  77. ->will($this->returnValueMap($cacheStatusMap));
  78. $this->cacheState->expects($this->once())->method('setEnabled')->with('foo', false);
  79. $this->cacheState->expects($this->once())->method('persist');
  80. $this->frontendPool->expects($this->never())->method('get');
  81. $this->assertEquals(['foo'], $this->model->setEnabled($caches, false));
  82. }
  83. /**
  84. * Test flushing all cache types
  85. *
  86. * Emulates situation when some cache frontends reuse the same backend
  87. * Asserts that the flush is invoked only once per affected storage
  88. */
  89. public function testFlushAll()
  90. {
  91. $cacheTypes = ['foo', 'bar', 'baz'];
  92. $frontendFoo = $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class);
  93. $frontendBar = $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class);
  94. $frontendBaz = $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class);
  95. $this->frontendPool->expects($this->exactly(3))->method('get')->will($this->returnValueMap([
  96. ['foo', $frontendFoo],
  97. ['bar', $frontendBar],
  98. ['baz', $frontendBaz],
  99. ]));
  100. $backendOne = $this->getMockForAbstractClass(\Zend_Cache_Backend_Interface::class);
  101. $backendTwo = $this->getMockForAbstractClass(\Zend_Cache_Backend_Interface::class);
  102. $frontendFoo->expects($this->once())->method('getBackend')->willReturn($backendOne);
  103. $frontendBar->expects($this->once())->method('getBackend')->willReturn($backendOne);
  104. $frontendBaz->expects($this->once())->method('getBackend')->willReturn($backendTwo);
  105. $backendOne->expects($this->once())->method('clean');
  106. $backendTwo->expects($this->once())->method('clean');
  107. $this->model->flush($cacheTypes);
  108. }
  109. public function testGetStatus()
  110. {
  111. $types = [
  112. ['id' => 'foo', 'status' => true],
  113. ['id' => 'bar', 'status' => false],
  114. ];
  115. $this->cacheTypeList->expects($this->once())->method('getTypes')->willReturn($types);
  116. $this->assertSame(['foo' => true, 'bar' => false], $this->model->getStatus());
  117. }
  118. public function testGetAvailableTypes()
  119. {
  120. $types = [
  121. ['id' => 'foo', 'status' => true],
  122. ['id' => 'bar', 'status' => false],
  123. ];
  124. $this->cacheTypeList->expects($this->once())->method('getTypes')->willReturn($types);
  125. $this->assertSame(['foo', 'bar'], $this->model->getAvailableTypes());
  126. }
  127. }