Skip to content

Commit d20b8bf

Browse files
MCLOUD-4094: Update direct check of specific declaration in root composer.json (magento#704)
1 parent c0a64ee commit d20b8bf

File tree

3 files changed

+122
-15
lines changed

3 files changed

+122
-15
lines changed

src/Config/Validator/Build/ComposerFile.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\MagentoCloud\Filesystem\FileList;
1414
use Magento\MagentoCloud\Filesystem\FileSystemException;
1515
use Magento\MagentoCloud\Package\MagentoVersion;
16+
use Magento\MagentoCloud\Package\Manager;
1617
use Magento\MagentoCloud\Package\UndefinedPackageException;
1718

1819
/**
@@ -40,28 +41,46 @@ class ComposerFile implements ValidatorInterface
4041
*/
4142
private $file;
4243

44+
/**
45+
* @var Manager
46+
*/
47+
private $manager;
48+
49+
/**
50+
* @var array
51+
*/
52+
private static $map = [
53+
'laminas/laminas-mvc' => 'Laminas\\Mvc\\Controller\\',
54+
'zendframework/zend-mvc' => 'Zend\\Mvc\\Controller\\',
55+
];
56+
4357
/**
4458
* @param FileList $fileList
4559
* @param File $file
4660
* @param MagentoVersion $magentoVersion
4761
* @param Validator\ResultFactory $resultFactory
62+
* @param Manager $manager
4863
*/
4964
public function __construct(
5065
FileList $fileList,
5166
File $file,
5267
MagentoVersion $magentoVersion,
53-
Validator\ResultFactory $resultFactory
68+
Validator\ResultFactory $resultFactory,
69+
Manager $manager
5470
) {
5571
$this->fileList = $fileList;
5672
$this->file = $file;
5773
$this->magentoVersion = $magentoVersion;
5874
$this->resultFactory = $resultFactory;
75+
$this->manager = $manager;
5976
}
6077

6178
/**
6279
* Validates that composer.json has all required configuration for correct deployment.
6380
*
6481
* @return Validator\ResultInterface
82+
*
83+
* @throws UndefinedPackageException
6584
*/
6685
public function validate(): Validator\ResultInterface
6786
{
@@ -78,14 +97,28 @@ public function validate(): Validator\ResultInterface
7897
return $this->resultFactory->error('Can\'t read composer.json file: ' . $e->getMessage());
7998
}
8099

81-
if (!isset($autoloadPsr4['Zend\Mvc\Controller\\'])) {
82-
return $this->resultFactory->error(
83-
'Required configuration is missed in autoload section of composer.json file.',
84-
'Add ("Zend\\\\Mvc\\\\Controller\\\\": "setup/src/Zend/Mvc/Controller/") to autoload -> psr-4 section' .
85-
' and re-run "composer update" command locally. Then commit new composer.json and composer.lock files.'
86-
);
100+
if (array_intersect_key(($autoloadPsr4), array_flip(self::$map))) {
101+
return $this->resultFactory->success();
102+
}
103+
104+
foreach (self::$map as $name => $namespace) {
105+
if ($this->manager->has($name)) {
106+
return $this->resultFactory->error(
107+
'Required configuration is missed in autoload section of composer.json file.',
108+
sprintf(
109+
'Add ("%s: "%s") to autoload -> psr-4 section ' .
110+
'and re-run "composer update" command locally. ' .
111+
'Then commit new composer.json and composer.lock files.',
112+
$namespace,
113+
'setup/src/Zend/Mvc/Controller/'
114+
)
115+
);
116+
}
87117
}
88118

119+
/**
120+
* Edge case when none of MVC packages installed.
121+
*/
89122
return $this->resultFactory->success();
90123
}
91124
}

src/Test/Unit/Config/Validator/Build/ComposerFileTest.php

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
namespace Magento\MagentoCloud\Test\Unit\Config\Validator\Build;
99

10+
use Composer\Package\PackageInterface;
1011
use Magento\MagentoCloud\Config\Validator\Build\ComposerFile;
1112
use Magento\MagentoCloud\Config\Validator\ResultFactory;
1213
use Magento\MagentoCloud\Filesystem\Driver\File;
1314
use Magento\MagentoCloud\Filesystem\FileList;
1415
use Magento\MagentoCloud\Package\MagentoVersion;
16+
use Magento\MagentoCloud\Package\Manager;
1517
use Magento\MagentoCloud\Package\UndefinedPackageException;
1618
use PHPUnit\Framework\MockObject\MockObject;
1719
use PHPUnit\Framework\TestCase;
@@ -41,24 +43,34 @@ class ComposerFileTest extends TestCase
4143
*/
4244
private $resultFactoryMock;
4345

