ScopedTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config\Test\Unit\Data;
  7. class ScopedTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  11. */
  12. private $objectManager;
  13. /**
  14. * @var \Magento\Framework\Config\Data\Scoped
  15. */
  16. protected $_model;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_readerMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_configScopeMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $_cacheMock;
  29. /**
  30. * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $serializerMock;
  33. protected function setUp()
  34. {
  35. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  36. $this->_readerMock = $this->createMock(\Magento\Framework\Config\ReaderInterface::class);
  37. $this->_configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
  38. $this->_cacheMock = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
  39. $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
  40. $this->_model = $this->objectManager->getObject(
  41. \Magento\Framework\Config\Data\Scoped::class,
  42. [
  43. 'reader' => $this->_readerMock,
  44. 'configScope' => $this->_configScopeMock,
  45. 'cache' => $this->_cacheMock,
  46. 'cacheId' => 'tag',
  47. 'serializer' => $this->serializerMock
  48. ]
  49. );
  50. }
  51. /**
  52. * @param string $path
  53. * @param mixed $expectedValue
  54. * @param string $default
  55. * @dataProvider getConfigByPathDataProvider
  56. */
  57. public function testGetConfigByPath($path, $expectedValue, $default)
  58. {
  59. $testData = [
  60. 'key_1' => [
  61. 'key_1.1' => ['key_1.1.1' => 'value_1.1.1'],
  62. 'key_1.2' => ['some' => 'arrayValue'],
  63. ],
  64. ];
  65. $this->_cacheMock->expects($this->once())
  66. ->method('load')
  67. ->willReturn(false);
  68. $this->_readerMock->expects($this->once())
  69. ->method('read')
  70. ->willReturn([]);
  71. $this->_model->merge($testData);
  72. $this->assertEquals($expectedValue, $this->_model->get($path, $default));
  73. }
  74. /**
  75. * @return array
  76. */
  77. public function getConfigByPathDataProvider()
  78. {
  79. return [
  80. ['key_1/key_1.1/key_1.1.1', 'value_1.1.1', 'error'],
  81. ['key_1/key_1.2', ['some' => 'arrayValue'], 'error'],
  82. [
  83. 'key_1',
  84. ['key_1.1' => ['key_1.1.1' => 'value_1.1.1'], 'key_1.2' => ['some' => 'arrayValue']],
  85. 'error'
  86. ],
  87. ['key_1/notExistedKey', 'defaultValue', 'defaultValue']
  88. ];
  89. }
  90. public function testGetScopeSwitchingWithNonCachedData()
  91. {
  92. $testValue = ['some' => 'testValue'];
  93. $serializedData = 'serialized data';
  94. /** change current area */
  95. $this->_configScopeMock->expects(
  96. $this->any()
  97. )->method(
  98. 'getCurrentScope'
  99. )->will(
  100. $this->returnValue('adminhtml')
  101. );
  102. /** set empty cache data */
  103. $this->_cacheMock->expects(
  104. $this->once()
  105. )->method(
  106. 'load'
  107. )->with(
  108. 'adminhtml::tag'
  109. )->will(
  110. $this->returnValue(false)
  111. );
  112. /** get data from reader */
  113. $this->_readerMock->expects(
  114. $this->once()
  115. )->method(
  116. 'read'
  117. )->with(
  118. 'adminhtml'
  119. )->will(
  120. $this->returnValue($testValue)
  121. );
  122. $this->serializerMock->expects($this->once())
  123. ->method('serialize')
  124. ->with($testValue)
  125. ->willReturn($serializedData);
  126. /** test cache saving */
  127. $this->_cacheMock->expects($this->once())
  128. ->method('save')
  129. ->with($serializedData, 'adminhtml::tag');
  130. /** test config value existence */
  131. $this->assertEquals('testValue', $this->_model->get('some'));
  132. /** test preventing of double config data loading from reader */
  133. $this->assertEquals('testValue', $this->_model->get('some'));
  134. }
  135. public function testGetScopeSwitchingWithCachedData()
  136. {
  137. $testValue = ['some' => 'testValue'];
  138. $serializedData = 'serialized data';
  139. /** change current area */
  140. $this->_configScopeMock->expects(
  141. $this->any()
  142. )->method(
  143. 'getCurrentScope'
  144. )->will(
  145. $this->returnValue('adminhtml')
  146. );
  147. $this->serializerMock->expects($this->once())
  148. ->method('unserialize')
  149. ->with($serializedData)
  150. ->willReturn($testValue);
  151. /** set cache data */
  152. $this->_cacheMock->expects($this->once())
  153. ->method('load')
  154. ->with('adminhtml::tag')
  155. ->willReturn($serializedData);
  156. /** test preventing of getting data from reader */
  157. $this->_readerMock->expects($this->never())->method('read');
  158. /** test preventing of cache saving */
  159. $this->_cacheMock->expects($this->never())->method('save');
  160. /** test config value existence */
  161. $this->assertEquals('testValue', $this->_model->get('some'));
  162. /** test preventing of double config data loading from reader */
  163. $this->assertEquals('testValue', $this->_model->get('some'));
  164. }
  165. }