Skip to content

Commit 6eef1d0

Browse files
committed
Add --no-update option to sampledata:deploy and remove commands
Unit test has been refactored to be reused for new cases
1 parent b05a957 commit 6eef1d0

File tree

5 files changed

+347
-118
lines changed

5 files changed

+347
-118
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
use Symfony\Component\Console\Command\Command;
1313
use Symfony\Component\Console\Input\ArrayInput;
1414
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Input\InputOption;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617

1718
/**
1819
* Command for deployment of Sample Data
1920
*/
2021
class SampleDataDeployCommand extends Command
2122
{
23+
const OPTION_NO_UPDATE = 'no-update';
24+
2225
/**
2326
* @var \Magento\Framework\Filesystem
2427
*/
@@ -66,6 +69,12 @@ protected function configure()
6669
{
6770
$this->setName('sampledata:deploy')
6871
->setDescription('Deploy sample data modules');
72+
$this->addOption(
73+
self::OPTION_NO_UPDATE,
74+
null,
75+
InputOption::VALUE_NONE,
76+
'Update composer.json without executing composer update'
77+
);
6978
parent::configure();
7079
}
7180

@@ -80,6 +89,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
8089
if (!empty($sampleDataPackages)) {
8190
$baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
8291
$commonArgs = ['--working-dir' => $baseDir, '--no-progress' => 1];
92+
if ($input->getOption(self::OPTION_NO_UPDATE)) {
93+
$commonArgs['--no-update'] = 1;
94+
}
8395
$packages = [];
8496
foreach ($sampleDataPackages as $name => $version) {
8597
$packages[] = "$name:$version";

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Symfony\Component\Console\Command\Command;
1010
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Input\InputOption;
1112
use Symfony\Component\Console\Output\OutputInterface;
1213
use Magento\SampleData\Model\Dependency;
1314
use Symfony\Component\Console\Input\ArrayInput;
@@ -22,6 +23,8 @@
2223
*/
2324
class SampleDataRemoveCommand extends Command
2425
{
26+
const OPTION_NO_UPDATE = 'no-update';
27+
2528
/**
2629
* @var Filesystem
2730
*/
@@ -69,6 +72,12 @@ protected function configure()
6972
{
7073
$this->setName('sampledata:remove')
7174
->setDescription('Remove all sample data packages from composer.json');
75+
$this->addOption(
76+
self::OPTION_NO_UPDATE,
77+
null,
78+
InputOption::VALUE_NONE,
79+
'Update composer.json without executing composer update'
80+
);
7281
parent::configure();
7382
}
7483

@@ -81,6 +90,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
8190
if (!empty($sampleDataPackages)) {
8291
$baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
8392
$commonArgs = ['--working-dir' => $baseDir, '--no-interaction' => 1, '--no-progress' => 1];
93+
if ($input->getOption(self::OPTION_NO_UPDATE)) {
94+
$commonArgs['--no-update'] = 1;
95+
}
8496
$packages = array_keys($sampleDataPackages);
8597
$arguments = array_merge(['command' => 'remove', 'packages' => $packages], $commonArgs);
8698
$commandInput = new ArrayInput($arguments);
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\SampleData\Test\Unit\Console\Command;
7+
8+
use Composer\Console\Application;
9+
use Composer\Console\ApplicationFactory;
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Filesystem;
12+
use Magento\Framework\Filesystem\Directory\ReadInterface;
13+
use Magento\Framework\Filesystem\Directory\WriteInterface;
14+
use Magento\SampleData\Model\Dependency;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Console\Input\ArrayInput;
17+
use Symfony\Component\Console\Input\ArrayInputFactory;
18+
19+
/**
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
*/
22+
abstract class AbstractSampleDataCommandTest extends TestCase
23+
{
24+
/**
25+
* @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
protected $directoryReadMock;
28+
29+
/**
30+
* @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
protected $directoryWriteMock;
33+
34+
/**
35+
* @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
protected $filesystemMock;
38+
39+
/**
40+
* @var Dependency|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
protected $sampleDataDependencyMock;
43+
44+
/**
45+
* @var ArrayInputFactory|\PHPUnit_Framework_MockObject_MockObject
46+
*/
47+
protected $arrayInputFactoryMock;
48+
49+
/**
50+
* @var Application|\PHPUnit_Framework_MockObject_MockObject
51+
*/
52+
protected $applicationMock;
53+
54+
/**
55+
* @var ApplicationFactory|\PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
protected $applicationFactoryMock;
58+
59+
/**
60+
* @return void
61+
*/
62+
protected function setUp()
63+
{
64+
$this->directoryReadMock = $this->createMock(ReadInterface::class);
65+
$this->directoryWriteMock = $this->createMock(WriteInterface::class);
66+
$this->filesystemMock = $this->createMock(Filesystem::class);
67+
$this->sampleDataDependencyMock = $this->createMock(Dependency::class);
68+
$this->arrayInputFactoryMock = $this->createMock(ArrayInputFactory::class);
69+
$this->applicationMock = $this->createMock(Application::class);
70+
$this->applicationFactoryMock = $this->createPartialMock(ApplicationFactory::class, ['create']);
71+
}
72+
73+
/**
74+
* @param array $sampleDataPackages Array in form [package_name => version_constraint]
75+
* @param string $pathToComposerJson Fake path to composer.json
76+
* @param int $appRunResult Composer exit code
77+
* @param array $additionalComposerArgs Additional arguments that composer expects
78+
*/
79+
protected function setupMocks(
80+
$sampleDataPackages,
81+
$pathToComposerJson,
82+
$appRunResult,
83+
$additionalComposerArgs = []
84+
) {
85+
$this->directoryReadMock->expects($this->any())->method('getAbsolutePath')->willReturn($pathToComposerJson);
86+
$this->filesystemMock->expects($this->any())->method('getDirectoryRead')->with(DirectoryList::ROOT)->willReturn(
87+
$this->directoryReadMock
88+
);
89+
$this->sampleDataDependencyMock->expects($this->any())->method('getSampleDataPackages')->willReturn(
90+
$sampleDataPackages
91+
);
92+
$this->arrayInputFactoryMock->expects($this->never())->method('create');
93+
94+
$this->applicationMock->expects($this->any())
95+
->method('run')
96+
->with(
97+
new ArrayInput(
98+
array_merge(
99+
$this->expectedComposerArguments(
100+
$sampleDataPackages,
101+
$pathToComposerJson
102+
),
103+
$additionalComposerArgs
104+
)
105+
),
106+
$this->anything()
107+
)
108+
->willReturn($appRunResult);
109+
110+
if (($appRunResult !== 0) && !empty($sampleDataPackages)) {
111+
$this->applicationMock->expects($this->once())->method('resetComposer')->willReturnSelf();
112+
}
113+
114+
$this->applicationFactoryMock->expects($this->any())
115+
->method('create')
116+
->willReturn($this->applicationMock);
117+
}
118+
119+
/**
120+
* Expected arguments for composer based on sample data packages and composer.json path
121+
*
122+
* @param array $sampleDataPackages
123+
* @param string $pathToComposerJson
124+
* @return array
125+
*/
126+
abstract protected function expectedComposerArguments(
127+
array $sampleDataPackages,
128+
string $pathToComposerJson
129+
) : array;
130+
}

0 commit comments

Comments
 (0)