ConfigSetCommandTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\ConfigSet\ProcessorFacadeFactory;
  8. use Magento\Config\Console\Command\ConfigSet\ProcessorFacade;
  9. use Magento\Config\Console\Command\ConfigSetCommand;
  10. use Magento\Config\Console\Command\EmulatedAdminhtmlAreaProcessor;
  11. use Magento\Deploy\Model\DeploymentConfig\ChangeDetector;
  12. use Magento\Framework\App\DeploymentConfig;
  13. use Magento\Framework\Console\Cli;
  14. use Magento\Framework\Exception\ValidatorException;
  15. use PHPUnit_Framework_MockObject_MockObject as Mock;
  16. use Symfony\Component\Console\Tester\CommandTester;
  17. /**
  18. * Test for ConfigSetCommand.
  19. *
  20. * @see ConfigSetCommand
  21. */
  22. class ConfigSetCommandTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /**
  25. * @var ConfigSetCommand
  26. */
  27. private $command;
  28. /**
  29. * @var EmulatedAdminhtmlAreaProcessor|Mock
  30. */
  31. private $emulatedAreProcessorMock;
  32. /**
  33. * @var ChangeDetector|Mock
  34. */
  35. private $changeDetectorMock;
  36. /**
  37. * @var ProcessorFacadeFactory|Mock
  38. */
  39. private $processorFacadeFactoryMock;
  40. /**
  41. * @var DeploymentConfig|Mock
  42. */
  43. private $deploymentConfigMock;
  44. /**
  45. * @var ProcessorFacade|Mock
  46. */
  47. private $processorFacadeMock;
  48. /**
  49. * @inheritdoc
  50. */
  51. protected function setUp()
  52. {
  53. $this->emulatedAreProcessorMock = $this->getMockBuilder(EmulatedAdminhtmlAreaProcessor::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->changeDetectorMock = $this->getMockBuilder(ChangeDetector::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->processorFacadeFactoryMock = $this->getMockBuilder(ProcessorFacadeFactory::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->processorFacadeMock = $this->getMockBuilder(ProcessorFacade::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->command = new ConfigSetCommand(
  69. $this->emulatedAreProcessorMock,
  70. $this->changeDetectorMock,
  71. $this->processorFacadeFactoryMock,
  72. $this->deploymentConfigMock
  73. );
  74. }
  75. public function testExecute()
  76. {
  77. $this->deploymentConfigMock->expects($this->once())
  78. ->method('isAvailable')
  79. ->willReturn(true);
  80. $this->changeDetectorMock->expects($this->once())
  81. ->method('hasChanges')
  82. ->willReturn(false);
  83. $this->processorFacadeFactoryMock->expects($this->once())
  84. ->method('create')
  85. ->willReturn($this->processorFacadeMock);
  86. $this->processorFacadeMock->expects($this->once())
  87. ->method('processWithLockTarget')
  88. ->willReturn('Some message');
  89. $this->emulatedAreProcessorMock->expects($this->once())
  90. ->method('process')
  91. ->willReturnCallback(function ($function) {
  92. return $function();
  93. });
  94. $tester = new CommandTester($this->command);
  95. $tester->execute([
  96. ConfigSetCommand::ARG_PATH => 'test/test/test',
  97. ConfigSetCommand::ARG_VALUE => 'value'
  98. ]);
  99. $this->assertContains(
  100. __('Some message')->render(),
  101. $tester->getDisplay()
  102. );
  103. $this->assertSame(Cli::RETURN_SUCCESS, $tester->getStatusCode());
  104. }
  105. public function testExecuteMagentoUninstalled()
  106. {
  107. $this->deploymentConfigMock->expects($this->once())
  108. ->method('isAvailable')
  109. ->willReturn(false);
  110. $this->emulatedAreProcessorMock->expects($this->never())
  111. ->method('process');
  112. $tester = new CommandTester($this->command);
  113. $tester->execute([
  114. ConfigSetCommand::ARG_PATH => 'test/test/test',
  115. ConfigSetCommand::ARG_VALUE => 'value'
  116. ]);
  117. $this->assertContains(
  118. __('You cannot run this command because the Magento application is not installed.')->render(),
  119. $tester->getDisplay()
  120. );
  121. $this->assertSame(Cli::RETURN_FAILURE, $tester->getStatusCode());
  122. }
  123. public function testExecuteNeedsRegeneration()
  124. {
  125. $this->deploymentConfigMock->expects($this->once())
  126. ->method('isAvailable')
  127. ->willReturn(true);
  128. $this->changeDetectorMock->expects($this->once())
  129. ->method('hasChanges')
  130. ->willReturn(true);
  131. $this->emulatedAreProcessorMock->expects($this->never())
  132. ->method('process');
  133. $tester = new CommandTester($this->command);
  134. $tester->execute([
  135. ConfigSetCommand::ARG_PATH => 'test/test/test',
  136. ConfigSetCommand::ARG_VALUE => 'value'
  137. ]);
  138. $this->assertContains(
  139. __('This command is unavailable right now.')->render(),
  140. $tester->getDisplay()
  141. );
  142. $this->assertSame(Cli::RETURN_FAILURE, $tester->getStatusCode());
  143. }
  144. public function testExecuteWithException()
  145. {
  146. $this->deploymentConfigMock->expects($this->once())
  147. ->method('isAvailable')
  148. ->willReturn(true);
  149. $this->changeDetectorMock->expects($this->once())
  150. ->method('hasChanges')
  151. ->willReturn(false);
  152. $this->emulatedAreProcessorMock->expects($this->once())
  153. ->method('process')
  154. ->willThrowException(new ValidatorException(__('The "test/test/test" path does not exists')));
  155. $tester = new CommandTester($this->command);
  156. $tester->execute([
  157. ConfigSetCommand::ARG_PATH => 'test/test/test',
  158. ConfigSetCommand::ARG_VALUE => 'value'
  159. ]);
  160. $this->assertContains(
  161. __('The "test/test/test" path does not exists')->render(),
  162. $tester->getDisplay()
  163. );
  164. $this->assertSame(Cli::RETURN_FAILURE, $tester->getStatusCode());
  165. }
  166. }