Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigModelTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class ConfigModelTest extends \PHPUnit\Framework\TestCase
13 {
17  private $configModel;
18 
22  private $collector;
23 
27  private $writer;
28 
32  private $deploymentConfig;
33 
37  private $configData;
38 
42  private $filePermissions;
43 
47  private $configOptionsList;
48 
49  public function setUp()
50  {
51  $this->collector = $this->createMock(\Magento\Setup\Model\ConfigOptionsListCollector::class);
52  $this->writer = $this->createMock(\Magento\Framework\App\DeploymentConfig\Writer::class);
53  $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
54  $this->configOptionsList = $this->createMock(\Magento\Backend\Setup\ConfigOptionsList::class);
55  $this->configData = $this->createMock(\Magento\Framework\Config\Data\ConfigData::class);
56  $this->filePermissions = $this->createMock(\Magento\Framework\Setup\FilePermissions::class);
57 
58  $this->deploymentConfig->expects($this->any())->method('get');
59 
60  $this->configModel = new ConfigModel(
61  $this->collector,
62  $this->writer,
63  $this->deploymentConfig,
64  $this->filePermissions
65  );
66  }
67 
68  public function testValidate()
69  {
70  $option = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
71  $option->expects($this->exactly(3))->method('getName')->willReturn('Fake');
72  $optionsSet = [
73  $option,
74  $option,
75  $option
76  ];
77  $configOption = $this->configOptionsList;
78  $configOption->expects($this->once())->method('getOptions')->will($this->returnValue($optionsSet));
79  $configOption->expects($this->once())->method('validate')->will($this->returnValue([]));
80 
81  $this->collector
82  ->expects($this->exactly(2))
83  ->method('collectOptionsLists')
84  ->will($this->returnValue([$configOption]));
85 
86  $this->configModel->validate(['Fake' => null]);
87  }
88 
89  public function testProcess()
90  {
91  $testSet1 = [
93  'segment' => [
94  'someKey' => 'value',
95  'test' => 'value1'
96  ]
97  ]
98  ];
99 
100  $testSet2 = [
102  'segment' => [
103  'test' => 'value2'
104  ]
105  ]
106  ];
107 
108  $testSetExpected1 = [
110  'segment' => [
111  'someKey' => 'value',
112  'test' => 'value1'
113  ]
114  ]
115  ];
116 
117  $testSetExpected2 = [
119  'segment' => [
120  'test' => 'value2'
121  ]
122  ]
123  ];
124 
125  $configData1 = clone $this->configData;
126  $configData2 = clone $this->configData;
127 
128  $configData1->expects($this->any())
129  ->method('getData')
130  ->will($this->returnValue($testSet1[ConfigFilePool::APP_CONFIG]));
131  $configData1->expects($this->any())->method('getFileKey')->will($this->returnValue(ConfigFilePool::APP_CONFIG));
132  $configData1->expects($this->once())->method('isOverrideWhenSave')->willReturn(false);
133 
134  $configData2->expects($this->any())
135  ->method('getData')
136  ->will($this->returnValue($testSet2[ConfigFilePool::APP_CONFIG]));
137  $configData2->expects($this->any())->method('getFileKey')->will($this->returnValue(ConfigFilePool::APP_CONFIG));
138  $configData2->expects($this->once())->method('isOverrideWhenSave')->willReturn(false);
139 
140  $configOption = $this->configOptionsList;
141  $configOption->expects($this->once())
142  ->method('createConfig')
143  ->will($this->returnValue([$configData1, $configData2]));
144 
145  $configOptionsList = [
146  'Fake_Module' => $configOption
147  ];
148  $this->collector->expects($this->once())
149  ->method('collectOptionsLists')
150  ->will($this->returnValue($configOptionsList));
151 
152  $this->writer->expects($this->at(0))->method('saveConfig')->with($testSetExpected1);
153  $this->writer->expects($this->at(1))->method('saveConfig')->with($testSetExpected2);
154 
155  $this->configModel->process([]);
156  }
157 
162  public function testProcessException()
163  {
164  $configOption = $this->configOptionsList;
165  $configOption->expects($this->once())
166  ->method('createConfig')
167  ->will($this->returnValue([null]));
168 
169  $wrongData = [
170  'Fake_Module' => $configOption
171  ];
172 
173  $this->collector->expects($this->once())->method('collectOptionsLists')->will($this->returnValue($wrongData));
174 
175  $this->configModel->process([]);
176  }
177 
182  public function testWritePermissionErrors()
183  {
184  $this->filePermissions->expects($this->once())->method('getMissingWritablePathsForInstallation')
185  ->willReturn(['/a/ro/dir', '/media']);
186  $this->configModel->process([]);
187  }
188 }