Skip to content

Commit 9a3510b

Browse files
authored
ENGCOM-7280: Fix #27523: throw informative errors in setup:db:generate-patch #27579
2 parents ced8102 + b881a6f commit 9a3510b

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

app/code/Magento/Developer/Console/Command/GeneratePatchCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
133133
}
134134
$type = $input->getOption(self::INPUT_KEY_PATCH_TYPE);
135135
$modulePath = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
136+
if (null === $modulePath) {
137+
throw new \InvalidArgumentException(sprintf('Cannot find a registered module with name "%s"', $moduleName));
138+
}
136139
$preparedModuleName = str_replace('_', '\\', $moduleName);
137140
$preparedType = ucfirst($type);
138141
$patchInterface = sprintf('%sPatchInterface', $preparedType);
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Developer\Test\Unit\Console\Command;
8+
9+
use Magento\Developer\Console\Command\GeneratePatchCommand;
10+
use Magento\Framework\Component\ComponentRegistrar;
11+
use Magento\Framework\Filesystem\Directory\Read;
12+
use Magento\Framework\Filesystem\Directory\ReadFactory;
13+
use Magento\Framework\Filesystem\Directory\Write;
14+
use Magento\Framework\Filesystem\Directory\WriteFactory;
15+
use Magento\Framework\Filesystem\DirectoryList;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\Console\Tester\CommandTester;
19+
20+
class GeneratePatchCommandTest extends TestCase
21+
{
22+
/**
23+
* @var ComponentRegistrar|MockObject
24+
*/
25+
private $componentRegistrarMock;
26+
27+
/**
28+
* @var DirectoryList|MockObject
29+
*/
30+
private $directoryListMock;
31+
32+
/**
33+
* @var ReadFactory|MockObject
34+
*/
35+
private $readFactoryMock;
36+
37+
/**
38+
* @var WriteFactory|MockObject
39+
*/
40+
private $writeFactoryMock;
41+
42+
/**
43+
* @var GeneratePatchCommand|MockObject
44+
*/
45+
private $command;
46+
47+
protected function setUp(): void
48+
{
49+
$this->componentRegistrarMock = $this->createMock(ComponentRegistrar::class);
50+
$this->directoryListMock = $this->createMock(DirectoryList::class);
51+
$this->readFactoryMock = $this->createMock(ReadFactory::class);
52+
$this->writeFactoryMock = $this->createMock(WriteFactory::class);
53+
54+
$this->command = new GeneratePatchCommand(
55+
$this->componentRegistrarMock,
56+
$this->directoryListMock,
57+
$this->readFactoryMock,
58+
$this->writeFactoryMock
59+
);
60+
}
61+
62+
public function testExecute()
63+
{
64+
$this->componentRegistrarMock->expects($this->once())
65+
->method('getPath')
66+
->with('module', 'Vendor_Module')
67+
->willReturn('/long/path/to/Vendor/Module');
68+
69+
$read = $this->createMock(Read::class);
70+
$read->expects($this->at(0))
71+
->method('readFile')
72+
->with('patch_template.php.dist')
73+
->willReturn('something');
74+
$this->readFactoryMock->method('create')->willReturn($read);
75+
76+
$write = $this->createMock(Write::class);
77+
$write->expects($this->once())->method('writeFile');
78+
$this->writeFactoryMock->method('create')->willReturn($write);
79+
80+
$this->directoryListMock->expects($this->once())->method('getRoot')->willReturn('/some/path');
81+
82+
$commandTester = new CommandTester($this->command);
83+
$commandTester->execute(
84+
[
85+
GeneratePatchCommand::MODULE_NAME => 'Vendor_Module',
86+
GeneratePatchCommand::INPUT_KEY_PATCH_NAME => 'SomePatch'
87+
]
88+
);
89+
$this->assertStringContainsString('successfully generated', $commandTester->getDisplay());
90+
}
91+
92+
public function testWrongParameter()
93+
{
94+
$this->expectExceptionMessage('Not enough arguments');
95+
$this->expectException(\RuntimeException::class);
96+
97+
$commandTester = new CommandTester($this->command);
98+
$commandTester->execute([]);
99+
}
100+
101+
public function testBadModule()
102+
{
103+
$this->componentRegistrarMock->expects($this->once())
104+
->method('getPath')
105+
->with('module', 'Fake_Module')
106+
->willReturn(null);
107+
108+
$this->expectExceptionMessage('Cannot find a registered module with name "Fake_Module"');
109+
$this->expectException(\InvalidArgumentException::class);
110+
111+
$commandTester = new CommandTester($this->command);
112+
$commandTester->execute(
113+
[
114+
GeneratePatchCommand::MODULE_NAME => 'Fake_Module',
115+
GeneratePatchCommand::INPUT_KEY_PATCH_NAME => 'SomePatch'
116+
]
117+
);
118+
}
119+
}

0 commit comments

Comments
 (0)