InputValidatorTest.php 6.9 KB

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