46+
/**
47+
* @var Manager|MockObject
48+
*/
49+
private $managerMock;
50+
4451
/**
4552
* @inheritdoc
4653
*/
47-
protected function setUp()
54+
protected function setUp(): void
4855
{
4956
$this->fileListMock = $this->createMock(FileList::class);
5057
$this->magentoVersionMock = $this->createMock(MagentoVersion::class);
5158
$this->resultFactoryMock = $this->createMock(ResultFactory::class);
59+
$this->managerMock = $this->createMock(Manager::class);
5260

5361
$this->validator = new ComposerFile(
5462
$this->fileListMock,
5563
new File(),
5664
$this->magentoVersionMock,
57-
$this->resultFactoryMock
65+
$this->resultFactoryMock,
66+
$this->managerMock
5867
);
5968
}
6069

61-
public function testValidateCorrectComposerJson()
70+
/**
71+
* @throws UndefinedPackageException
72+
*/
73+
public function testValidateCorrectComposerJson(): void
6274
{
6375
$this->magentoVersionMock->expects($this->once())
6476
->method('isGreaterOrEqual')
@@ -73,7 +85,28 @@ public function testValidateCorrectComposerJson()
7385
$this->validator->validate();
7486
}
7587

76-
public function testValidateWrongComposerJson()
88+
/**
89+
* @throws UndefinedPackageException
90+
*/
91+
public function testValidateCorrectLaminasComposerJson(): void
92+
{
93+
$this->magentoVersionMock->expects($this->once())
94+
->method('isGreaterOrEqual')
95+
->with('2.3')
96+
->willReturn(true);
97+
$this->fileListMock->expects($this->once())
98+
->method('getMagentoComposer')
99+
->willReturn(__DIR__ . '/_files/correct_composer_2.3_2.json');
100+
$this->resultFactoryMock->expects($this->once())
101+
->method('success');
102+
103+
$this->validator->validate();
104+
}
105+
106+
/**
107+
* @throws UndefinedPackageException
108+
*/
109+
public function testValidateWrongComposerJson(): void
77110
{
78111
$this->magentoVersionMock->expects($this->once())
79112
->method('isGreaterOrEqual')
@@ -83,12 +116,25 @@ public function testValidateWrongComposerJson()
83116
->method('getMagentoComposer')
84117
->willReturn(__DIR__ . '/_files/wrong_composer_2.3.json');
85118
$this->resultFactoryMock->expects($this->once())
86-
->method('error');
119+
->method('error')
120+
->with(
121+
'Required configuration is missed in autoload section of composer.json file.',
122+
'Add ("Laminas\Mvc\Controller\: "setup/src/Zend/Mvc/Controller/") to autoload -> psr-4 ' .
123+
'section and re-run "composer update" command locally. Then commit new composer.json ' .
124+
'and composer.lock files.'
125+
);
126+
$this->managerMock->expects($this->once())
127+
->method('has')
128+
->with('laminas/laminas-mvc')
129+
->willReturn(true);
87130

88131
$this->validator->validate();
89132
}
90133

91-
public function testValidateMagentoLover2dot3()
134+
/**
135+
* @throws UndefinedPackageException
136+
*/
137+
public function testValidateMagentoLower23(): void
92138
{
93139
$this->magentoVersionMock->expects($this->once())
94140
->method('isGreaterOrEqual')
@@ -102,7 +148,10 @@ public function testValidateMagentoLover2dot3()
102148
$this->validator->validate();
103149
}
104150

105-
public function testValidateComposerFileNotExists()
151+
/**
152+
* @throws UndefinedPackageException
153+
*/
154+
public function testValidateComposerFileNotExists(): void
106155
{
107156
$this->magentoVersionMock->expects($this->once())
108157
->method('isGreaterOrEqual')
@@ -118,7 +167,10 @@ public function testValidateComposerFileNotExists()
118167
$this->validator->validate();
119168
}
120169

121-
public function testValidateCantGetMagentoVersion()
170+
/**
171+
* @throws UndefinedPackageException
172+
*/
173+
public function testValidateCantGetMagentoVersion(): void
122174
{
123175
$this->magentoVersionMock->expects($this->once())
124176
->method('isGreaterOrEqual')
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"require": {
3+
"magento/product-enterprise-edition": "2.3"
4+
},
5+
"autoload": {
6+
"psr-4": {
7+
"Magento\\Framework\\": "lib/internal/Magento/Framework/",
8+
"Magento\\Setup\\": "setup/src/Magento/Setup/",
9+
"Magento\\": "app/code/Magento/",
10+
"Laminas\\Mvc\\Controller\\": "setup/src/Zend/Mvc/Controller/"
11+
},
12+
"psr-0": {
13+
"": [
14+
"app/code/",
15+
"generated/code/"
16+
]
17+
},
18+
"files": [
19+
"app/etc/NonComposerComponentRegistration.php"
20+
]
21+
}
22+
}

0 commit comments

Comments
 (0)