Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigWriterTest.php
Go to the documentation of this file.
1 <?php
7 
15 use PHPUnit_Framework_MockObject_MockObject as MockObject;
16 
17 class ConfigWriterTest extends \PHPUnit\Framework\TestCase
18 {
22  private $writerMock;
23 
27  private $arrayManagerMock;
28 
32  private $preparedValueFactoryMock;
33 
37  private $valueInterfaceMock;
38 
42  private $valueMock;
43 
47  private $model;
48 
52  public function setUp()
53  {
54  $this->arrayManagerMock = $this->getMockBuilder(ArrayManager::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->writerMock = $this->getMockBuilder(Writer::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->preparedValueFactoryMock = $this->getMockBuilder(PreparedValueFactory::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63 
64  $this->valueInterfaceMock = $this->getMockBuilder(ValueInterface::class)
65  ->getMockForAbstractClass();
66  $this->valueMock = $this->getMockBuilder(Value::class)
67  ->disableOriginalConstructor()
68  ->setMethods(['validateBeforeSave', 'beforeSave', 'getValue', 'afterSave'])
69  ->getMock();
70 
71  $this->model = new ConfigWriter(
72  $this->writerMock,
73  $this->arrayManagerMock,
74  $this->preparedValueFactoryMock
75  );
76  }
77 
78  public function testSave()
79  {
80  $values = [
81  'some1/config1/path1' => 'someValue1',
82  'some2/config2/path2' => 'someValue2',
83  'some3/config3/path3' => 'someValue3'
84  ];
85  $config = ['system' => []];
86 
87  $this->preparedValueFactoryMock->expects($this->exactly(3))
88  ->method('create')
89  ->withConsecutive(
90  ['some1/config1/path1', 'someValue1', 'scope', 'scope_code'],
91  ['some2/config2/path2', 'someValue2', 'scope', 'scope_code'],
92  ['some3/config3/path3', 'someValue3', 'scope', 'scope_code']
93  )
94  ->willReturnOnConsecutiveCalls(
95  $this->valueInterfaceMock,
96  $this->valueMock,
97  $this->valueMock
98  );
99 
100  $this->valueMock->expects($this->exactly(2))
101  ->method('validateBeforeSave');
102  $this->valueMock->expects($this->exactly(2))
103  ->method('beforeSave');
104  $this->valueMock->expects($this->exactly(2))
105  ->method('getValue')
106  ->willReturnOnConsecutiveCalls('someValue2', 'someValue3');
107  $this->valueMock->expects($this->exactly(2))
108  ->method('afterSave');
109 
110  $this->arrayManagerMock->expects($this->exactly(3))
111  ->method('set')
112  ->withConsecutive(
113  ['system/scope/scope_code/some1/config1/path1', $this->anything(), 'someValue1'],
114  ['system/scope/scope_code/some2/config2/path2', $this->anything(), 'someValue2'],
115  ['system/scope/scope_code/some3/config3/path3', $this->anything(), 'someValue3']
116  )
117  ->willReturn($config);
118  $this->writerMock->expects($this->once())
119  ->method('saveConfig')
120  ->with([ConfigFilePool::APP_ENV => $config]);
121 
122  $this->model->save($values, 'scope', 'scope_code');
123  }
124 
125  public function testSaveDefaultScope()
126  {
127  $values = [
128  'some1/config1/path1' => 'someValue1',
129  'some2/config2/path2' => 'someValue2',
130  'some3/config3/path3' => 'someValue3'
131  ];
132  $config = ['system' => []];
133 
134  $this->preparedValueFactoryMock->expects($this->exactly(3))
135  ->method('create')
136  ->withConsecutive(
137  ['some1/config1/path1', 'someValue1', 'default'],
138  ['some2/config2/path2', 'someValue2', 'default'],
139  ['some3/config3/path3', 'someValue3', 'default']
140  )
141  ->willReturnOnConsecutiveCalls(
142  $this->valueInterfaceMock,
143  $this->valueMock,
144  $this->valueMock
145  );
146 
147  $this->valueMock->expects($this->exactly(2))
148  ->method('validateBeforeSave');
149  $this->valueMock->expects($this->exactly(2))
150  ->method('beforeSave');
151  $this->valueMock->expects($this->exactly(2))
152  ->method('getValue')
153  ->willReturnOnConsecutiveCalls('someValue2', 'someValue3');
154  $this->valueMock->expects($this->exactly(2))
155  ->method('afterSave');
156 
157  $this->arrayManagerMock->expects($this->exactly(3))
158  ->method('set')
159  ->withConsecutive(
160  ['system/default/some1/config1/path1', $this->anything(), 'someValue1'],
161  ['system/default/some2/config2/path2', $this->anything(), 'someValue2'],
162  ['system/default/some3/config3/path3', $this->anything(), 'someValue3']
163  )
164  ->willReturn($config);
165  $this->writerMock->expects($this->once())
166  ->method('saveConfig')
167  ->with([ConfigFilePool::APP_ENV => $config]);
168 
169  $this->model->save($values);
170  }
171 
176  public function testSavingNullValues()
177  {
178  $values = [
179  'some1/config1/path1' => null,
180  ];
181 
182  $this->preparedValueFactoryMock->expects($this->never())->method('create');
183 
184  $this->writerMock->expects($this->once())
185  ->method('saveConfig')
186  ->with([ConfigFilePool::APP_ENV => []]);
187 
188  $this->model->save($values);
189  }
190 }
$config
Definition: fraud_order.php:17
$values
Definition: options.phtml:88