ConfigCacheTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\ObjectManager;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. class ConfigCacheTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\App\ObjectManager\ConfigCache
  12. */
  13. private $configCache;
  14. /**
  15. * @var \Magento\Framework\App\ObjectManager\ConfigCache|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $cacheFrontendMock;
  18. /**
  19. * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $serializerMock;
  22. protected function setUp()
  23. {
  24. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  25. $this->cacheFrontendMock = $this->createMock(\Magento\Framework\Cache\FrontendInterface::class);
  26. $this->configCache = $objectManagerHelper->getObject(
  27. \Magento\Framework\App\ObjectManager\ConfigCache::class,
  28. ['cacheFrontend' => $this->cacheFrontendMock]
  29. );
  30. $this->serializerMock = $this->createMock(SerializerInterface::class);
  31. $objectManagerHelper->setBackwardCompatibleProperty(
  32. $this->configCache,
  33. 'serializer',
  34. $this->serializerMock
  35. );
  36. }
  37. protected function tearDown()
  38. {
  39. unset($this->configCache);
  40. }
  41. /**
  42. * @param $data
  43. * @param $expectedResult
  44. * @param $unserializeCalledNum
  45. * @dataProvider getDataProvider
  46. */
  47. public function testGet($data, $expectedResult, $unserializeCalledNum = 1)
  48. {
  49. $key = 'key';
  50. $this->cacheFrontendMock->expects($this->once())
  51. ->method('load')
  52. ->with('diConfig' . $key)
  53. ->willReturn($data);
  54. $this->serializerMock->expects($this->exactly($unserializeCalledNum))
  55. ->method('unserialize')
  56. ->with($data)
  57. ->willReturn($expectedResult);
  58. $this->assertEquals($expectedResult, $this->configCache->get($key));
  59. }
  60. /**
  61. * @return array
  62. */
  63. public function getDataProvider()
  64. {
  65. return [
  66. [false, false, 0],
  67. ['serialized data', ['some data']],
  68. ];
  69. }
  70. public function testSave()
  71. {
  72. $key = 'key';
  73. $config = ['config'];
  74. $serializedData = 'serialized data';
  75. $this->serializerMock->expects($this->once())
  76. ->method('serialize')
  77. ->willReturn($serializedData);
  78. $this->cacheFrontendMock->expects($this->once())->method('save')->with($serializedData, 'diConfig' . $key);
  79. $this->configCache->save($config, $key);
  80. }
  81. }