Skip to content

Commit 716938f

Browse files
committed
Unit Test for Whitelist Command Class
1 parent f0ae717 commit 716938f

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Developer\Console\Command;
9+
10+
use Magento\Developer\Console\Command\TablesWhitelistGenerateCommand as GenerateCommand;
11+
use Magento\Developer\Model\Setup\Declaration\Schema\WhitelistGenerator;
12+
use Magento\Framework\Console\Cli;
13+
use Magento\Framework\Exception\ConfigurationMismatchException as ConfigException;
14+
use PHPUnit\Framework\TestCase;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use Symfony\Component\Console\Tester\CommandTester;
17+
18+
/**
19+
* Class TablesWhitelistGenerateCommandTest
20+
*
21+
* @package Magento\Developer\Console\Command
22+
*/
23+
class TablesWhitelistGenerateCommandTest extends TestCase
24+
{
25+
// Exception Messages!
26+
const CONFIG_EXCEPTION = 'Configuration Exception';
27+
const EXCEPTION = 'General Exception';
28+
29+
/** @var WhitelistGenerator|MockObject $whitelistGenerator */
30+
private $whitelistGenerator;
31+
32+
/** @var TablesWhitelistGenerateCommand $instance */
33+
private $instance;
34+
35+
protected function setUp()
36+
{
37+
$this->whitelistGenerator = $this->getMockBuilder(WhitelistGenerator::class)
38+
->disableOriginalConstructor()
39+
->getMock();
40+
41+
$this->instance = new GenerateCommand($this->whitelistGenerator);
42+
}
43+
44+
/**
45+
* Test case for success scenario
46+
*
47+
* @param string $arguments
48+
* @param string $expected
49+
*
50+
* @dataProvider successDataProvider
51+
*/
52+
public function testCommandSuccess(string $arguments, string $expected)
53+
{
54+
$this->whitelistGenerator->expects($this->once())
55+
->method('generate')
56+
->with($arguments);
57+
58+
$commandTest = $this->execute($arguments);
59+
$this->assertEquals($expected, $commandTest->getStatusCode());
60+
$this->assertEquals('', $commandTest->getDisplay());
61+
}
62+
63+
/**
64+
* Test case for failure scenario
65+
*
66+
* @param string $arguments
67+
* @param string $expected
68+
* @param \Exception|ConfigException $exception
69+
* @param string $output
70+
*
71+
* @dataProvider failureDataProvider
72+
*/
73+
public function testCommandFailure(string $arguments, string $expected, $exception, string $output)
74+
{
75+
$this->whitelistGenerator->expects($this->once())
76+
->method('generate')
77+
->with($arguments)
78+
->willReturnCallback(function () use ($exception) {
79+
throw $exception;
80+
});
81+
82+
$commandTest = $this->execute($arguments);
83+
$this->assertEquals($expected, $commandTest->getStatusCode());
84+
$this->assertEquals($output . PHP_EOL, $commandTest->getDisplay());
85+
}
86+
87+
/**
88+
* Data provider for success test case
89+
*
90+
* @return array
91+
*/
92+
public function successDataProvider()
93+
{
94+
return [
95+
[
96+
'all',
97+
Cli::RETURN_SUCCESS,
98+
99+
],
100+
[
101+
'Module_Name',
102+
Cli::RETURN_SUCCESS
103+
]
104+
];
105+
}
106+
107+
/**
108+
* Data provider for failure test case
109+
*
110+
* @return array
111+
*/
112+
public function failureDataProvider()
113+
{
114+
return [
115+
[
116+
'all',
117+
Cli::RETURN_FAILURE,
118+
new ConfigException(__(self::CONFIG_EXCEPTION)),
119+
self::CONFIG_EXCEPTION
120+
],
121+
[
122+
'Module_Namer',
123+
Cli::RETURN_FAILURE,
124+
new ConfigException(__(self::CONFIG_EXCEPTION)),
125+
self::CONFIG_EXCEPTION
126+
],
127+
[
128+
'all',
129+
Cli::RETURN_FAILURE,
130+
new \Exception(self::EXCEPTION),
131+
self::EXCEPTION
132+
],
133+
[
134+
'Module_Name',
135+
Cli::RETURN_FAILURE,
136+
new \Exception(self::EXCEPTION),
137+
self::EXCEPTION
138+
]
139+
];
140+
}
141+
142+
/**
143+
* Execute command test class for symphony
144+
*
145+
* @param string $arguments
146+
* @return CommandTester
147+
*/
148+
private function execute(string $arguments)
149+
{
150+
$commandTest = new CommandTester($this->instance);
151+
$commandTest->execute(['--' . GenerateCommand::MODULE_NAME_KEY => $arguments]);
152+
153+
return $commandTest;
154+
}
155+
}

0 commit comments

Comments
 (0)