|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Deploy\Test\Unit\Model\DeploymentConfig; |
| 7 | + |
| 8 | +use Magento\Framework\App\DeploymentConfig\ImporterInterface; |
| 9 | +use Magento\Framework\App\DeploymentConfig; |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Psr\Log\LoggerInterface as Logger; |
| 12 | +use Magento\Deploy\Console\Command\App\ConfigImport\Importer; |
| 13 | +use Magento\Deploy\Model\DeploymentConfig\Validator; |
| 14 | +use Magento\Deploy\Model\DeploymentConfig\Hash; |
| 15 | +use Magento\Deploy\Model\DeploymentConfig\ImporterPool; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | + |
| 18 | +class ImporterTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Validator|\PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + private $configValidatorMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var ImporterPool|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $configImporterPoolMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $deploymentConfigMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Hash|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $configHashMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Logger|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + private $loggerMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject |
| 47 | + */ |
| 48 | + private $outputMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var Importer |
| 52 | + */ |
| 53 | + private $importer; |
| 54 | + |
| 55 | + /** |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + protected function setUp() |
| 59 | + { |
| 60 | + $this->configValidatorMock = $this->getMockBuilder(Validator::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->getMock(); |
| 63 | + $this->configImporterPoolMock = $this->getMockBuilder(ImporterPool::class) |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->getMock(); |
| 66 | + $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class) |
| 67 | + ->disableOriginalConstructor() |
| 68 | + ->getMock(); |
| 69 | + $this->configHashMock = $this->getMockBuilder(Hash::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->getMock(); |
| 72 | + $this->loggerMock = $this->getMockBuilder(Logger::class) |
| 73 | + ->disableOriginalConstructor() |
| 74 | + ->getMock(); |
| 75 | + $this->outputMock = $this->getMockBuilder(OutputInterface::class) |
| 76 | + ->getMockForAbstractClass(); |
| 77 | + |
| 78 | + $this->importer = new Importer( |
| 79 | + $this->configValidatorMock, |
| 80 | + $this->configImporterPoolMock, |
| 81 | + $this->deploymentConfigMock, |
| 82 | + $this->configHashMock, |
| 83 | + $this->loggerMock |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + public function testImport() |
| 91 | + { |
| 92 | + $configData = ['some data']; |
| 93 | + $messages = ['Import has done']; |
| 94 | + $expectsMessages = ['Import has done']; |
| 95 | + $importerMock = $this->getMockBuilder(ImporterInterface::class) |
| 96 | + ->getMockForAbstractClass(); |
| 97 | + $importers = ['someSection' => $importerMock]; |
| 98 | + |
| 99 | + $this->configImporterPoolMock->expects($this->once()) |
| 100 | + ->method('getImporters') |
| 101 | + ->willReturn($importers); |
| 102 | + $this->configValidatorMock->expects($this->any()) |
| 103 | + ->method('isValid') |
| 104 | + ->willReturn(false); |
| 105 | + $this->deploymentConfigMock->expects($this->once()) |
| 106 | + ->method('getConfigData') |
| 107 | + ->with('someSection') |
| 108 | + ->willReturn($configData); |
| 109 | + $importerMock->expects($this->once()) |
| 110 | + ->method('import') |
| 111 | + ->with($configData) |
| 112 | + ->willReturn($messages); |
| 113 | + $this->configHashMock->expects($this->once()) |
| 114 | + ->method('regenerate'); |
| 115 | + $this->loggerMock->expects($this->never()) |
| 116 | + ->method('error'); |
| 117 | + |
| 118 | + $this->outputMock->expects($this->at(0)) |
| 119 | + ->method('writeln') |
| 120 | + ->with('<info>Start import:</info>'); |
| 121 | + $this->outputMock->expects($this->at(1)) |
| 122 | + ->method('writeln') |
| 123 | + ->with($expectsMessages); |
| 124 | + |
| 125 | + $this->importer->import($this->outputMock); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @return void |
| 130 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 131 | + * @expectedExceptionMessage Import is failed |
| 132 | + */ |
| 133 | + public function testImportWithException() |
| 134 | + { |
| 135 | + $exception = new LocalizedException(__('Some error')); |
| 136 | + $this->outputMock->expects($this->at(0)) |
| 137 | + ->method('writeln') |
| 138 | + ->with('<info>Start import:</info>'); |
| 139 | + $this->configImporterPoolMock->expects($this->once()) |
| 140 | + ->method('getImporters') |
| 141 | + ->willThrowException($exception); |
| 142 | + $this->loggerMock->expects($this->once()) |
| 143 | + ->method('error') |
| 144 | + ->with($exception); |
| 145 | + |
| 146 | + $this->importer->import($this->outputMock); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @param array $importers |
| 151 | + * @param bool $isValid |
| 152 | + * @return void |
| 153 | + * @dataProvider importNothingToImportDataProvider |
| 154 | + */ |
| 155 | + public function testImportNothingToImport(array $importers, $isValid) |
| 156 | + { |
| 157 | + $this->configImporterPoolMock->expects($this->once()) |
| 158 | + ->method('getImporters') |
| 159 | + ->willReturn($importers); |
| 160 | + $this->configValidatorMock->expects($this->any()) |
| 161 | + ->method('isValid') |
| 162 | + ->willReturn($isValid); |
| 163 | + $this->deploymentConfigMock->expects($this->never()) |
| 164 | + ->method('getConfigData'); |
| 165 | + $this->configHashMock->expects($this->never()) |
| 166 | + ->method('regenerate'); |
| 167 | + $this->loggerMock->expects($this->never()) |
| 168 | + ->method('error'); |
| 169 | + |
| 170 | + $this->outputMock->expects($this->at(0)) |
| 171 | + ->method('writeln') |
| 172 | + ->with('<info>Start import:</info>'); |
| 173 | + $this->outputMock->expects($this->at(1)) |
| 174 | + ->method('writeln') |
| 175 | + ->with('<info>Nothing to import</info>'); |
| 176 | + |
| 177 | + $this->importer->import($this->outputMock); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @return array |
| 182 | + */ |
| 183 | + public function importNothingToImportDataProvider() |
| 184 | + { |
| 185 | + return [ |
| 186 | + ['importers' => [], 'isValid' => true], |
| 187 | + ['importers' => [], 'isValid' => false], |
| 188 | + ['importers' => ['someImporter'], 'isValid' => true], |
| 189 | + ]; |
| 190 | + } |
| 191 | +} |
0 commit comments