ConfigLoaderTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ConfigLoaderTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\App\ObjectManager\ConfigLoader
  12. */
  13. private $object;
  14. /**
  15. * @var \Magento\Framework\ObjectManager\Config\Reader\DomFactory|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $readerFactoryMock;
  18. /**
  19. * @var \Magento\Framework\ObjectManager\Config\Reader\Dom|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $readerMock;
  22. /**
  23. * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $cacheMock;
  26. /**
  27. * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $serializerMock;
  30. protected function setUp()
  31. {
  32. $this->readerMock = $this->createMock(\Magento\Framework\ObjectManager\Config\Reader\Dom::class);
  33. $this->readerFactoryMock =
  34. $this->createPartialMock(\Magento\Framework\ObjectManager\Config\Reader\DomFactory::class, ['create']);
  35. $this->readerFactoryMock->expects($this->any())
  36. ->method('create')
  37. ->will($this->returnValue($this->readerMock));
  38. $this->cacheMock = $this->createMock(\Magento\Framework\App\Cache\Type\Config::class);
  39. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  40. $this->object = $objectManagerHelper->getObject(
  41. \Magento\Framework\App\ObjectManager\ConfigLoader::class,
  42. [
  43. 'cache' => $this->cacheMock,
  44. 'readerFactory' => $this->readerFactoryMock,
  45. ]
  46. );
  47. $this->serializerMock = $this->createMock(SerializerInterface::class);
  48. $objectManagerHelper->setBackwardCompatibleProperty(
  49. $this->object,
  50. 'serializer',
  51. $this->serializerMock
  52. );
  53. }
  54. /**
  55. * @param $area
  56. * @dataProvider loadDataProvider
  57. */
  58. public function testLoadNotCached($area)
  59. {
  60. $configData = ['some' => 'config', 'data' => 'value'];
  61. $serializedData = 'serialized data';
  62. $this->cacheMock->expects($this->once())
  63. ->method('load')
  64. ->with($area . '::DiConfig')
  65. ->will($this->returnValue(false));
  66. $this->cacheMock->expects($this->once())
  67. ->method('save')
  68. ->with($serializedData);
  69. $this->readerMock->expects($this->once())
  70. ->method('read')
  71. ->with($area)
  72. ->will($this->returnValue($configData));
  73. $this->serializerMock->expects($this->once())
  74. ->method('serialize')
  75. ->willReturn($serializedData);
  76. $this->serializerMock->expects($this->never())->method('unserialize');
  77. $this->assertEquals($configData, $this->object->load($area));
  78. }
  79. /**
  80. * Data provider
  81. *
  82. * @return array
  83. */
  84. public function loadDataProvider()
  85. {
  86. return [
  87. 'global files' => ['global'],
  88. 'adminhtml files' => ['adminhtml'],
  89. 'any area files' => ['any']
  90. ];
  91. }
  92. public function testLoadCached()
  93. {
  94. $configData = ['some' => 'config', 'data' => 'value'];
  95. $serializedData = 'serialized data';
  96. $this->cacheMock->expects($this->once())
  97. ->method('load')
  98. ->willReturn($serializedData);
  99. $this->cacheMock->expects($this->never())
  100. ->method('save');
  101. $this->readerMock->expects($this->never())->method('read');
  102. $this->serializerMock->expects($this->once())
  103. ->method('unserialize')
  104. ->with($serializedData)
  105. ->willReturn($configData);
  106. $this->serializerMock->expects($this->never())->method('serialize');
  107. $this->assertEquals($configData, $this->object->load('testArea'));
  108. }
  109. }