GetConfigTest.php 5.3 KB

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