Skip to content

Commit 5e6f153

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-54716' into FALCONS-BUGFIX
2 parents 0967a60 + 4790c40 commit 5e6f153

File tree

2 files changed

+175
-43
lines changed

2 files changed

+175
-43
lines changed

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,49 @@
99
use Symfony\Component\Console\Command\Command;
1010
use Symfony\Component\Console\Input\InputInterface;
1111
use Symfony\Component\Console\Output\OutputInterface;
12-
use Magento\SampleData\Model\Dependency;
1312
use Magento\Framework\App\State;
1413
use Symfony\Component\Console\Input\ArrayInput;
15-
use Symfony\Component\Console\Input\ArrayInputFactory;
1614
use Magento\Framework\App\Filesystem\DirectoryList;
17-
use Magento\Framework\Filesystem;
1815
use Composer\Console\Application;
19-
use Composer\Console\ApplicationFactory;
16+
use Magento\Setup\Model\PackagesAuth;
2017

2118
/**
2219
* Command for deployment of Sample Data
2320
*/
2421
class SampleDataDeployCommand extends Command
2522
{
2623
/**
27-
* @var Filesystem
24+
* @var \Magento\Framework\Filesystem
2825
*/
2926
private $filesystem;
3027

3128
/**
32-
* @var Dependency
29+
* @var \Magento\SampleData\Model\Dependency
3330
*/
3431
private $sampleDataDependency;
3532

3633
/**
37-
* @var ArrayInputFactory
34+
* @var \Symfony\Component\Console\Input\ArrayInputFactory
3835
* @deprecated
3936
*/
4037
private $arrayInputFactory;
4138

4239
/**
43-
* @var ApplicationFactory
40+
* @var \Composer\Console\ApplicationFactory
4441
*/
4542
private $applicationFactory;
4643

4744
/**
48-
* @param Filesystem $filesystem
49-
* @param Dependency $sampleDataDependency
50-
* @param ArrayInputFactory $arrayInputFactory
51-
* @param ApplicationFactory $applicationFactory
45+
* @param \Magento\Framework\Filesystem $filesystem
46+
* @param \Magento\SampleData\Model\Dependency $sampleDataDependency
47+
* @param \Symfony\Component\Console\Input\ArrayInputFactory $arrayInputFactory
48+
* @param \Composer\Console\ApplicationFactory $applicationFactory
5249
*/
5350
public function __construct(
54-
Filesystem $filesystem,
55-
Dependency $sampleDataDependency,
56-
ArrayInputFactory $arrayInputFactory,
57-
ApplicationFactory $applicationFactory
51+
\Magento\Framework\Filesystem $filesystem,
52+
\Magento\SampleData\Model\Dependency $sampleDataDependency,
53+
\Symfony\Component\Console\Input\ArrayInputFactory $arrayInputFactory,
54+
\Composer\Console\ApplicationFactory $applicationFactory
5855
) {
5956
$this->filesystem = $filesystem;
6057
$this->sampleDataDependency = $sampleDataDependency;
@@ -79,6 +76,7 @@ protected function configure()
7976
protected function execute(InputInterface $input, OutputInterface $output)
8077
{
8178
$this->updateMemoryLimit();
79+
$this->createAuthFile();
8280
$sampleDataPackages = $this->sampleDataDependency->getSampleDataPackages();
8381
if (!empty($sampleDataPackages)) {
8482
$baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
@@ -107,6 +105,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
107105
}
108106
}
109107

108+
/**
109+
* Create new auth.json file if it doesn't exist.
110+
*
111+
* We create auth.json with correct permissions instead of relying on Composer.
112+
*
113+
* @return void
114+
* @throws \Exception
115+
*/
116+
private function createAuthFile()
117+
{
118+
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::COMPOSER_HOME);
119+
120+
if (!$directory->isExist(PackagesAuth::PATH_TO_AUTH_FILE)) {
121+
try {
122+
$directory->writeFile(PackagesAuth::PATH_TO_AUTH_FILE, '{}');
123+
} catch (\Exception $e) {
124+
$message = 'Error in writing Auth file '
125+
. $directory->getAbsolutePath(PackagesAuth::PATH_TO_AUTH_FILE)
126+
. '. Please check permissions for writing.';
127+
throw new \Exception($message);
128+
}
129+
}
130+
}
131+
110132
/**
111133
* @return void
112134
*/

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

Lines changed: 137 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,106 @@
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

20+
/**
21+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22+
*/
1223
class SampleDataDeployCommandTest extends \PHPUnit_Framework_TestCase
1324
{
25+
/**
26+
* @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $directoryReadMock;
29+
30+
/**
31+
* @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $directoryWriteMock;
34+
35+
/**
36+
* @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $filesystemMock;
39+
40+
/**
41+
* @var Dependency|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $sampleDataDependencyMock;
44+
45+
/**
46+
* @var ArrayInputFactory|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
private $arrayInputFactoryMock;
49+
50+
/**
51+
* @var Application|\PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
private $applicationMock;
54+
55+
/**
56+
* @var ApplicationFactory|\PHPUnit_Framework_MockObject_MockObject
57+
*/
58+
private $applicationFactoryMock;
59+
60+
/**
61+
* @return void
62+
*/
63+
protected function setUp()
64+
{
65+
$this->directoryReadMock = $this->getMock(ReadInterface::class, [], [], '', false);
66+
$this->directoryWriteMock = $this->getMock(WriteInterface::class, [], [], '', false);
67+
$this->filesystemMock = $this->getMock(Filesystem::class, [], [], '', false);
68+
$this->sampleDataDependencyMock = $this->getMock(Dependency::class, [], [], '', false);
69+
$this->arrayInputFactoryMock = $this->getMock(ArrayInputFactory::class, [], [], '', false);
70+
$this->applicationMock = $this->getMock(Application::class, [], [], '', false);
71+
$this->applicationFactoryMock = $this->getMock(ApplicationFactory::class, ['create'], [], '', false);
72+
}
73+
1474
/**
1575
* @param array $sampleDataPackages
1676
* @param int $appRunResult - int 0 if everything went fine, or an error code
1777
* @param string $expectedMsg
78+
* @param bool $authExist
1879
* @return void
1980
*
2081
* @dataProvider processDataProvider
2182
*/
22-
public function testExecute(array $sampleDataPackages, $appRunResult, $expectedMsg)
83+
public function testExecute(array $sampleDataPackages, $appRunResult, $expectedMsg, $authExist)
2384
{
24-
$directoryRead = $this->getMock(
25-
\Magento\Framework\Filesystem\Directory\ReadInterface::class,
26-
[],
27-
[],
28-
'',
29-
false
30-
);
31-
$directoryRead->expects($this->any())->method('getAbsolutePath')->willReturn('/path/to/composer.json');
32-
33-
$filesystem = $this->getMock(\Magento\Framework\Filesystem::class, [], [], '', false);
34-
$filesystem->expects($this->any())->method('getDirectoryRead')->with(DirectoryList::ROOT)
35-
->willReturn($directoryRead);
85+
$pathToComposerJson = '/path/to/composer.json';
3686

37-
$sampleDataDependency = $this->getMock(\Magento\SampleData\Model\Dependency::class, [], [], '', false);
38-
$sampleDataDependency
39-
->expects($this->any())
87+
$this->directoryReadMock->expects($this->any())
88+
->method('getAbsolutePath')
89+
->willReturn($pathToComposerJson);
90+
$this->directoryWriteMock->expects($this->once())
91+
->method('isExist')
92+
->with(PackagesAuth::PATH_TO_AUTH_FILE)
93+
->willReturn($authExist);
94+
$this->directoryWriteMock->expects($authExist ? $this->never() : $this->once())
95+
->method('writeFile')
96+
->with(PackagesAuth::PATH_TO_AUTH_FILE, '{}');
97+
$this->filesystemMock->expects($this->any())
98+
->method('getDirectoryRead')
99+
->with(DirectoryList::ROOT)
100+
->willReturn($this->directoryReadMock);
101+
$this->filesystemMock->expects($this->once())
102+
->method('getDirectoryWrite')
103+
->with(DirectoryList::COMPOSER_HOME)
104+
->willReturn($this->directoryWriteMock);
105+
$this->sampleDataDependencyMock->expects($this->any())
40106
->method('getSampleDataPackages')
41107
->willReturn($sampleDataPackages);
42-
43-
$arrayInputFactory = $this
44-
->getMock(\Symfony\Component\Console\Input\ArrayInputFactory::class, ['create'], [], '', false);
45-
$arrayInputFactory->expects($this->never())->method('create');
108+
$this->arrayInputFactoryMock->expects($this->never())
109+
->method('create');
46110

47111
array_walk($sampleDataPackages, function (&$v, $k) {
48112
$v = "$k:$v";
@@ -52,24 +116,32 @@ public function testExecute(array $sampleDataPackages, $appRunResult, $expectedM
52116

53117
$requireArgs = [
54118
'command' => 'require',
55-
'--working-dir' => '/path/to/composer.json',
119+
'--working-dir' => $pathToComposerJson,
56120
'--no-progress' => 1,
57121
'packages' => $packages,
58122
];
59123
$commandInput = new \Symfony\Component\Console\Input\ArrayInput($requireArgs);
60124

61-
$application = $this->getMock(\Composer\Console\Application::class, [], [], '', false);
62-
$application->expects($this->any())->method('run')
125+
$this->applicationMock->expects($this->any())
126+
->method('run')
63127
->with($commandInput, $this->anything())
64128
->willReturn($appRunResult);
129+
65130
if (($appRunResult !== 0) && !empty($sampleDataPackages)) {
66-
$application->expects($this->once())->method('resetComposer')->willReturnSelf();
131+
$this->applicationMock->expects($this->once())->method('resetComposer')->willReturnSelf();
67132
}
68-
$applicationFactory = $this->getMock(\Composer\Console\ApplicationFactory::class, ['create'], [], '', false);
69-
$applicationFactory->expects($this->any())->method('create')->willReturn($application);
133+
134+
$this->applicationFactoryMock->expects($this->any())
135+
->method('create')
136+
->willReturn($this->applicationMock);
70137

71138
$commandTester = new CommandTester(
72-
new SampleDataDeployCommand($filesystem, $sampleDataDependency, $arrayInputFactory, $applicationFactory)
139+
new SampleDataDeployCommand(
140+
$this->filesystemMock,
141+
$this->sampleDataDependencyMock,
142+
$this->arrayInputFactoryMock,
143+
$this->applicationFactoryMock
144+
)
73145
);
74146
$commandTester->execute([]);
75147

@@ -86,6 +158,7 @@ public function processDataProvider()
86158
'sampleDataPackages' => [],
87159
'appRunResult' => 1,
88160
'expectedMsg' => 'There is no sample data for current set of modules.' . PHP_EOL,
161+
'authExist' => true,
89162
],
90163
[
91164
'sampleDataPackages' => [
@@ -94,14 +167,51 @@ public function processDataProvider()
94167
'appRunResult' => 1,
95168
'expectedMsg' => 'There is an error during sample data deployment. Composer file will be reverted.'
96169
. PHP_EOL,
170+
'authExist' => false,
97171
],
98172
[
99173
'sampleDataPackages' => [
100174
'magento/module-cms-sample-data' => '1.0.0-beta',
101175
],
102176
'appRunResult' => 0,
103177
'expectedMsg' => '',
178+
'authExist' => true,
104179
],
105180
];
106181
}
182+
183+
/**
184+
* @expectedException \Exception
185+
* @expectedExceptionMessage Error in writing Auth file path/to/auth.json. Please check permissions for writing.
186+
* @return void
187+
*/
188+
public function testExecuteWithException()
189+
{
190+
$this->directoryWriteMock->expects($this->once())
191+
->method('isExist')
192+
->with(PackagesAuth::PATH_TO_AUTH_FILE)
193+
->willReturn(false);
194+
$this->directoryWriteMock->expects($this->once())
195+
->method('writeFile')
196+
->with(PackagesAuth::PATH_TO_AUTH_FILE, '{}')
197+
->willThrowException(new \Exception('Something went wrong...'));
198+
$this->directoryWriteMock->expects($this->once())
199+
->method('getAbsolutePath')
200+
->with(PackagesAuth::PATH_TO_AUTH_FILE)
201+
->willReturn('path/to/auth.json');
202+
$this->filesystemMock->expects($this->once())
203+
->method('getDirectoryWrite')
204+
->with(DirectoryList::COMPOSER_HOME)
205+
->willReturn($this->directoryWriteMock);
206+
207+
$commandTester = new CommandTester(
208+
new SampleDataDeployCommand(
209+
$this->filesystemMock,
210+
$this->sampleDataDependencyMock,
211+
$this->arrayInputFactoryMock,
212+
$this->applicationFactoryMock
213+
)
214+
);
215+
$commandTester->execute([]);
216+
}
107217
}

0 commit comments

Comments
 (0)