Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/internal/Magento/Framework/App/DeploymentConfig/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\RuntimeException;
use Magento\Framework\Filesystem\DriverPool;
use Magento\Framework\Phrase;

/**
* Deployment configuration reader.
Expand Down Expand Up @@ -87,6 +89,7 @@ public function getFiles()
* @param string $fileKey The file key (deprecated)
* @return array
* @throws FileSystemException If file can not be read
* @throws RuntimeException If file is invalid
* @throws \Exception If file key is not correct
* @see FileReader
*/
Expand All @@ -99,6 +102,9 @@ public function load($fileKey = null)
$filePath = $path . '/' . $this->configFilePool->getPath($fileKey);
if ($fileDriver->isExists($filePath)) {
$result = include $filePath;
if (!is_array($result)) {
throw new RuntimeException(new Phrase("Invalid configuration file: '%1'", [$filePath]));
}
}
} else {
$configFiles = $this->configFilePool->getPaths();
Expand All @@ -108,11 +114,14 @@ public function load($fileKey = null)
$configFile = $path . '/' . $this->configFilePool->getPath($fileKey);
if ($fileDriver->isExists($configFile)) {
$fileData = include $configFile;
if (!is_array($fileData)) {
throw new RuntimeException(new Phrase("Invalid configuration file: '%1'", [$configFile]));
}
} else {
continue;
}
$allFilesData[$configFile] = $fileData;
if (is_array($fileData) && count($fileData) > 0) {
if ($fileData) {
$result = array_replace_recursive($result, $fileData);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@

use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Filesystem\DriverPool;

class ReaderTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Framework\App\Filesystem\DirectoryList|\PHPUnit_Framework_MockObject_MockObject
*/
private $dirList;

/**
* @var \Magento\Framework\Filesystem\DriverPool|\PHPUnit_Framework_MockObject_MockObject
* @var DriverPool|\PHPUnit_Framework_MockObject_MockObject
*/
private $driverPool;

/**
* @var \Magento\Framework\Filesystem\Driver\File|\PHPUnit_Framework_MockObject_MockObject
* @var File|\PHPUnit_Framework_MockObject_MockObject
*/
private $fileDriver;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var ConfigFilePool|\PHPUnit_Framework_MockObject_MockObject
*/
private $configFilePool;

Expand All @@ -38,7 +41,7 @@ protected function setUp()
->method('getPath')
->with(DirectoryList::CONFIG)
->willReturn(__DIR__ . '/_files');
$this->fileDriver = $this->createMock(\Magento\Framework\Filesystem\Driver\File::class);
$this->fileDriver = $this->createMock(File::class);
$this->fileDriver
->expects($this->any())
->method('isExists')
Expand All @@ -51,12 +54,12 @@ protected function setUp()
[__DIR__ . '/_files/mergeTwo.php', true],
[__DIR__ . '/_files/nonexistent.php', false]
]));
$this->driverPool = $this->createMock(\Magento\Framework\Filesystem\DriverPool::class);
$this->driverPool = $this->createMock(DriverPool::class);
$this->driverPool
->expects($this->any())
->method('getDriver')
->willReturn($this->fileDriver);
$this->configFilePool = $this->createMock(\Magento\Framework\Config\File\ConfigFilePool::class);
$this->configFilePool = $this->createMock(ConfigFilePool::class);
$this->configFilePool
->expects($this->any())
->method('getPaths')
Expand Down Expand Up @@ -100,13 +103,97 @@ public function testLoad()
*/
public function testCustomLoad($file, $expected)
{
$configFilePool = $this->createMock(\Magento\Framework\Config\File\ConfigFilePool::class);
$configFilePool = $this->createMock(ConfigFilePool::class);
$configFilePool->expects($this->any())->method('getPaths')->willReturn([$file]);
$configFilePool->expects($this->any())->method('getPath')->willReturn($file);
$object = new Reader($this->dirList, $this->driverPool, $configFilePool, $file);
$this->assertSame($expected, $object->load($file));
}

/**
* Test Reader::load() will throw exception in case of invalid configuration file(single file).
*
* @expectedException \Magento\Framework\Exception\RuntimeException
* @expectedExceptionMessageRegExp /Invalid configuration file: \'.*\/\_files\/emptyConfig\.php\'/
* @return void
*/
public function testLoadInvalidConfigurationFileWithFileKey()
{
$fileDriver = $this->getMockBuilder(File::class)
->disableOriginalConstructor()
->getMock();
$fileDriver->expects($this->once())
->method('isExists')
->willReturn(true);
/** @var DriverPool|\PHPUnit_Framework_MockObject_MockObject $driverPool */
$driverPool = $this->getMockBuilder(DriverPool::class)
->disableOriginalConstructor()
->getMock();
$driverPool
->expects($this->once())
->method('getDriver')
->willReturn($fileDriver);
/** @var ConfigFilePool|\PHPUnit_Framework_MockObject_MockObject $configFilePool */
$configFilePool = $this->getMockBuilder(ConfigFilePool::class)
->disableOriginalConstructor()
->getMock();
$configFilePool
->expects($this->once())
->method('getPath')
->with($this->identicalTo('testConfig'))
->willReturn('emptyConfig.php');
$object = new Reader($this->dirList, $driverPool, $configFilePool);
$object->load('testConfig');
}

/**
* Test Reader::load() will throw exception in case of invalid configuration file(multiple files).
*
* @expectedException \Magento\Framework\Exception\RuntimeException
* @expectedExceptionMessageRegExp /Invalid configuration file: \'.*\/\_files\/emptyConfig\.php\'/
* @return void
*/
public function testLoadInvalidConfigurationFile()
{
$fileDriver = $this->getMockBuilder(File::class)
->disableOriginalConstructor()
->getMock();
$fileDriver->expects($this->exactly(2))
->method('isExists')
->willReturn(true);
/** @var DriverPool|\PHPUnit_Framework_MockObject_MockObject $driverPool */
$driverPool = $this->getMockBuilder(DriverPool::class)
->disableOriginalConstructor()
->getMock();
$driverPool
->expects($this->once())
->method('getDriver')
->willReturn($fileDriver);
/** @var ConfigFilePool|\PHPUnit_Framework_MockObject_MockObject $configFilePool */
$configFilePool = $this->getMockBuilder(ConfigFilePool::class)
->disableOriginalConstructor()
->getMock();
$configFilePool->expects($this->exactly(2))
->method('getPaths')
->willReturn(
[
'configKeyOne' => 'config.php',
'testConfig' => 'emptyConfig.php'
]
);
$configFilePool->expects($this->exactly(2))
->method('getPath')
->withConsecutive(
[$this->identicalTo('configKeyOne')],
[$this->identicalTo('testConfig')]
)->willReturnOnConsecutiveCalls(
'config.php',
'emptyConfig.php'
);
$object = new Reader($this->dirList, $driverPool, $configFilePool);
$object->load();
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
//Example of wrong(empty) configuration file.