Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
$type = $input->getOption(self::INPUT_KEY_PATCH_TYPE);
$modulePath = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
if (null === $modulePath) {
throw new \InvalidArgumentException(sprintf('Cannot find a registered module with name "%s"', $moduleName));
}
$preparedModuleName = str_replace('_', '\\', $moduleName);
$preparedType = ucfirst($type);
$patchInterface = sprintf('%sPatchInterface', $preparedType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Developer\Test\Unit\Console\Command;

use Magento\Developer\Console\Command\GeneratePatchCommand;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Filesystem\Directory\ReadFactory;
use Magento\Framework\Filesystem\Directory\WriteFactory;
use Magento\Framework\Filesystem\DirectoryList;
use Symfony\Component\Console\Tester\CommandTester;

class GeneratePatchCommandTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ComponentRegistrar
*/
private $componentRegistrar;
Comment thread
korostii marked this conversation as resolved.
Outdated

/**
* @var DirectoryList
Comment thread
korostii marked this conversation as resolved.
Outdated
*/
private $directoryList;

/**
* @var ReadFactory
*/
private $readFactory;

/**
* @var WriteFactory
*/
private $writeFactory;

/**
* @var GeneratePatchCommand
*/
private $command;

protected function setUp()
{
$this->componentRegistrar = $this->createMock(ComponentRegistrar::class);
$this->directoryList = $this->createMock(DirectoryList::class);
$this->readFactory = $this->createMock(ReadFactory::class);
$this->writeFactory = $this->createMock(WriteFactory::class);

$this->command = new GeneratePatchCommand(
$this->componentRegistrar,
$this->directoryList,
$this->readFactory,
$this->writeFactory
);
}

public function testExecute()
{
$this->componentRegistrar->expects($this->once())
->method('getPath')
->with('module', 'Vendor_Module')
->willReturn('/long/path/to/Vendor/Module');

$read = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
Comment thread
korostii marked this conversation as resolved.
Outdated
$read->expects($this->at(0))
->method('readFile')
->with('patch_template.php.dist')
->willReturn('something');
$this->readFactory->method('create')->willReturn($read);

$write = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
$write->expects($this->once())->method('writeFile');
$this->writeFactory->method('create')->willReturn($write);

$this->directoryList->expects($this->once())->method('getRoot')->willReturn('/some/path');

$commandTester = new CommandTester($this->command);
$commandTester->execute(
[
GeneratePatchCommand::MODULE_NAME => 'Vendor_Module',
GeneratePatchCommand::INPUT_KEY_PATCH_NAME => 'SomePatch'
]
);
$this->assertContains('successfully generated', $commandTester->getDisplay());
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Not enough arguments
Comment thread
korostii marked this conversation as resolved.
Outdated
*/
public function testWrongParameter()
{
$commandTester = new CommandTester($this->command);
$commandTester->execute([]);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot find a registered module with name "Fake_Module"
*/
public function testBadModule()
{
$this->componentRegistrar->expects($this->once())
->method('getPath')
->with('module', 'Fake_Module')
->willReturn(null);

$commandTester = new CommandTester($this->command);
$commandTester->execute(
[
GeneratePatchCommand::MODULE_NAME => 'Fake_Module',
GeneratePatchCommand::INPUT_KEY_PATCH_NAME => 'SomePatch'
]
);
$this->assertContains('successfully generated', $commandTester->getDisplay());
}
}