Skip to content

Commit 9ce8f83

Browse files
committed
Merge branch 'sample-data-composer.json-version' of github.com:fredden/magento2 into 2.4-develop-prs
2 parents 2211d36 + 1e46a0b commit 9ce8f83

File tree

4 files changed

+46
-195
lines changed

4 files changed

+46
-195
lines changed

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

Lines changed: 23 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Magento\SampleData\Console\Command;
88

9-
use Composer\Console\Application;
109
use Composer\Console\ApplicationFactory;
1110
use Exception;
1211
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -15,12 +14,10 @@
1514
use Magento\Framework\Exception\InvalidArgumentException;
1615
use Magento\Framework\Exception\LocalizedException;
1716
use Magento\Framework\Filesystem;
18-
use Magento\Framework\Serialize\Serializer\Json;
1917
use Magento\SampleData\Model\Dependency;
2018
use Magento\Setup\Model\PackagesAuth;
2119
use Symfony\Component\Console\Command\Command;
2220
use Symfony\Component\Console\Input\ArrayInput;
23-
use Symfony\Component\Console\Input\ArrayInputFactory;
2421
use Symfony\Component\Console\Input\InputInterface;
2522
use Symfony\Component\Console\Input\InputOption;
2623
use Symfony\Component\Console\Output\OutputInterface;
@@ -32,53 +29,31 @@
3229
*/
3330
class SampleDataDeployCommand extends Command
3431
{
35-
const OPTION_NO_UPDATE = 'no-update';
32+
public const OPTION_NO_UPDATE = 'no-update';
3633

37-
/**
38-
* @var Filesystem
39-
*/
40-
private $filesystem;
34+
/** @var Filesystem */
35+
private Filesystem $filesystem;
4136

42-
/**
43-
* @var Dependency
44-
*/
45-
private $sampleDataDependency;
37+
/** @var Dependency */
38+
private Dependency $sampleDataDependency;
4639

47-
/**
48-
* @var ArrayInputFactory
49-
* @deprecated 100.1.0
50-
*/
51-
private $arrayInputFactory;
52-
53-
/**
54-
* @var ApplicationFactory
55-
*/
56-
private $applicationFactory;
57-
58-
/**
59-
* @var Json
60-
*/
61-
private $serializer;
40+
/** @var ApplicationFactory */
41+
private ApplicationFactory $applicationFactory;
6242

6343
/**
6444
* @param Filesystem $filesystem
6545
* @param Dependency $sampleDataDependency
66-
* @param ArrayInputFactory $arrayInputFactory
6746
* @param ApplicationFactory $applicationFactory
68-
* @param Json $serializer
6947
*/
7048
public function __construct(
7149
Filesystem $filesystem,
7250
Dependency $sampleDataDependency,
73-
ArrayInputFactory $arrayInputFactory,
74-
ApplicationFactory $applicationFactory,
75-
Json $serializer
51+
ApplicationFactory $applicationFactory
7652
) {
7753
$this->filesystem = $filesystem;
7854
$this->sampleDataDependency = $sampleDataDependency;
79-
$this->arrayInputFactory = $arrayInputFactory;
8055
$this->applicationFactory = $applicationFactory;
81-
$this->serializer = $serializer;
56+
8257
parent::__construct();
8358
}
8459

@@ -107,35 +82,8 @@ protected function configure()
10782
* @throws FileSystemException
10883
* @throws LocalizedException
10984
*/
110-
protected function execute(InputInterface $input, OutputInterface $output)
85+
protected function execute(InputInterface $input, OutputInterface $output): int
11186
{
112-
$rootJson = $this->serializer->unserialize(
113-
$this->filesystem->getDirectoryRead(
114-
DirectoryList::ROOT
115-
)->readFile("composer.json")
116-
);
117-
if (!isset($rootJson['version'])) {
118-
$magentoProductPackage = array_filter(
119-
$rootJson['require'],
120-
function ($package) {
121-
return false !== strpos($package, 'magento/product-');
122-
},
123-
ARRAY_FILTER_USE_KEY
124-
);
125-
$version = reset($magentoProductPackage);
126-
$output->writeln(
127-
'<info>' .
128-
// @codingStandardsIgnoreLine
129-
'We don\'t recommend to remove the "version" field from your composer.json; see https://getcomposer.org/doc/02-libraries.md#library-versioning for more information.' .
130-
'</info>'
131-
);
132-
$restoreVersion = new ArrayInput([
133-
'command' => 'config',
134-
'setting-key' => 'version',
135-
'setting-value' => [$version],
136-
'--quiet' => 1
137-
]);
138-
}
13987
$this->updateMemoryLimit();
14088
$this->createAuthFile();
14189
$sampleDataPackages = $this->sampleDataDependency->getSampleDataPackages();
@@ -153,15 +101,8 @@ function ($package) {
153101
$arguments = array_merge(['command' => 'require'], $commonArgs);
154102
$commandInput = new ArrayInput($arguments);
155103

156-
/** @var Application $application */
157104
$application = $this->applicationFactory->create();
158105
$application->setAutoExit(false);
159-
if (!empty($restoreVersion)) {
160-
$result = $application->run($restoreVersion, clone $output);
161-
if ($result === 0) {
162-
$output->writeln('<info>The field "version" has been restored.</info>');
163-
}
164-
}
165106
$result = $application->run($commandInput, $output);
166107
if ($result !== 0) {
167108
$output->writeln(
@@ -173,12 +114,18 @@ function ($package) {
173114
return Cli::RETURN_FAILURE;
174115
}
175116

176-
return Cli::RETURN_SUCCESS;
177-
} else {
178-
$output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
117+
$output->writeln(
118+
'<info>'
119+
. 'Sample data modules have been added via composer. Please run bin/magento setup:upgrade'
120+
. '</info>'
121+
);
179122

180-
return Cli::RETURN_FAILURE;
123+
return Cli::RETURN_SUCCESS;
181124
}
125+
126+
$output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
127+
128+
return Cli::RETURN_FAILURE;
182129
}
183130

184131
/**
@@ -189,7 +136,7 @@ function ($package) {
189136
* @return void
190137
* @throws LocalizedException
191138
*/
192-
private function createAuthFile()
139+
private function createAuthFile(): void
193140
{
194141
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::COMPOSER_HOME);
195142

@@ -211,7 +158,7 @@ private function createAuthFile()
211158
* @throws InvalidArgumentException
212159
* @return void
213160
*/
214-
private function updateMemoryLimit()
161+
private function updateMemoryLimit(): void
215162
{
216163
if (function_exists('ini_set')) {
217164
// phpcs:ignore Magento2.Functions.DiscouragedFunction
@@ -244,7 +191,7 @@ private function updateMemoryLimit()
244191
* @param string $value
245192
* @return int
246193
*/
247-
private function getMemoryInBytes($value)
194+
private function getMemoryInBytes(string $value): int
248195
{
249196
$unit = strtolower(substr($value, -1, 1));
250197
$value = (int) $value;

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

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@
2626
*/
2727
abstract class AbstractSampleDataCommandTest extends TestCase
2828
{
29-
/*
30-
* Expected arguments for `composer config` to set missing field "version"
31-
*/
32-
private const STUB_EXPECTED_COMPOSER_CONFIG = [
33-
'command' => 'config',
34-
'setting-key' => 'version',
35-
'setting-value' => ['0.0.1'],
36-
'--quiet' => 1
37-
];
38-
3929
/**
4030
* @var ReadInterface|MockObject
4131
*/
@@ -118,49 +108,21 @@ protected function setupMocks(
118108
->willReturn($sampleDataPackages);
119109
$this->arrayInputFactoryMock->expects($this->never())->method('create');
120110

121-
if (!array_key_exists('version', $composerJsonContent)) {
122-
$this->applicationMock->expects($this->any())
123-
->method('run')
124-
->withConsecutive(
125-
[
126-
'input' => new ArrayInput(
127-
self::STUB_EXPECTED_COMPOSER_CONFIG
111+
$this->applicationMock->expects($this->any())
112+
->method('run')
113+
->with(
114+
new ArrayInput(
115+
array_merge(
116+
$this->expectedComposerArgumentsSampleDataCommands(
117+
$sampleDataPackages,
118+
$pathToComposerJson
128119
),
129-
'output' => $this->anything()
130-
],
131-
[
132-
'input' => new ArrayInput(
133-
array_merge(
134-
$this->expectedComposerArgumentsSampleDataCommands(
135-
$sampleDataPackages,
136-
$pathToComposerJson
137-
),
138-
$additionalComposerArgs
139-
)
140-
),
141-
'output' => $this->anything()
142-
]
143-
)->willReturnOnConsecutiveCalls(
144-
$this->returnValue(0),
145-
$this->returnValue($appRunResult)
146-
);
147-
} else {
148-
$this->applicationMock->expects($this->any())
149-
->method('run')
150-
->with(
151-
new ArrayInput(
152-
array_merge(
153-
$this->expectedComposerArgumentsSampleDataCommands(
154-
$sampleDataPackages,
155-
$pathToComposerJson
156-
),
157-
$additionalComposerArgs
158-
)
159-
),
160-
$this->anything()
161-
)
162-
->willReturn($appRunResult);
163-
}
120+
$additionalComposerArgs
121+
)
122+
),
123+
$this->anything()
124+
)
125+
->willReturn($appRunResult);
164126

165127
if (($appRunResult !== 0) && !empty($sampleDataPackages)) {
166128
$this->applicationMock->expects($this->any())

0 commit comments

Comments
 (0)