ConfigShowCommandTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Test\Unit\Console\Command;
  7. use Magento\Config\Console\Command\ConfigShowCommand;
  8. use Magento\Framework\App\Config\ConfigSourceInterface;
  9. use Magento\Framework\Console\Cli;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  12. use Magento\Framework\App\Scope\ValidatorInterface;
  13. use Symfony\Component\Console\Tester\CommandTester;
  14. use Magento\Framework\App\Config\ConfigPathResolver;
  15. use Magento\Config\Console\Command\ConfigShow\ValueProcessor;
  16. class ConfigShowCommandTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var ValidatorInterface|MockObject
  20. */
  21. private $scopeValidatorMock;
  22. /**
  23. * @var ConfigSourceInterface|MockObject
  24. */
  25. private $configSourceMock;
  26. /**
  27. * @var ValueProcessor|MockObject
  28. */
  29. private $valueProcessorMock;
  30. /**
  31. * @var ConfigPathResolver|MockObject
  32. */
  33. private $pathResolverMock;
  34. /**
  35. * @var ConfigShowCommand
  36. */
  37. private $command;
  38. protected function setUp()
  39. {
  40. $this->valueProcessorMock = $this->getMockBuilder(ValueProcessor::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->pathResolverMock = $this->getMockBuilder(ConfigPathResolver::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->scopeValidatorMock = $this->getMockBuilder(ValidatorInterface::class)
  47. ->getMockForAbstractClass();
  48. $this->configSourceMock = $this->getMockBuilder(ConfigSourceInterface::class)
  49. ->getMockForAbstractClass();
  50. $this->command = new ConfigShowCommand(
  51. $this->scopeValidatorMock,
  52. $this->configSourceMock,
  53. $this->pathResolverMock,
  54. $this->valueProcessorMock
  55. );
  56. }
  57. public function testExecute()
  58. {
  59. $configPath = 'some/config/path';
  60. $resolvedConfigPath = 'someScope/someScopeCode/some/config/path';
  61. $scope = 'someScope';
  62. $scopeCode = 'someScopeCode';
  63. $this->scopeValidatorMock->expects($this->once())
  64. ->method('isValid')
  65. ->with($scope, $scopeCode)
  66. ->willReturn(true);
  67. $this->pathResolverMock->expects($this->once())
  68. ->method('resolve')
  69. ->with($configPath, $scope, $scopeCode)
  70. ->willReturn($resolvedConfigPath);
  71. $this->configSourceMock->expects($this->once())
  72. ->method('get')
  73. ->with($resolvedConfigPath)
  74. ->willReturn('someValue');
  75. $this->valueProcessorMock->expects($this->once())
  76. ->method('process')
  77. ->with($scope, $scopeCode, 'someValue', $configPath)
  78. ->willReturn('someProcessedValue');
  79. $tester = $this->getConfigShowCommandTester($configPath, $scope, $scopeCode);
  80. $this->assertEquals(
  81. Cli::RETURN_SUCCESS,
  82. $tester->getStatusCode()
  83. );
  84. $this->assertContains(
  85. 'someProcessedValue',
  86. $tester->getDisplay()
  87. );
  88. }
  89. public function testNotValidScopeOrScopeCode()
  90. {
  91. $configPath = 'some/config/path';
  92. $scope = 'someScope';
  93. $scopeCode = 'someScopeCode';
  94. $this->scopeValidatorMock->expects($this->once())
  95. ->method('isValid')
  96. ->with($scope, $scopeCode)
  97. ->willThrowException(new LocalizedException(__('error message')));
  98. $tester = $this->getConfigShowCommandTester($configPath, $scope, $scopeCode);
  99. $this->assertEquals(
  100. Cli::RETURN_FAILURE,
  101. $tester->getStatusCode()
  102. );
  103. $this->assertContains(
  104. __('error message')->render(),
  105. $tester->getDisplay()
  106. );
  107. }
  108. public function testConfigPathNotExist()
  109. {
  110. $configPath = 'some/path';
  111. $tester = $this->getConfigShowCommandTester($configPath);
  112. $this->assertEquals(
  113. Cli::RETURN_FAILURE,
  114. $tester->getStatusCode()
  115. );
  116. $this->assertContains(
  117. __('Configuration for path: "%1" doesn\'t exist', $configPath)->render(),
  118. $tester->getDisplay()
  119. );
  120. }
  121. /**
  122. * @param string $configPath
  123. * @param null|string $scope
  124. * @param null|string $scopeCode
  125. * @return CommandTester
  126. */
  127. private function getConfigShowCommandTester($configPath, $scope = null, $scopeCode = null)
  128. {
  129. $arguments = [
  130. ConfigShowCommand::INPUT_ARGUMENT_PATH => $configPath
  131. ];
  132. if ($scope !== null) {
  133. $arguments['--' . ConfigShowCommand::INPUT_OPTION_SCOPE] = $scope;
  134. }
  135. if ($scopeCode !== null) {
  136. $arguments['--' . ConfigShowCommand::INPUT_OPTION_SCOPE_CODE] = $scopeCode;
  137. }
  138. $tester = new CommandTester($this->command);
  139. $tester->execute($arguments);
  140. return $tester;
  141. }
  142. }