Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SensitiveConfigSetCommandTest.php
Go to the documentation of this file.
1 <?php
7 
19 use Symfony\Component\Console\Helper\QuestionHelper;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Output\OutputInterface;
22 
26 class SensitiveConfigSetCommandTest extends \PHPUnit\Framework\TestCase
27 {
31  private $objectManager;
32 
36  private $reader;
37 
41  private $writer;
42 
46  private $configFilePool;
47 
51  private $envConfig;
52 
56  private $config;
57 
61  private $filesystem;
62 
66  public function setUp()
67  {
68  $this->objectManager = Bootstrap::getObjectManager();
69  $this->reader = $this->objectManager->get(FileReader::class);
70  $this->writer = $this->objectManager->get(Writer::class);
71  $this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
72  $this->filesystem = $this->objectManager->get(Filesystem::class);
73 
74  $this->envConfig = $this->loadEnvConfig();
75  $this->config = $this->loadConfig();
76 
77  $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
78  $this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
79  file_get_contents(__DIR__ . '/../../../_files/config.php')
80  );
81  }
82 
92  public function testExecute($scope, $scopeCode, callable $assertCallback)
93  {
94  $outputMock = $this->createMock(OutputInterface::class);
95  $outputMock->expects($this->at(0))
96  ->method('writeln')
97  ->with('<info>Configuration value saved in app/etc/env.php</info>');
98 
99  $inputMocks = [];
100 
101  $inputMocks[] = $this->createInputMock(
102  'some/config/path_two',
103  'sensitiveValue',
104  $scope,
105  $scopeCode
106  );
107 
108  $inputMocks[] = $this->createInputMock(
109  'some/config/path_three',
110  'sensitiveValue',
111  $scope,
112  $scopeCode
113  );
114 
115  // attempt to overwrite existing value for path with null (should not be allowed)
116  $inputMocks[] = $this->createInputMock(
117  'some/config/path_three',
118  null,
119  $scope,
120  $scopeCode
121  );
122 
123  foreach ($inputMocks as $inputMock) {
125  $command = $this->objectManager->create(SensitiveConfigSetCommand::class);
126  $command->run($inputMock, $outputMock);
127  }
128 
129  $config = $this->loadEnvConfig();
130 
131  $assertCallback($config);
132  }
133 
134  public function executeDataProvider()
135  {
136  return [
137  [
139  null,
140  function (array $config) {
141  $this->assertTrue(isset($config['system']['default']['some']['config']['path_two']));
142  $this->assertTrue(isset($config['system']['default']['some']['config']['path_three']));
143  $this->assertEquals(
144  'sensitiveValue',
145  $config['system']['default']['some']['config']['path_two']
146  );
147  $this->assertEquals(
148  'sensitiveValue',
149  $config['system']['default']['some']['config']['path_three']
150  );
151  }
152  ],
153  [
154  'website',
155  'test',
156  function (array $config) {
157  $this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_two']));
158  $this->assertEquals(
159  'sensitiveValue',
160  $config['system']['website']['test']['some']['config']['path_two']
161  );
162  $this->assertEquals(
163  'sensitiveValue',
164  $config['system']['website']['test']['some']['config']['path_three']
165  );
166  }
167  ]
168  ];
169  }
170 
180  public function testExecuteInteractive($scope, $scopeCode, callable $assertCallback)
181  {
182  $inputMock = $this->createInputMock(null, null, $scope, $scopeCode);
183 
184  $outputMock = $this->createMock(OutputInterface::class);
185  $outputMock->expects($this->at(0))
186  ->method('writeln')
187  ->with('<info>Please set configuration values or skip them by pressing [Enter]:</info>');
188  $outputMock->expects($this->at(1))
189  ->method('writeln')
190  ->with('<info>Configuration values saved in app/etc/env.php</info>');
191 
192  $command = $this->createInteractiveCommand('sensitiveValue');
193  $command->run($inputMock, $outputMock);
194 
195  // attempt to overwrite existing value for path with null (should not be allowed)
196  $inputMock = $this->createInputMock(null, null, $scope, $scopeCode);
197  $command = $this->createInteractiveCommand(null);
198  $command->run($inputMock, $outputMock);
199 
200  $config = $this->loadEnvConfig();
201 
202  $assertCallback($config);
203  }
204 
205  public function executeInteractiveDataProvider()
206  {
207  return [
208  [
210  null,
211  function (array $config) {
212  $this->assertTrue(isset($config['system']['default']['some']['config']['path_one']));
213  $this->assertTrue(isset($config['system']['default']['some']['config']['path_two']));
214  $this->assertTrue(isset($config['system']['default']['some']['config']['path_three']));
215  $this->assertEquals(
216  'sensitiveValue',
217  $config['system']['default']['some']['config']['path_one']
218  );
219  $this->assertEquals(
220  'sensitiveValue',
221  $config['system']['default']['some']['config']['path_two']
222  );
223  $this->assertEquals(
224  'sensitiveValue',
225  $config['system']['default']['some']['config']['path_three']
226  );
227  }
228  ],
229  [
230  'website',
231  'test',
232  function (array $config) {
233  $this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_one']));
234  $this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_two']));
235  $this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_three']));
236  $this->assertEquals(
237  'sensitiveValue',
238  $config['system']['website']['test']['some']['config']['path_one']
239  );
240  $this->assertEquals(
241  'sensitiveValue',
242  $config['system']['website']['test']['some']['config']['path_two']
243  );
244  $this->assertEquals(
245  'sensitiveValue',
246  $config['system']['website']['test']['some']['config']['path_three']
247  );
248  }
249  ]
250  ];
251  }
252 
256  public function tearDown()
257  {
258  $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
259  $this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
260  "<?php\n return array();\n"
261  );
262  $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
263  $this->configFilePool->getPath(ConfigFilePool::APP_ENV),
264  "<?php\n return array();\n"
265  );
266 
268  $writer = $this->objectManager->get(Writer::class);
269  $writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig]);
270 
272  $writer = $this->objectManager->get(Writer::class);
273  $writer->saveConfig([ConfigFilePool::APP_CONFIG => $this->config]);
274  }
275 
279  private function loadEnvConfig()
280  {
281  return $this->reader->load(ConfigFilePool::APP_ENV);
282  }
283 
287  private function loadConfig()
288  {
289  return $this->reader->load(ConfigFilePool::APP_CONFIG);
290  }
291 
299  private function createInputMock($key, $val, $scope, $scopeCode)
300  {
301  $inputMock = $this->createMock(InputInterface::class);
302  $isInteractive = $key === null;
303 
304  if (!$isInteractive) {
305  $inputMock->expects($this->exactly(2))
306  ->method('getArgument')
307  ->withConsecutive(
310  )
311  ->willReturnOnConsecutiveCalls(
312  $key,
313  $val
314  );
315  }
316 
317  $inputMock->expects($this->exactly(3))
318  ->method('getOption')
319  ->withConsecutive(
323  )
324  ->willReturnOnConsecutiveCalls(
325  $scope,
326  $scopeCode,
327  $isInteractive
328  );
329 
330  return $inputMock;
331  }
332 
337  private function createInteractiveCommand($inputValue)
338  {
339  $questionHelperMock = $this->createMock(QuestionHelper::class);
340  $questionHelperMock->expects($this->exactly(3))
341  ->method('ask')
342  ->willReturn($inputValue);
343 
344  $interactiveCollectorMock = $this->objectManager->create(
345  InteractiveCollector::class,
346  [
347  'questionHelper' => $questionHelperMock
348  ]
349  );
350  $collectorFactoryMock = $this->getMockBuilder(CollectorFactory::class)
351  ->disableOriginalConstructor()
352  ->getMock();
353 
354  $collectorFactoryMock->expects($this->once())
355  ->method('create')
357  ->willReturn($interactiveCollectorMock);
358 
360  $command = $this->objectManager->create(
361  SensitiveConfigSetCommand::class,
362  [
363  'facade' => $this->objectManager->create(
364  SensitiveConfigSetFacade::class,
365  [
366  'collectorFactory' => $collectorFactoryMock
367  ]
368  )
369  ]
370  );
371 
372  return $command;
373  }
374 }
$objectManager
Definition: bootstrap.php:17
$config
Definition: fraud_order.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$filesystem