|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Deploy\Test\Unit\Console; |
| 8 | + |
| 9 | +use Magento\Framework\Validator\Regex; |
| 10 | +use Magento\Framework\Validator\RegexFactory; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Magento\Deploy\Console\InputValidator; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 14 | +use Magento\Deploy\Console\DeployStaticOptions as Options; |
| 15 | +use Magento\Framework\Validator\Locale; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Input\InputDefinition; |
| 18 | +use Symfony\Component\Console\Input\ArrayInput; |
| 19 | +use InvalidArgumentException; |
| 20 | +use Symfony\Component\Console\Input\InputArgument; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class InputValidatorTest |
| 24 | + * @package Magento\Deploy\Test\Unit\Console |
| 25 | + * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 26 | + */ |
| 27 | +class InputValidatorTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var ObjectManagerHelper |
| 31 | + */ |
| 32 | + protected $objectManagerHelper; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var InputValidator |
| 36 | + */ |
| 37 | + protected $inputValidator; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Locale |
| 41 | + */ |
| 42 | + protected $localeValidator; |
| 43 | + |
| 44 | + /** |
| 45 | + * @throws \Zend_Validate_Exception |
| 46 | + */ |
| 47 | + protected function setUp() |
| 48 | + { |
| 49 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 50 | + |
| 51 | + $regexFactoryMock = $this->getMockBuilder(RegexFactory::class) |
| 52 | + ->disableOriginalConstructor() |
| 53 | + ->setMethods(['create']) |
| 54 | + ->getMock(); |
| 55 | + |
| 56 | + $regexObject = new Regex('/^[A-Za-z0-9_.]+$/'); |
| 57 | + |
| 58 | + $regexFactoryMock->expects($this->any())->method('create') |
| 59 | + ->willReturn($regexObject); |
| 60 | + |
| 61 | + $localeObjectMock = $this->getMockBuilder(Locale::class)->setMethods(['isValid']) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + |
| 65 | + $localeObjectMock->expects($this->any())->method('isValid') |
| 66 | + ->with('en_US') |
| 67 | + ->will($this->returnValue(true)); |
| 68 | + |
| 69 | + $this->inputValidator = $this->objectManagerHelper->getObject( |
| 70 | + InputValidator::class, |
| 71 | + [ |
| 72 | + 'localeValidator' => $localeObjectMock, |
| 73 | + 'versionValidatorFactory' => $regexFactoryMock |
| 74 | + ] |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @throws \Zend_Validate_Exception |
| 80 | + */ |
| 81 | + public function testValidate() |
| 82 | + { |
| 83 | + $input = $this->getMockBuilder(ArrayInput::class) |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->setMethods(['getOption', 'getArgument']) |
| 86 | + ->getMock(); |
| 87 | + |
| 88 | + $input->expects($this->atLeastOnce())->method('getArgument')->willReturn(['all']); |
| 89 | + |
| 90 | + $input->expects($this->atLeastOnce())->method('getOption') |
| 91 | + ->willReturnMap( |
| 92 | + [ |
| 93 | + [Options::AREA, ['all']], |
| 94 | + [Options::EXCLUDE_AREA, ['none']], |
| 95 | + [Options::THEME, ['all']], |
| 96 | + [Options::EXCLUDE_THEME, ['none']], |
| 97 | + [Options::EXCLUDE_LANGUAGE, ['none']], |
| 98 | + [Options::CONTENT_VERSION, '12345'] |
| 99 | + ] |
| 100 | + ); |
| 101 | + |
| 102 | + /** @noinspection PhpParamsInspection */ |
| 103 | + $this->inputValidator->validate($input); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @covers \Magento\Deploy\Console\InputValidator::checkAreasInput() |
| 108 | + */ |
| 109 | + public function testCheckAreasInputException() |
| 110 | + { |
| 111 | + $options = [ |
| 112 | + new InputOption(Options::AREA, null, 4, null, ['test']), |
| 113 | + new InputOption(Options::EXCLUDE_AREA, null, 4, null, ['test']) |
| 114 | + ]; |
| 115 | + |
| 116 | + $inputDefinition = new InputDefinition($options); |
| 117 | + |
| 118 | + try { |
| 119 | + $this->inputValidator->validate( |
| 120 | + new ArrayInput([], $inputDefinition) |
| 121 | + ); |
| 122 | + } catch (\Exception $e) { |
| 123 | + $this->assertContains('--area (-a) and --exclude-area cannot be used at the same time', $e->getMessage()); |
| 124 | + $this->assertInstanceOf(InvalidArgumentException::class, $e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @covers \Magento\Deploy\Console\InputValidator::checkThemesInput() |
| 130 | + */ |
| 131 | + public function testCheckThemesInputException() |
| 132 | + { |
| 133 | + $options = [ |
| 134 | + new InputOption(Options::AREA, null, 4, null, ['all']), |
| 135 | + new InputOption(Options::EXCLUDE_AREA, null, 4, null, ['none']), |
| 136 | + new InputOption(Options::THEME, null, 4, '', ['blank']), |
| 137 | + new InputOption(Options::EXCLUDE_THEME, null, 4, '', ['luma']) |
| 138 | + ]; |
| 139 | + |
| 140 | + $inputDefinition = new InputDefinition($options); |
| 141 | + |
| 142 | + try { |
| 143 | + $this->inputValidator->validate( |
| 144 | + new ArrayInput([], $inputDefinition) |
| 145 | + ); |
| 146 | + } catch (\Exception $e) { |
| 147 | + $this->assertContains('--theme (-t) and --exclude-theme cannot be used at the same time', $e->getMessage()); |
| 148 | + $this->assertInstanceOf(InvalidArgumentException::class, $e); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + public function testCheckLanguagesInputException() |
| 153 | + { |
| 154 | + $options = [ |
| 155 | + new InputOption(Options::AREA, null, 4, null, ['all']), |
| 156 | + new InputOption(Options::EXCLUDE_AREA, null, 4, null, ['none']), |
| 157 | + new InputOption(Options::THEME, null, 4, '', ['all']), |
| 158 | + new InputOption(Options::EXCLUDE_THEME, null, 4, '', ['none']), |
| 159 | + new InputArgument(Options::LANGUAGES_ARGUMENT, null, 4, ['en_US']), |
| 160 | + new InputOption(Options::EXCLUDE_LANGUAGE, null, 4, '', ['all']) |
| 161 | + ]; |
| 162 | + |
| 163 | + $inputDefinition = new InputDefinition($options); |
| 164 | + |
| 165 | + try { |
| 166 | + $this->inputValidator->validate( |
| 167 | + new ArrayInput([], $inputDefinition) |
| 168 | + ); |
| 169 | + } catch (\Exception $e) { |
| 170 | + $this->assertContains( |
| 171 | + '--language (-l) and --exclude-language cannot be used at the same time', |
| 172 | + $e->getMessage() |
| 173 | + ); |
| 174 | + |
| 175 | + $this->assertInstanceOf(InvalidArgumentException::class, $e); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + public function testCheckVersionInputException() |
| 180 | + { |
| 181 | + $options = [ |
| 182 | + new InputOption(Options::AREA, null, 4, null, ['all']), |
| 183 | + new InputOption(Options::EXCLUDE_AREA, null, 4, null, ['none']), |
| 184 | + new InputOption(Options::THEME, null, 4, '', ['all']), |
| 185 | + new InputOption(Options::EXCLUDE_THEME, null, 4, '', ['none']), |
| 186 | + new InputArgument(Options::LANGUAGES_ARGUMENT, null, 4, ['en_US']), |
| 187 | + new InputOption(Options::EXCLUDE_LANGUAGE, null, 4, '', ['none']), |
| 188 | + new InputOption(Options::CONTENT_VERSION, null, 4, '', '/*!#') |
| 189 | + ]; |
| 190 | + |
| 191 | + $inputDefinition = new InputDefinition($options); |
| 192 | + |
| 193 | + try { |
| 194 | + $this->inputValidator->validate( |
| 195 | + new ArrayInput([], $inputDefinition) |
| 196 | + ); |
| 197 | + } catch (\Exception $e) { |
| 198 | + $this->assertContains( |
| 199 | + 'Argument "' . |
| 200 | + Options::CONTENT_VERSION |
| 201 | + . '" has invalid value, content version should contain only characters, digits and dots', |
| 202 | + $e->getMessage() |
| 203 | + ); |
| 204 | + |
| 205 | + $this->assertInstanceOf(InvalidArgumentException::class, $e); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments