Skip to content

Commit f054b5c

Browse files
author
Bohdan Korablov
committed
MAGETWO-54716: Unable to setup Magento via web installer
1 parent 7bbb2bf commit f054b5c

File tree

2 files changed

+151
-21
lines changed

2 files changed

+151
-21
lines changed

app/code/Magento/SampleData/Console/Command/SampleDataDeployCommand.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Filesystem;
1818
use Composer\Console\Application;
1919
use Composer\Console\ApplicationFactory;
20+
use Magento\Setup\Model\PackagesAuth;
2021

2122
/**
2223
* Command for deployment of Sample Data
@@ -79,6 +80,7 @@ protected function configure()
7980
protected function execute(InputInterface $input, OutputInterface $output)
8081
{
8182
$this->updateMemoryLimit();
83+
$this->createAuthFile();
8284
$sampleDataPackages = $this->sampleDataDependency->getSampleDataPackages();
8385
if (!empty($sampleDataPackages)) {
8486
$baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
@@ -107,6 +109,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
107109
}
108110
}
109111

112+
/**
113+
* Create new auth.json file if it doesn't exist.
114+
*
115+
* @return void
116+
* @throws \Exception
117+
*/
118+
private function createAuthFile()
119+
{
120+
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::COMPOSER_HOME);
121+
122+
if (!$directory->isExist(PackagesAuth::PATH_TO_AUTH_FILE)) {
123+
try {
124+
$directory->writeFile(PackagesAuth::PATH_TO_AUTH_FILE, '{}');
125+
} catch (\Exception $e) {
126+
throw new \Exception('Error in writing Auth file. Please check permissions for writing.');
127+
}
128+
}
129+
}
130+
110131
/**
111132
* @return void
112133
*/

app/code/Magento/SampleData/Test/Unit/Console/Command/SampleDataDeployCommandTest.php

Lines changed: 130 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,103 @@
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\SampleData\Console\Command\SampleDataDeployCommand;
10+
use Magento\Setup\Model\PackagesAuth;
1011
use Symfony\Component\Console\Tester\CommandTester;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\Directory\ReadInterface;
14+
use Magento\Framework\Filesystem\Directory\WriteInterface;
15+
use Magento\SampleData\Model\Dependency;
16+
use Symfony\Component\Console\Input\ArrayInputFactory;
17+
use Composer\Console\ApplicationFactory;
18+
use Composer\Console\Application;
1119

