123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\App\Test\Unit\DeploymentConfig;
- use Magento\Framework\App\DeploymentConfig;
- use Magento\Framework\App\DeploymentConfig\Reader;
- use Magento\Framework\App\DeploymentConfig\Writer;
- use Magento\Framework\App\DeploymentConfig\CommentParser;
- use Magento\Framework\App\DeploymentConfig\Writer\FormatterInterface;
- use Magento\Framework\App\Filesystem\DirectoryList;
- use Magento\Framework\Config\File\ConfigFilePool;
- use Magento\Framework\Exception\FileSystemException;
- use Magento\Framework\Filesystem;
- use Magento\Framework\Filesystem\Directory\ReadInterface;
- use Magento\Framework\Filesystem\Directory\WriteInterface;
- use Magento\Framework\Phrase;
- use \PHPUnit_Framework_MockObject_MockObject as Mock;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class WriterTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Writer
- */
- private $object;
- /**
- * @var DeploymentConfig\Reader|Mock
- */
- private $reader;
- /**
- * @var WriteInterface|Mock
- */
- private $dirWrite;
- /**
- * @var ReadInterface|Mock
- */
- private $dirRead;
- /**
- * @var FormatterInterface|Mock
- */
- protected $formatter;
- /**
- * @var ConfigFilePool|Mock
- */
- private $configFilePool;
- /**
- * @var DeploymentConfig|Mock
- */
- private $deploymentConfig;
- /**
- * @var Filesystem|Mock
- */
- private $filesystem;
- /**
- * @var CommentParser|Mock
- */
- private $commentParserMock;
- protected function setUp()
- {
- $this->commentParserMock = $this->getMockBuilder(CommentParser::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->reader = $this->getMockBuilder(Reader::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->filesystem = $this->getMockBuilder(Filesystem::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->formatter = $this->getMockForAbstractClass(FormatterInterface::class);
- $this->configFilePool = $this->getMockBuilder(ConfigFilePool::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->deploymentConfig = $this->getMockBuilder(DeploymentConfig::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->dirWrite = $this->getMockForAbstractClass(WriteInterface::class);
- $this->dirRead = $this->getMockForAbstractClass(ReadInterface::class);
- $this->object = new Writer(
- $this->reader,
- $this->filesystem,
- $this->configFilePool,
- $this->deploymentConfig,
- $this->formatter,
- $this->commentParserMock
- );
- }
- public function testSaveConfig()
- {
- $configFiles = [
- ConfigFilePool::APP_CONFIG => 'config.php'
- ];
- $testSetExisting = [
- ConfigFilePool::APP_CONFIG => [
- 'foo' => 'bar',
- 'key' => 'value',
- 'baz' => [
- 'test' => 'value',
- 'test1' => 'value1'
- ]
- ],
- ];
- $testSetUpdate = [
- ConfigFilePool::APP_CONFIG => [
- 'baz' => [
- 'test' => 'value2'
- ]
- ],
- ];
- $testSetExpected = [
- ConfigFilePool::APP_CONFIG => [
- 'foo' => 'bar',
- 'key' => 'value',
- 'baz' => [
- 'test' => 'value2',
- 'test1' => 'value1'
- ]
- ],
- ];
- $testComments = [
- 'baz' => 'Baz comment2',
- 'bar' => 'Bar comment'
- ];
- $existedComments = [
- 'foo' => 'Foo comment',
- 'baz' => 'Baz comment',
- ];
- $expectedComments = [
- 'foo' => 'Foo comment',
- 'baz' => 'Baz comment2',
- 'bar' => 'Bar comment'
- ];
- $this->deploymentConfig->expects($this->once())
- ->method('resetData');
- $this->configFilePool->expects($this->once())
- ->method('getPaths')
- ->willReturn($configFiles);
- $this->dirWrite->expects($this->any())
- ->method('isExist')
- ->willReturn(true);
- $this->reader->expects($this->once())
- ->method('load')
- ->willReturn($testSetExisting[ConfigFilePool::APP_CONFIG]);
- $this->commentParserMock->expects($this->once())
- ->method('execute')
- ->with('config.php')
- ->willReturn($existedComments);
- $this->formatter->expects($this->once())
- ->method('format')
- ->with($testSetExpected[ConfigFilePool::APP_CONFIG], $expectedComments)
- ->willReturn([]);
- $this->dirWrite->expects($this->once())
- ->method('writeFile')
- ->with('config.php', []);
- $this->reader->expects($this->any())
- ->method('getFiles')
- ->willReturn('test.php');
- $this->dirRead->expects($this->any())
- ->method('getAbsolutePath');
- $this->filesystem->expects($this->any())
- ->method('getDirectoryWrite')
- ->with(DirectoryList::CONFIG)
- ->willReturn($this->dirWrite);
- $this->filesystem->expects($this->any())
- ->method('getDirectoryRead')
- ->with(DirectoryList::CONFIG)
- ->willReturn($this->dirRead);
- $this->object->saveConfig($testSetUpdate, false, null, $testComments);
- }
- public function testSaveConfigOverride()
- {
- $configFiles = [
- ConfigFilePool::APP_CONFIG => 'config.php'
- ];
- $testSetUpdate = [
- ConfigFilePool::APP_CONFIG => [
- 'baz' => [
- 'test' => 'value2'
- ]
- ],
- ];
- $testSetExpected = [
- ConfigFilePool::APP_CONFIG => [
- 'baz' => [
- 'test' => 'value2',
- ]
- ],
- ];
- $this->deploymentConfig->expects($this->once())
- ->method('resetData');
- $this->configFilePool->expects($this->once())
- ->method('getPaths')
- ->willReturn($configFiles);
- $this->dirWrite->expects($this->any())
- ->method('isExist')
- ->willReturn(true);
- $this->commentParserMock->expects($this->once())
- ->method('execute')
- ->with('config.php')
- ->willReturn([]);
- $this->formatter->expects($this->once())
- ->method('format')
- ->with($testSetExpected[ConfigFilePool::APP_CONFIG])
- ->willReturn([]);
- $this->dirWrite->expects($this->once())
- ->method('writeFile')
- ->with('config.php', []);
- $this->reader->expects($this->any())
- ->method('getFiles')
- ->willReturn('test.php');
- $this->dirRead->expects($this->any())
- ->method('getAbsolutePath');
- $this->filesystem->expects($this->any())
- ->method('getDirectoryWrite')
- ->with(DirectoryList::CONFIG)
- ->willReturn($this->dirWrite);
- $this->filesystem->expects($this->any())
- ->method('getDirectoryRead')
- ->with(DirectoryList::CONFIG)
- ->willReturn($this->dirRead);
- $this->object->saveConfig($testSetUpdate, true);
- }
- /**
- * @expectedException \Magento\Framework\Exception\FileSystemException
- * @expectedExceptionMessage The "env.php" deployment config file isn't writable.
- */
- public function testSaveConfigException()
- {
- $exception = new FileSystemException(new Phrase('error when writing file config file'));
- $this->configFilePool->method('getPaths')
- ->willReturn([ConfigFilePool::APP_ENV => 'env.php']);
- $this->commentParserMock->expects($this->once())
- ->method('execute')
- ->with('env.php')
- ->willReturn([]);
- $this->dirWrite->method('writeFile')
- ->willThrowException($exception);
- $this->reader->expects($this->any())
- ->method('getFiles')
- ->willReturn('test.php');
- $this->dirRead->expects($this->any())
- ->method('getAbsolutePath');
- $this->filesystem->expects($this->any())
- ->method('getDirectoryWrite')
- ->with(DirectoryList::CONFIG)
- ->willReturn($this->dirWrite);
- $this->filesystem->expects($this->any())
- ->method('getDirectoryRead')
- ->with(DirectoryList::CONFIG)
- ->willReturn($this->dirRead);
- $this->dirWrite->expects($this->any())
- ->method('isExist')
- ->willReturn(true);
- $this->object->saveConfig([ConfigFilePool::APP_ENV => ['key' => 'value']]);
- }
- }
|