CacheTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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;
  7. class CacheTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\App\Cache
  11. */
  12. protected $_model;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject[]
  15. */
  16. protected $_cacheTypeMocks;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_cacheFrontendMock;
  21. protected function setUp()
  22. {
  23. $this->_initCacheTypeMocks();
  24. $this->_cacheFrontendMock = $this->getMockForAbstractClass(
  25. \Magento\Framework\Cache\FrontendInterface::class,
  26. [],
  27. '',
  28. true,
  29. true,
  30. true,
  31. ['clean']
  32. );
  33. $frontendPoolMock = $this->createMock(\Magento\Framework\App\Cache\Frontend\Pool::class);
  34. $frontendPoolMock->expects($this->any())->method('valid')->will($this->onConsecutiveCalls(true, false));
  35. $frontendPoolMock->expects(
  36. $this->any()
  37. )->method(
  38. 'current'
  39. )->will(
  40. $this->returnValue($this->_cacheFrontendMock)
  41. );
  42. $frontendPoolMock->expects(
  43. $this->any()
  44. )->method(
  45. 'get'
  46. )->with(
  47. \Magento\Framework\App\Cache\Frontend\Pool::DEFAULT_FRONTEND_ID
  48. )->will(
  49. $this->returnValue($this->_cacheFrontendMock)
  50. );
  51. $this->_model = new \Magento\Framework\App\Cache($frontendPoolMock);
  52. }
  53. /**
  54. * Init necessary cache type mocks
  55. */
  56. protected function _initCacheTypeMocks()
  57. {
  58. $cacheTypes = [
  59. \Magento\Framework\Cache\Frontend\Decorator\TagScope::class,
  60. \Magento\Framework\Cache\Frontend\Decorator\Bare::class,
  61. ];
  62. foreach ($cacheTypes as $type) {
  63. $this->_cacheTypeMocks[$type] = $this->getMockBuilder($type)
  64. ->setMethods(['clean'])
  65. ->setConstructorArgs(
  66. [
  67. $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class), '
  68. FIXTURE_TAG'
  69. ]
  70. )
  71. ->getMock();
  72. }
  73. }
  74. /**
  75. * Callback for the object manager to get different cache type mocks
  76. *
  77. * @param string $type Class of the cache type
  78. * @return \PHPUnit_Framework_MockObject_MockObject
  79. */
  80. public function getTypeMock($type)
  81. {
  82. return $this->_cacheTypeMocks[$type];
  83. }
  84. protected function tearDown()
  85. {
  86. $this->_cacheTypeMocks = [];
  87. $this->_cacheFrontendMock = null;
  88. $this->_model = null;
  89. }
  90. public function testConstructor()
  91. {
  92. $this->assertSame($this->_cacheFrontendMock, $this->_model->getFrontend());
  93. }
  94. public function testGetFrontend()
  95. {
  96. $frontend = $this->_model->getFrontend();
  97. $this->assertSame($this->_cacheFrontendMock, $frontend);
  98. }
  99. public function testLoad()
  100. {
  101. $this->_cacheFrontendMock->expects(
  102. $this->once()
  103. )->method(
  104. 'load'
  105. )->with(
  106. 'test_id'
  107. )->will(
  108. $this->returnValue('test_data')
  109. );
  110. $this->assertEquals('test_data', $this->_model->load('test_id'));
  111. }
  112. /**
  113. * @dataProvider saveDataProvider
  114. * @param string|mixed $inputData
  115. * @param string $inputId
  116. * @param array $inputTags
  117. * @param string $expectedData
  118. * @param string $expectedId
  119. * @param array $expectedTags
  120. */
  121. public function testSave($inputData, $inputId, $inputTags, $expectedData, $expectedId, $expectedTags)
  122. {
  123. $this->_cacheFrontendMock->expects(
  124. $this->once()
  125. )->method(
  126. 'save'
  127. )->with(
  128. $this->identicalTo($expectedData),
  129. $expectedId,
  130. $expectedTags
  131. );
  132. $this->_model->save($inputData, $inputId, $inputTags);
  133. }
  134. /**
  135. * @return array
  136. */
  137. public function saveDataProvider()
  138. {
  139. $configTag = \Magento\Framework\App\Config::CACHE_TAG;
  140. return [
  141. 'default tags' => ['test_data', 'test_id', [], 'test_data', 'test_id', []],
  142. 'config tags' => [
  143. 'test_data',
  144. 'test_id',
  145. [$configTag],
  146. 'test_data',
  147. 'test_id',
  148. [$configTag],
  149. ],
  150. 'lowercase tags' => [
  151. 'test_data',
  152. 'test_id',
  153. ['test_tag'],
  154. 'test_data',
  155. 'test_id',
  156. ['test_tag'],
  157. ],
  158. 'non-string data' => [1234567890, 'test_id', [], '1234567890', 'test_id', []]
  159. ];
  160. }
  161. /**
  162. * @dataProvider successFailureDataProvider
  163. * @param bool $result
  164. */
  165. public function testRemove($result)
  166. {
  167. $this->_cacheFrontendMock->expects(
  168. $this->once()
  169. )->method(
  170. 'remove'
  171. )->with(
  172. 'test_id'
  173. )->will(
  174. $this->returnValue($result)
  175. );
  176. $this->assertEquals($result, $this->_model->remove('test_id'));
  177. }
  178. /**
  179. * @return array
  180. */
  181. public function successFailureDataProvider()
  182. {
  183. return ['success' => [true], 'failure' => [false]];
  184. }
  185. public function testCleanByTags()
  186. {
  187. $expectedTags = ['test_tag'];
  188. $this->_cacheFrontendMock->expects(
  189. $this->once()
  190. )->method(
  191. 'clean'
  192. )->with(
  193. \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  194. $expectedTags
  195. )->will(
  196. $this->returnValue(true)
  197. );
  198. $this->assertTrue($this->_model->clean($expectedTags));
  199. }
  200. public function testCleanByEmptyTags()
  201. {
  202. $this->_cacheFrontendMock->expects(
  203. $this->once()
  204. )->method(
  205. 'clean'
  206. )->with(
  207. \Zend_Cache::CLEANING_MODE_ALL
  208. )->will(
  209. $this->returnValue(true)
  210. );
  211. $this->assertTrue($this->_model->clean());
  212. }
  213. }