Skip to content

Commit 7fec913

Browse files
andrewbessAndrii Beziazychnyi
authored and
Andrii Beziazychnyi
committed
magento/issue#19481: minor and codestyle fixes
1 parent b0c3ead commit 7fec913

File tree

4 files changed

+107
-24
lines changed

4 files changed

+107
-24
lines changed

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
use Composer\Console\ApplicationFactory;
1111
use Exception;
1212
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\Console\Cli;
1314
use Magento\Framework\Exception\FileSystemException;
15+
use Magento\Framework\Exception\InvalidArgumentException;
1416
use Magento\Framework\Exception\LocalizedException;
1517
use Magento\Framework\Filesystem;
18+
use Magento\Framework\Serialize\Serializer\Json;
1619
use Magento\SampleData\Model\Dependency;
1720
use Magento\Setup\Model\PackagesAuth;
1821
use Symfony\Component\Console\Command\Command;
@@ -24,6 +27,8 @@
2427

2528
/**
2629
* Command for deployment of Sample Data
30+
*
31+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2732
*/
2833
class SampleDataDeployCommand extends Command
2934
{
@@ -50,22 +55,30 @@ class SampleDataDeployCommand extends Command
5055
*/
5156
private $applicationFactory;
5257

58+
/**
59+
* @var Json
60+
*/
61+
private $serializer;
62+
5363
/**
5464
* @param Filesystem $filesystem
5565
* @param Dependency $sampleDataDependency
5666
* @param ArrayInputFactory $arrayInputFactory
5767
* @param ApplicationFactory $applicationFactory
68+
* @param Json $serializer
5869
*/
5970
public function __construct(
6071
Filesystem $filesystem,
6172
Dependency $sampleDataDependency,
6273
ArrayInputFactory $arrayInputFactory,
63-
ApplicationFactory $applicationFactory
74+
ApplicationFactory $applicationFactory,
75+
Json $serializer
6476
) {
6577
$this->filesystem = $filesystem;
6678
$this->sampleDataDependency = $sampleDataDependency;
6779
$this->arrayInputFactory = $arrayInputFactory;
6880
$this->applicationFactory = $applicationFactory;
81+
$this->serializer = $serializer;
6982
parent::__construct();
7083
}
7184

@@ -90,20 +103,20 @@ protected function configure()
90103
*
91104
* @param InputInterface $input
92105
* @param OutputInterface $output
93-
* @return int|void
106+
* @return int
94107
* @throws FileSystemException
95108
* @throws LocalizedException
96109
*/
97110
protected function execute(InputInterface $input, OutputInterface $output)
98111
{
99-
$rootJson = json_decode(
112+
$rootJson = $this->serializer->unserialize(
100113
$this->filesystem->getDirectoryRead(
101114
DirectoryList::ROOT
102115
)->readFile("composer.json")
103116
);
104-
if (!isset($rootJson->version)) {
117+
if (!isset($rootJson['version'])) {
105118
$magentoProductPackage = array_filter(
106-
(array) $rootJson->require,
119+
$rootJson['require'],
107120
function ($package) {
108121
return false !== strpos($package, 'magento/product-');
109122
},
@@ -156,9 +169,15 @@ function ($package) {
156169
. '</info>'
157170
);
158171
$application->resetComposer();
172+
173+
return Cli::RETURN_FAILURE;
159174
}
175+
176+
return Cli::RETURN_SUCCESS;
160177
} else {
161178
$output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
179+
180+
return Cli::RETURN_FAILURE;
162181
}
163182
}
164183

@@ -189,15 +208,30 @@ private function createAuthFile()
189208
/**
190209
* Updates PHP memory limit
191210
*
211+
* @throws InvalidArgumentException
192212
* @return void
193213
*/
194214
private function updateMemoryLimit()
195215
{
196216
if (function_exists('ini_set')) {
197-
@ini_set('display_errors', 1);
217+
$result = ini_alter('display_errors', 1);
218+
if ($result === false) {
219+
$error = error_get_last();
220+
throw new InvalidArgumentException(__(
221+
'Failed to set ini option display_errors to value 1. %1',
222+
$error['message']
223+
));
224+
}
198225
$memoryLimit = trim(ini_get('memory_limit'));
199226
if ($memoryLimit != -1 && $this->getMemoryInBytes($memoryLimit) < 756 * 1024 * 1024) {
200-
@ini_set('memory_limit', '756M');
227+
$result = ini_alter('memory_limit', '756M');
228+
if ($result === false) {
229+
$error = error_get_last();
230+
throw new InvalidArgumentException(__(
231+
'Failed to set ini option memory_limit to 756M. %1',
232+
$error['message']
233+
));
234+
}
201235
}
202236
}
203237
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
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+
2939
/**
3040
* @var ReadInterface|MockObject
3141
*/
@@ -121,7 +131,7 @@ protected function setupMocks(
121131
->withConsecutive(
122132
[
123133
'input' => new ArrayInput(
124-
$this->expectedComposerArgumentsCommandConfig()
134+
self::STUB_EXPECTED_COMPOSER_CONFIG
125135
),
126136
'output' => $this->anything()
127137
],

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

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,46 @@
99

1010
use Exception;
1111
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Framework\Serialize\Serializer\Json;
1213
use Magento\SampleData\Console\Command\SampleDataDeployCommand;
1314
use Magento\Setup\Model\PackagesAuth;
15+
use PHPUnit\Framework\MockObject\MockObject;
1416
use Symfony\Component\Console\Tester\CommandTester;
1517

1618
/**
1719
* Tests for command `sampledata:deploy`
1820
*/
1921
class SampleDataDeployCommandTest extends AbstractSampleDataCommandTest
2022
{
23+
/**
24+
* @var Json|MockObject
25+
*/
26+
private $serializerMock;
27+
28+
/**
29+
* Creates mocks
30+
*
31+
* @return void
32+
*/
33+
protected function setUp(): void
34+
{
35+
parent::setUp();
36+
37+
$this->serializerMock = $this->createMock(Json::class);
38+
}
39+
40+
/**
41+
* Sets mock for unserialization composer content
42+
* @param array $composerJsonContent
43+
* @return void
44+
*/
45+
protected function setupMockForSerializer(array $composerJsonContent): void
46+
{
47+
$this->serializerMock->expects($this->any())
48+
->method('unserialize')
49+
->will($this->returnValue($composerJsonContent));
50+
}
51+
2152
/**
2253
* Sets mocks for auth file
2354
*
@@ -63,6 +94,7 @@ public function testExecute(
6394
$composerJsonContent
6495
);
6596
$this->setupMocksForAuthFile($authExist);
97+
$this->setupMockForSerializer($composerJsonContent);
6698
$commandTester = $this->createCommandTester();
6799
$commandTester->execute([]);
68100

@@ -94,6 +126,7 @@ public function testExecuteWithNoUpdate(
94126
['--no-update' => 1]
95127
);
96128
$this->setupMocksForAuthFile($authExist);
129+
$this->setupMockForSerializer($composerJsonContent);
97130
$commandInput = ['--no-update' => 1];
98131

99132
$commandTester = $this->createCommandTester();
@@ -172,6 +205,12 @@ public function testExecuteWithException()
172205
->method('readFile')
173206
->with('composer.json')
174207
->willReturn('{"require": {"magento/product-community-edition": "0.0.1"}, "version": "0.0.1"}');
208+
$this->serializerMock->expects($this->any())
209+
->method('unserialize')
210+
->will($this->returnValue([
211+
'require' => ["magento/product-community-edition" => "0.0.1"],
212+
'version' => '0.0.1'
213+
]));
175214
$this->filesystemMock->expects($this->once())
176215
->method('getDirectoryRead')
177216
->with(DirectoryList::ROOT)
@@ -208,26 +247,12 @@ private function createCommandTester(): CommandTester
208247
$this->filesystemMock,
209248
$this->sampleDataDependencyMock,
210249
$this->arrayInputFactoryMock,
211-
$this->applicationFactoryMock
250+
$this->applicationFactoryMock,
251+
$this->serializerMock
212252
)
213253
);
214254
}
215255

216-
/**
217-
* Expected arguments for `composer config` to set missing field "version"
218-
*
219-
* @return array
220-
*/
221-
protected function expectedComposerArgumentsCommandConfig(): array
222-
{
223-
return [
224-
'command' => 'config',
225-
'setting-key' => 'version',
226-
'setting-value' => ['0.0.1'],
227-
'--quiet' => 1
228-
];
229-
}
230-
231256
/**
232257
* Returns expected arguments for command `composer require`
233258
*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Exception;
8+
9+
/**
10+
* An exception to be thrown when arguments are invalidated
11+
*/
12+
class InvalidArgumentException extends LocalizedException
13+
{
14+
}

0 commit comments

Comments
 (0)