Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApplicationDumpCommandTest.php
Go to the documentation of this file.
1 <?php
7 
17 use Symfony\Component\Console\Input\InputInterface;
18 use Symfony\Component\Console\Output\OutputInterface;
19 
23 class ApplicationDumpCommandTest extends \PHPUnit\Framework\TestCase
24 {
28  private $objectManager;
29 
33  private $reader;
34 
38  private $configFilePool;
39 
43  private $filesystem;
44 
48  private $writer;
49 
53  private $envConfig;
54 
58  private $config;
59 
63  private $hash;
64 
68  public function setUp()
69  {
70  $this->objectManager = Bootstrap::getObjectManager();
71  $this->reader = $this->objectManager->get(DeploymentConfig\FileReader::class);
72  $this->filesystem = $this->objectManager->get(Filesystem::class);
73  $this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
74  $this->reader = $this->objectManager->get(DeploymentConfig\Reader::class);
75  $this->writer = $this->objectManager->get(DeploymentConfig\Writer::class);
76  $this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
77  $this->hash = $this->objectManager->get(Hash::class);
78 
79  // Snapshot of configuration.
80  $this->config = $this->loadConfig();
81  $this->envConfig = $this->loadEnvConfig();
82 
83  $this->writer->saveConfig(
84  [
86  'system' => [
87  'default' => [
88  'web' => [
89  'test' => [
90  'test_value_3' => 'value from the file'
91  ]
92  ]
93  ]
94  ]
95  ]
96  ],
97  true
98  );
99  }
100 
104  private function loadConfig()
105  {
106  return $this->reader->load(ConfigFilePool::APP_CONFIG);
107  }
108 
112  private function loadEnvConfig()
113  {
114  return $this->reader->load(ConfigFilePool::APP_ENV);
115  }
116 
120  private function loadRawConfig()
121  {
122  return $this->filesystem->getDirectoryRead(DirectoryList::CONFIG)
123  ->readFile($this->configFilePool->getPath(ConfigFilePool::APP_CONFIG));
124  }
125 
130  public function testExecute()
131  {
132  $this->objectManager->configure([
133  ExcludeList::class => [
134  'arguments' => [
135  'configs' => [
136  'web/test/test_value_1' => '',
137  'web/test/test_value_2' => '0',
138  'web/test/test_sensitive' => '1',
139  ],
140  ],
141  ],
142  TypePool::class => [
143  'arguments' => [
144  'sensitive' => [
145  'web/test/test_sensitive1' => '',
146  'web/test/test_sensitive2' => '0',
147  'web/test/test_sensitive3' => '1',
148  'web/test/test_sensitive_environment4' => '1',
149  'web/test/test_sensitive_environment5' => '1',
150  'web/test/test_sensitive_environment6' => '0',
151  ],
152  'environment' => [
153  'web/test/test_sensitive_environment4' => '1',
154  'web/test/test_sensitive_environment5' => '0',
155  'web/test/test_sensitive_environment6' => '1',
156  'web/test/test_environment7' => '',
157  'web/test/test_environment8' => '0',
158  'web/test/test_environment9' => '1',
159  ],
160  ]
161  ]
162  ]);
163 
164  $comment = implode(PHP_EOL, [
165  'Shared configuration was written to config.php and system-specific configuration to env.php.',
166  'Shared configuration file (config.php) doesn\'t contain sensitive data for security reasons.',
167  'Sensitive data can be stored in the following environment variables:',
168  'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE for web/test/test_sensitive',
169  'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE3 for web/test/test_sensitive3',
170  'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE_ENVIRONMENT4 for web/test/test_sensitive_environment4',
171  'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE_ENVIRONMENT5 for web/test/test_sensitive_environment5'
172  ]);
173  $outputMock = $this->createMock(OutputInterface::class);
174  $outputMock->expects($this->at(0))
175  ->method('writeln')
176  ->with(['system' => $comment]);
177  $outputMock->expects($this->at(1))
178  ->method('writeln')
179  ->with($this->matchesRegularExpression('/<info>Done. Config types dumped: [a-z0-9,\s]+<\/info>/'));
180 
182  $command = $this->objectManager->create(ApplicationDumpCommand::class);
183  $command->run($this->createMock(InputInterface::class), $outputMock);
184 
185  $config = $this->loadConfig();
186 
187  $this->validateSystemSection($config);
188  $this->validateThemesSection($config);
189 
190  $configEnv = $this->loadEnvConfig();
191  $this->validateSystemEnvSection($configEnv);
192 
193  $this->assertNotEmpty($this->hash->get());
194  $this->assertContains('For the section: system', $this->loadRawConfig());
195  }
196 
203  private function validateSystemSection(array $config)
204  {
205  $this->assertArrayHasKey('test_value_1', $config['system']['default']['web']['test']);
206  $this->assertArrayHasKey('test_value_2', $config['system']['default']['web']['test']);
207  $this->assertArrayHasKey('test_sensitive1', $config['system']['default']['web']['test']);
208  $this->assertArrayHasKey('test_sensitive2', $config['system']['default']['web']['test']);
209  $this->assertArrayHasKey('test_environment7', $config['system']['default']['web']['test']);
210  $this->assertArrayHasKey('test_environment8', $config['system']['default']['web']['test']);
211  $this->assertArrayNotHasKey('test_sensitive', $config['system']['default']['web']['test']);
212  $this->assertArrayNotHasKey('test_sensitive3', $config['system']['default']['web']['test']);
213  $this->assertArrayNotHasKey('test_sensitive_environment4', $config['system']['default']['web']['test']);
214  $this->assertArrayNotHasKey('test_sensitive_environment5', $config['system']['default']['web']['test']);
215  $this->assertArrayNotHasKey('test_sensitive_environment6', $config['system']['default']['web']['test']);
216  $this->assertArrayNotHasKey('test_environment9', $config['system']['default']['web']['test']);
218  $this->assertEquals(
219  'frontend/Magento/blank',
220  $config['system']['default']['design']['theme']['theme_id']
221  );
222  $this->assertEquals(
223  'frontend/Magento/luma',
224  $config['system']['stores']['default']['design']['theme']['theme_id']
225  );
226  $this->assertEquals(
227  'frontend/Magento/luma',
228  $config['system']['websites']['base']['design']['theme']['theme_id']
229  );
230 
231  $this->assertEquals('value from the file', $config['system']['default']['web']['test']['test_value_3']);
232  $this->assertEquals('GB', $config['system']['default']['general']['country']['default']);
233  $this->assertEquals(
234  'HK,IE,MO,PA,GB',
235  $config['system']['default']['general']['country']['optional_zip_countries']
236  );
237  }
238 
245  private function validateSystemEnvSection(array $config)
246  {
247  $envTestKeys = [
248  'test_sensitive',
249  'test_sensitive3',
250  'test_sensitive_environment4',
251  'test_sensitive_environment5',
252  'test_sensitive_environment6',
253  'test_environment9'
254  ];
255 
256  $this->assertEmpty(
257  array_diff($envTestKeys, array_keys($config['system']['default']['web']['test']))
258  );
259  }
260 
267  private function validateThemesSection(array $config)
268  {
269  $this->assertEquals(
270  [
271  'parent_id' => null,
272  'theme_path' => 'Magento/backend',
273  'theme_title' => 'Magento 2 backend',
274  'is_featured' => '0',
275  'area' => 'adminhtml',
276  'type' => '0',
277  'code' => 'Magento/backend',
278  ],
279  $config['themes']['adminhtml/Magento/backend']
280  );
281  $this->assertEquals(
282  [
283  'parent_id' => null,
284  'theme_path' => 'Magento/blank',
285  'theme_title' => 'Magento Blank',
286  'is_featured' => '0',
287  'area' => 'frontend',
288  'type' => '0',
289  'code' => 'Magento/blank',
290  ],
291  $config['themes']['frontend/Magento/blank']
292  );
293  $this->assertEquals(
294  [
295  'parent_id' => 'Magento/blank',
296  'theme_path' => 'Magento/luma',
297  'theme_title' => 'Magento Luma',
298  'is_featured' => '0',
299  'area' => 'frontend',
300  'type' => '0',
301  'code' => 'Magento/luma',
302  ],
303  $config['themes']['frontend/Magento/luma']
304  );
305  }
306 
310  public function tearDown()
311  {
312  $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
313  $this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
314  "<?php\n return array();\n"
315  );
316  $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
317  $this->configFilePool->getPath(ConfigFilePool::APP_ENV),
318  "<?php\n return array();\n"
319  );
320 
322  $writer = $this->objectManager->get(DeploymentConfig\Writer::class);
323  $writer->saveConfig([ConfigFilePool::APP_CONFIG => $this->config]);
324 
326  $writer = $this->objectManager->get(DeploymentConfig\Writer::class);
327  $writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig]);
328 
330  $deploymentConfig = $this->objectManager->get(DeploymentConfig::class);
331  $deploymentConfig->resetData();
332  }
333 }
$objectManager
Definition: bootstrap.php:17
$config
Definition: fraud_order.php:17
$deploymentConfig
$filesystem