Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ScopedTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ScopedTest extends \PHPUnit\Framework\TestCase
9 {
13  private $objectManager;
14 
18  protected $_model;
19 
23  protected $_readerMock;
24 
28  protected $_configScopeMock;
29 
33  protected $_cacheMock;
34 
38  private $serializerMock;
39 
40  protected function setUp()
41  {
42  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
43  $this->_readerMock = $this->createMock(\Magento\Framework\Config\ReaderInterface::class);
44  $this->_configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
45  $this->_cacheMock = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
46  $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
47 
48  $this->_model = $this->objectManager->getObject(
49  \Magento\Framework\Config\Data\Scoped::class,
50  [
51  'reader' => $this->_readerMock,
52  'configScope' => $this->_configScopeMock,
53  'cache' => $this->_cacheMock,
54  'cacheId' => 'tag',
55  'serializer' => $this->serializerMock
56  ]
57  );
58  }
59 
66  public function testGetConfigByPath($path, $expectedValue, $default)
67  {
68  $testData = [
69  'key_1' => [
70  'key_1.1' => ['key_1.1.1' => 'value_1.1.1'],
71  'key_1.2' => ['some' => 'arrayValue'],
72  ],
73  ];
74  $this->_cacheMock->expects($this->once())
75  ->method('load')
76  ->willReturn(false);
77  $this->_readerMock->expects($this->once())
78  ->method('read')
79  ->willReturn([]);
80  $this->_model->merge($testData);
81  $this->assertEquals($expectedValue, $this->_model->get($path, $default));
82  }
83 
87  public function getConfigByPathDataProvider()
88  {
89  return [
90  ['key_1/key_1.1/key_1.1.1', 'value_1.1.1', 'error'],
91  ['key_1/key_1.2', ['some' => 'arrayValue'], 'error'],
92  [
93  'key_1',
94  ['key_1.1' => ['key_1.1.1' => 'value_1.1.1'], 'key_1.2' => ['some' => 'arrayValue']],
95  'error'
96  ],
97  ['key_1/notExistedKey', 'defaultValue', 'defaultValue']
98  ];
99  }
100 
102  {
103  $testValue = ['some' => 'testValue'];
104  $serializedData = 'serialized data';
105 
107  $this->_configScopeMock->expects(
108  $this->any()
109  )->method(
110  'getCurrentScope'
111  )->will(
112  $this->returnValue('adminhtml')
113  );
114 
116  $this->_cacheMock->expects(
117  $this->once()
118  )->method(
119  'load'
120  )->with(
121  'adminhtml::tag'
122  )->will(
123  $this->returnValue(false)
124  );
125 
127  $this->_readerMock->expects(
128  $this->once()
129  )->method(
130  'read'
131  )->with(
132  'adminhtml'
133  )->will(
134  $this->returnValue($testValue)
135  );
136 
137  $this->serializerMock->expects($this->once())
138  ->method('serialize')
139  ->with($testValue)
140  ->willReturn($serializedData);
141 
143  $this->_cacheMock->expects($this->once())
144  ->method('save')
145  ->with($serializedData, 'adminhtml::tag');
146 
148  $this->assertEquals('testValue', $this->_model->get('some'));
149 
151  $this->assertEquals('testValue', $this->_model->get('some'));
152  }
153 
155  {
156  $testValue = ['some' => 'testValue'];
157  $serializedData = 'serialized data';
158 
160  $this->_configScopeMock->expects(
161  $this->any()
162  )->method(
163  'getCurrentScope'
164  )->will(
165  $this->returnValue('adminhtml')
166  );
167 
168  $this->serializerMock->expects($this->once())
169  ->method('unserialize')
170  ->with($serializedData)
171  ->willReturn($testValue);
172 
174  $this->_cacheMock->expects($this->once())
175  ->method('load')
176  ->with('adminhtml::tag')
177  ->willReturn($serializedData);
178 
180  $this->_readerMock->expects($this->never())->method('read');
181 
183  $this->_cacheMock->expects($this->never())->method('save');
184 
186  $this->assertEquals('testValue', $this->_model->get('some'));
187 
189  $this->assertEquals('testValue', $this->_model->get('some'));
190  }
191 }
testGetConfigByPath($path, $expectedValue, $default)
Definition: ScopedTest.php:66