Skip to content

Commit 1dd5290

Browse files
committed
Fix #27523: add tests for GeneratePatchCommand
1 parent 1e8e825 commit 1dd5290

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\ReadFactory;
12+
use Magento\Framework\Filesystem\Directory\WriteFactory;
13+
use Magento\Framework\Filesystem\DirectoryList;
14+
use Symfony\Component\Console\Tester\CommandTester;
15+
16+
class GeneratePatchCommandTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var ComponentRegistrar
20+
*/
21+
private $componentRegistrar;
22+
23+
/**
24+
* @var DirectoryList
25+
*/
26+
private $directoryList;
27+
28+
/**
29+
* @var ReadFactory
30+
*/
31+
private $readFactory;
32+
33+
/**
34+
* @var WriteFactory
35+
*/
36+
private $writeFactory;
37+
38+
/**
39+
* @var GeneratePatchCommand
40+
*/
41+
private $command;
42+
43+
protected function setUp()
44+
{
45+
$this->componentRegistrar = $this->createMock(ComponentRegistrar::class);
46+
$this->directoryList = $this->createMock(DirectoryList::class);
47+
$this->readFactory = $this->createMock(ReadFactory::class);
48+
$this->writeFactory = $this->createMock(WriteFactory::class);
49+
50+
$this->command = new GeneratePatchCommand(
51+
$this->componentRegistrar,
52+
$this->directoryList,
53+
$this->readFactory,
54+
$this->writeFactory
55+
);
56+
}
57+
58+
public function testExecute()
59+
{
60+
$this->componentRegistrar->expects($this->once())
61+
->method('getPath')
62+
->with('module', 'Vendor_Module')
63+
->willReturn('/long/path/to/Vendor/Module');
64+
65+
$read = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
66+
$read->expects($this->at(0))
67+
->method('readFile')
68+
->with('patch_template.php.dist')
69+
->willReturn('something');
70+
$this->readFactory->method('create')->willReturn($read);
71+
72+
$write = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
73+
$write->expects($this->once())->method('writeFile');
74+
$this->writeFactory->method('create')->willReturn($write);
75+
76+
$this->directoryList->expects($this->once())->method('getRoot')->willReturn('/some/path');
77+
78+
$commandTester = new CommandTester($this->command);
79+
$commandTester->execute(
80+
[
81+
GeneratePatchCommand::MODULE_NAME => 'Vendor_Module',
82+
GeneratePatchCommand::INPUT_KEY_PATCH_NAME => 'SomePatch'
83+
]
84+
);
85+
$this->assertContains('successfully generated', $commandTester->getDisplay());
86+
}
87+
88+
/**
89+
* @expectedException \RuntimeException
90+
* @expectedExceptionMessage Not enough arguments
91+
*/
92+
public function testWrongParameter()
93+
{
94+
$commandTester = new CommandTester($this->command);
95+
$commandTester->execute([]);
96+
}
97+
98+
/**
99+
* @expectedException \InvalidArgumentException
100+
* @expectedExceptionMessage Cannot find a registered module with name "Fake_Module"
101+
*/
102+
public function testBadModule()
103+
{
104+
$this->componentRegistrar->expects($this->once())
105+
->method('getPath')
106+
->with('module', 'Fake_Module')
107+
->willReturn(null);
108+
109+
$commandTester = new CommandTester($this->command);
110+
$commandTester->execute(
111+
[
112+
GeneratePatchCommand::MODULE_NAME => 'Fake_Module',
113+
GeneratePatchCommand::INPUT_KEY_PATCH_NAME => 'SomePatch'
114+
]
115+
);
116+
$this->assertContains('successfully generated', $commandTester->getDisplay());
117+
}
118+
}

0 commit comments

Comments
 (0)