|
| 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\Test\Unit\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\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Console\Tester\CommandTester; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class TablesWhitelistGenerateCommandTest |
| 20 | + * |
| 21 | + * @package Magento\Developer\Test\Unit\Console\Command |
| 22 | + */ |
| 23 | +class TablesWhitelistGenerateCommandTest extends TestCase |
| 24 | +{ |
| 25 | + // Exception Messages! |
| 26 | + const CONFIG_EXCEPTION_MESSAGE = 'Configuration Exception Message'; |
| 27 | + const EXCEPTION_MESSAGE = 'General Exception Message'; |
| 28 | + |
| 29 | + /** @var WhitelistGenerator|MockObject $whitelistGenerator */ |
| 30 | + private $whitelistGenerator; |
| 31 | + |
| 32 | + /** @var GenerateCommand $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( |
| 79 | + function () use ($exception) { |
| 80 | + throw $exception; |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + $commandTest = $this->execute($arguments); |
| 85 | + $this->assertEquals($expected, $commandTest->getStatusCode()); |
| 86 | + $this->assertEquals($output . PHP_EOL, $commandTest->getDisplay()); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Data provider for success test case |
| 91 | + * |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public function successDataProvider() |
| 95 | + { |
| 96 | + return [ |
| 97 | + [ |
| 98 | + 'all', |
| 99 | + Cli::RETURN_SUCCESS, |
| 100 | + |
| 101 | + ], |
| 102 | + [ |
| 103 | + 'Module_Name', |
| 104 | + Cli::RETURN_SUCCESS |
| 105 | + ] |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Data provider for failure test case |
| 111 | + * |
| 112 | + * @return array |
| 113 | + */ |
| 114 | + public function failureDataProvider() |
| 115 | + { |
| 116 | + return [ |
| 117 | + [ |
| 118 | + 'all', |
| 119 | + Cli::RETURN_FAILURE, |
| 120 | + new ConfigException(__('Configuration Exception Message')), |
| 121 | + self::CONFIG_EXCEPTION_MESSAGE |
| 122 | + ], |
| 123 | + [ |
| 124 | + 'Module_Name', |
| 125 | + Cli::RETURN_FAILURE, |
| 126 | + new ConfigException(__('Configuration Exception Message')), |
| 127 | + self::CONFIG_EXCEPTION_MESSAGE |
| 128 | + ], |
| 129 | + [ |
| 130 | + 'all', |
| 131 | + Cli::RETURN_FAILURE, |
| 132 | + new \Exception(self::EXCEPTION_MESSAGE), |
| 133 | + self::EXCEPTION_MESSAGE |
| 134 | + ], |
| 135 | + [ |
| 136 | + 'Module_Name', |
| 137 | + Cli::RETURN_FAILURE, |
| 138 | + new \Exception(self::EXCEPTION_MESSAGE), |
| 139 | + self::EXCEPTION_MESSAGE |
| 140 | + ] |
| 141 | + ]; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Execute command test class for symphony |
| 146 | + * |
| 147 | + * @param string $arguments |
| 148 | + * |
| 149 | + * @return CommandTester |
| 150 | + */ |
| 151 | + private function execute(string $arguments) |
| 152 | + { |
| 153 | + $commandTest = new CommandTester($this->instance); |
| 154 | + $commandTest->execute(['--' . GenerateCommand::MODULE_NAME_KEY => $arguments]); |
| 155 | + |
| 156 | + return $commandTest; |
| 157 | + } |
| 158 | +} |
0 commit comments