GetConfigTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Tests\Core\Command\Config\App;
  8. use OC\AppConfig;
  9. use OC\Core\Command\Config\App\GetConfig;
  10. use OCP\Exceptions\AppConfigUnknownKeyException;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Test\TestCase;
  14. class GetConfigTest extends TestCase {
  15. /** @var \PHPUnit\Framework\MockObject\MockObject */
  16. protected $config;
  17. /** @var \PHPUnit\Framework\MockObject\MockObject */
  18. protected $consoleInput;
  19. /** @var \PHPUnit\Framework\MockObject\MockObject */
  20. protected $consoleOutput;
  21. /** @var \Symfony\Component\Console\Command\Command */
  22. protected $command;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $config = $this->config = $this->getMockBuilder(AppConfig::class)
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  29. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  30. /** @var \OCP\IAppConfig $config */
  31. $this->command = new GetConfig($config);
  32. }
  33. public function getData() {
  34. return [
  35. // String output as json
  36. ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')],
  37. // String output as plain text
  38. ['name', 'newvalue', true, null, false, 'plain', 0, 'newvalue'],
  39. // String falling back to default output as json
  40. ['name', null, false, 'newvalue', true, 'json', 0, json_encode('newvalue')],
  41. // String falling back without default: error
  42. ['name', null, false, null, false, 'json', 1, null],
  43. // Int "0" output as json/plain
  44. ['name', 0, true, null, false, 'json', 0, json_encode(0)],
  45. ['name', 0, true, null, false, 'plain', 0, '0'],
  46. // Int "1" output as json/plain
  47. ['name', 1, true, null, false, 'json', 0, json_encode(1)],
  48. ['name', 1, true, null, false, 'plain', 0, '1'],
  49. // Bool "true" output as json/plain
  50. ['name', true, true, null, false, 'json', 0, json_encode(true)],
  51. ['name', true, true, null, false, 'plain', 0, 'true'],
  52. // Bool "false" output as json/plain
  53. ['name', false, true, null, false, 'json', 0, json_encode(false)],
  54. ['name', false, true, null, false, 'plain', 0, 'false'],
  55. // Null output as json/plain
  56. ['name', null, true, null, false, 'json', 0, json_encode(null)],
  57. ['name', null, true, null, false, 'plain', 0, 'null'],
  58. // Array output as json/plain
  59. ['name', ['a', 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])],
  60. ['name', ['a', 'b'], true, null, false, 'plain', 0, "a\nb"],
  61. // Key array output as json/plain
  62. ['name', [0 => 'a', 1 => 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])],
  63. ['name', [0 => 'a', 1 => 'b'], true, null, false, 'plain', 0, "a\nb"],
  64. // Associative array output as json/plain
  65. ['name', ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(['a' => 1, 'b' => 2])],
  66. ['name', ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, "a: 1\nb: 2"],
  67. ];
  68. }
  69. /**
  70. * @dataProvider getData
  71. *
  72. * @param string $configName
  73. * @param mixed $value
  74. * @param bool $configExists
  75. * @param mixed $defaultValue
  76. * @param bool $hasDefault
  77. * @param string $outputFormat
  78. * @param int $expectedReturn
  79. * @param string $expectedMessage
  80. */
  81. public function testGet($configName, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage) {
  82. if (!$expectedReturn) {
  83. if ($configExists) {
  84. $this->config->expects($this->once())
  85. ->method('getDetails')
  86. ->with('app-name', $configName)
  87. ->willReturn(['value' => $value]);
  88. }
  89. }
  90. if (!$configExists) {
  91. $this->config->expects($this->once())
  92. ->method('getDetails')
  93. ->with('app-name', $configName)
  94. ->willThrowException(new AppConfigUnknownKeyException());
  95. }
  96. $this->consoleInput->expects($this->exactly(2))
  97. ->method('getArgument')
  98. ->willReturnMap([
  99. ['app', 'app-name'],
  100. ['name', $configName],
  101. ]);
  102. $this->consoleInput->expects($this->any())
  103. ->method('getOption')
  104. ->willReturnMap([
  105. ['default-value', $defaultValue],
  106. ['output', $outputFormat],
  107. ]);
  108. $this->consoleInput->expects($this->any())
  109. ->method('hasParameterOption')
  110. ->willReturnMap([
  111. ['--output', false, true],
  112. ['--default-value', false, $hasDefault],
  113. ]);
  114. if ($expectedMessage !== null) {
  115. global $output;
  116. $output = '';
  117. $this->consoleOutput->expects($this->any())
  118. ->method('writeln')
  119. ->willReturnCallback(function ($value) {
  120. global $output;
  121. $output .= $value . "\n";
  122. return $output;
  123. });
  124. }
  125. $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  126. if ($expectedMessage !== null) {
  127. global $output;
  128. // Remove the trailing newline
  129. $this->assertSame($expectedMessage, substr($output, 0, -1));
  130. }
  131. }
  132. }