diff --git a/lib/internal/Magento/Framework/App/DeploymentConfig/Reader.php b/lib/internal/Magento/Framework/App/DeploymentConfig/Reader.php index 06a66a2b3f873..ff7077213c5c3 100644 --- a/lib/internal/Magento/Framework/App/DeploymentConfig/Reader.php +++ b/lib/internal/Magento/Framework/App/DeploymentConfig/Reader.php @@ -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. @@ -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 */ @@ -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(); @@ -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); } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/ReaderTest.php b/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/ReaderTest.php index 3e3eea322cafd..8f8399263384c 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/ReaderTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/ReaderTest.php @@ -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; @@ -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') @@ -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') @@ -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 */ diff --git a/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/_files/emptyConfig.php b/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/_files/emptyConfig.php new file mode 100644 index 0000000000000..79549bf674aad --- /dev/null +++ b/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/_files/emptyConfig.php @@ -0,0 +1,6 @@ +