1220
class SampleDataDeployCommandTest extends \PHPUnit_Framework_TestCase
1321
{
22+
/**
23+
* @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $directoryReadMock;
26+
27+
/**
28+
* @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $directoryWriteMock;
31+
32+
/**
33+
* @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
private $filesystemMock;
36+
37+
/**
38+
* @var Dependency|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $sampleDataDependencyMock;
41+
42+
/**
43+
* @var ArrayInputFactory|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $arrayInputFactoryMock;
46+
47+
/**
48+
* @var Application|\PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
private $applicationMock;
51+
52+
/**
53+
* @var ApplicationFactory|\PHPUnit_Framework_MockObject_MockObject
54+
*/
55+
private $applicationFactoryMock;
56+
57+
/**
58+
* @return void
59+
*/
60+
protected function setUp()
61+
{
62+
$this->directoryReadMock = $this->getMock(ReadInterface::class, [], [], '', false);
63+
$this->directoryWriteMock = $this->getMock(WriteInterface::class, [], [], '', false);
64+
$this->filesystemMock = $this->getMock(Filesystem::class, [], [], '', false);
65+
$this->sampleDataDependencyMock = $this->getMock(Dependency::class, [], [], '', false);
66+
$this->arrayInputFactoryMock = $this->getMock(ArrayInputFactory::class, [], [], '', false);
67+
$this->applicationMock = $this->getMock(Application::class, [], [], '', false);
68+
$this->applicationFactoryMock = $this->getMock(ApplicationFactory::class, ['create'], [], '', false);
69+
}
70+
1471
/**
1572
* @param array $sampleDataPackages
1673
* @param int $appRunResult - int 0 if everything went fine, or an error code
1774
* @param string $expectedMsg
75+
* @param bool $authExist
1876
* @return void
1977
*
2078
* @dataProvider processDataProvider
2179
*/
22-
public function testExecute(array $sampleDataPackages, $appRunResult, $expectedMsg)
80+
public function testExecute(array $sampleDataPackages, $appRunResult, $expectedMsg, $authExist)
2381
{
24-
$directoryRead = $this->getMock('\Magento\Framework\Filesystem\Directory\ReadInterface', [], [], '', false);
25-
$directoryRead->expects($this->any())->method('getAbsolutePath')->willReturn('/path/to/composer.json');
26-
27-
$filesystem = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
28-
$filesystem->expects($this->any())->method('getDirectoryRead')->with(DirectoryList::ROOT)
29-
->willReturn($directoryRead);
82+
$pathToComposerJson = '/path/to/composer.json';
3083

31-
$sampleDataDependency = $this->getMock('Magento\SampleData\Model\Dependency', [], [], '', false);
32-
$sampleDataDependency
33-
->expects($this->any())
84+
$this->directoryReadMock->expects($this->any())
85+
->method('getAbsolutePath')
86+
->willReturn($pathToComposerJson);
87+
$this->directoryWriteMock->expects($this->once())
88+
->method('isExist')
89+
->with(PackagesAuth::PATH_TO_AUTH_FILE)
90+
->willReturn($authExist);
91+
$this->directoryWriteMock->expects($authExist ? $this->never() : $this->once())
92+
->method('writeFile')
93+
->with(PackagesAuth::PATH_TO_AUTH_FILE, '{}');
94+
$this->filesystemMock->expects($this->any())
95+
->method('getDirectoryRead')
96+
->with(DirectoryList::ROOT)
97+
->willReturn($this->directoryReadMock);
98+
$this->filesystemMock->expects($this->once())
99+
->method('getDirectoryWrite')
100+
->with(DirectoryList::COMPOSER_HOME)
101+
->willReturn($this->directoryWriteMock);
102+
$this->sampleDataDependencyMock->expects($this->any())
34103
->method('getSampleDataPackages')
35104
->willReturn($sampleDataPackages);
36-
37-
$arrayInputFactory = $this
38-
->getMock('Symfony\Component\Console\Input\ArrayInputFactory', ['create'], [], '', false);
39-
$arrayInputFactory->expects($this->never())->method('create');
105+
$this->arrayInputFactoryMock->expects($this->never())
106+
->method('create');
40107

41108
array_walk($sampleDataPackages, function (&$v, $k) {
42109
$v = "$k:$v";
@@ -46,24 +113,32 @@ public function testExecute(array $sampleDataPackages, $appRunResult, $expectedM
46113

47114
$requireArgs = [
48115
'command' => 'require',
49-
'--working-dir' => '/path/to/composer.json',
116+
'--working-dir' => $pathToComposerJson,
50117
'--no-progress' => 1,
51118
'packages' => $packages,
52119
];
53120
$commandInput = new \Symfony\Component\Console\Input\ArrayInput($requireArgs);
54121

55-
$application = $this->getMock('Composer\Console\Application', [], [], '', false);
56-
$application->expects($this->any())->method('run')
122+
$this->applicationMock->expects($this->any())
123+
->method('run')
57124
->with($commandInput, $this->anything())
58125
->willReturn($appRunResult);
126+
59127
if (($appRunResult !== 0) && !empty($sampleDataPackages)) {
60-
$application->expects($this->once())->method('resetComposer')->willReturnSelf();
128+
$this->applicationMock->expects($this->once())->method('resetComposer')->willReturnSelf();
61129
}
62-
$applicationFactory = $this->getMock('Composer\Console\ApplicationFactory', ['create'], [], '', false);
63-
$applicationFactory->expects($this->any())->method('create')->willReturn($application);
130+
131+
$this->applicationFactoryMock->expects($this->any())
132+
->method('create')
133+
->willReturn($this->applicationMock);
64134

65135
$commandTester = new CommandTester(
66-
new SampleDataDeployCommand($filesystem, $sampleDataDependency, $arrayInputFactory, $applicationFactory)
136+
new SampleDataDeployCommand(
137+
$this->filesystemMock,
138+
$this->sampleDataDependencyMock,
139+
$this->arrayInputFactoryMock,
140+
$this->applicationFactoryMock
141+
)
67142
);
68143
$commandTester->execute([]);
69144

@@ -80,6 +155,7 @@ public function processDataProvider()
80155
'sampleDataPackages' => [],
81156
'appRunResult' => 1,
82157
'expectedMsg' => 'There is no sample data for current set of modules.' . PHP_EOL,
158+
'authExist' => true,
83159
],
84160
[
85161
'sampleDataPackages' => [
@@ -88,14 +164,47 @@ public function processDataProvider()
88164
'appRunResult' => 1,
89165
'expectedMsg' => 'There is an error during sample data deployment. Composer file will be reverted.'
90166
. PHP_EOL,
167+
'authExist' => false,
91168
],
92169
[
93170
'sampleDataPackages' => [
94171
'magento/module-cms-sample-data' => '1.0.0-beta',
95172
],
96173
'appRunResult' => 0,
97174
'expectedMsg' => '',
175+
'authExist' => true,
98176
],
99177
];
100178
}
179+
180+
/**
181+
* @expectedException \Exception
182+
* @expectedExceptionMessage Error in writing Auth file. Please check permissions for writing.
183+
* @return void
184+
*/
185+
public function testExecuteWithException()
186+
{
187+
$this->directoryWriteMock->expects($this->once())
188+
->method('isExist')
189+
->with(PackagesAuth::PATH_TO_AUTH_FILE)
190+
->willReturn(false);
191+
$this->directoryWriteMock->expects($this->once())
192+
->method('writeFile')
193+
->with(PackagesAuth::PATH_TO_AUTH_FILE, '{}')
194+
->willThrowException(new \Exception('Something went wrong...'));
195+
$this->filesystemMock->expects($this->once())
196+
->method('getDirectoryWrite')
197+
->with(DirectoryList::COMPOSER_HOME)
198+
->willReturn($this->directoryWriteMock);
199+
200+
$commandTester = new CommandTester(
201+
new SampleDataDeployCommand(
202+
$this->filesystemMock,
203+
$this->sampleDataDependencyMock,
204+
$this->arrayInputFactoryMock,
205+
$this->applicationFactoryMock
206+
)
207+
);
208+
$commandTester->execute([]);
209+
}
101210
}

0 commit comments

Comments
 (0)