GetConfigTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Core\Command\Config\System;
  22. use OC\Core\Command\Config\System\GetConfig;
  23. use OC\SystemConfig;
  24. use Symfony\Component\Console\Input\InputInterface;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Test\TestCase;
  27. class GetConfigTest extends TestCase {
  28. /** @var \PHPUnit\Framework\MockObject\MockObject */
  29. protected $systemConfig;
  30. /** @var \PHPUnit\Framework\MockObject\MockObject */
  31. protected $consoleInput;
  32. /** @var \PHPUnit\Framework\MockObject\MockObject */
  33. protected $consoleOutput;
  34. /** @var \Symfony\Component\Console\Command\Command */
  35. protected $command;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $systemConfig = $this->systemConfig = $this->getMockBuilder(SystemConfig::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  42. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  43. /** @var \OC\SystemConfig $systemConfig */
  44. $this->command = new GetConfig($systemConfig);
  45. }
  46. public function getData() {
  47. return [
  48. // String output as json
  49. ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')],
  50. // String output as plain text
  51. ['name', 'newvalue', true, null, false, 'plain', 0, 'newvalue'],
  52. // String falling back to default output as json
  53. ['name', null, false, 'newvalue', true, 'json', 0, json_encode('newvalue')],
  54. // String falling back without default: error
  55. ['name', null, false, null, false, 'json', 1, null],
  56. // Int "0" output as json/plain
  57. ['name', 0, true, null, false, 'json', 0, json_encode(0)],
  58. ['name', 0, true, null, false, 'plain', 0, '0'],
  59. // Int "1" output as json/plain
  60. ['name', 1, true, null, false, 'json', 0, json_encode(1)],
  61. ['name', 1, true, null, false, 'plain', 0, '1'],
  62. // Bool "true" output as json/plain
  63. ['name', true, true, null, false, 'json', 0, json_encode(true)],
  64. ['name', true, true, null, false, 'plain', 0, 'true'],
  65. // Bool "false" output as json/plain
  66. ['name', false, true, null, false, 'json', 0, json_encode(false)],
  67. ['name', false, true, null, false, 'plain', 0, 'false'],
  68. // Null output as json/plain
  69. ['name', null, true, null, false, 'json', 0, json_encode(null)],
  70. ['name', null, true, null, false, 'plain', 0, 'null'],
  71. // Array output as json/plain
  72. ['name', ['a', 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])],
  73. ['name', ['a', 'b'], true, null, false, 'plain', 0, "a\nb"],
  74. // Key array output as json/plain
  75. ['name', [0 => 'a', 1 => 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])],
  76. ['name', [0 => 'a', 1 => 'b'], true, null, false, 'plain', 0, "a\nb"],
  77. // Associative array output as json/plain
  78. ['name', ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(['a' => 1, 'b' => 2])],
  79. ['name', ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, "a: 1\nb: 2"],
  80. // Nested depth
  81. [['name', 'a'], ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(1)],
  82. [['name', 'a'], ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, '1'],
  83. [['name', 'c'], ['a' => 1, 'b' => 2], true, true, true, 'json', 0, json_encode(true)],
  84. [['name', 'c'], ['a' => 1, 'b' => 2], true, true, false, 'json', 1, null],
  85. ];
  86. }
  87. /**
  88. * @dataProvider getData
  89. *
  90. * @param string[] $configNames
  91. * @param mixed $value
  92. * @param bool $configExists
  93. * @param mixed $defaultValue
  94. * @param bool $hasDefault
  95. * @param string $outputFormat
  96. * @param int $expectedReturn
  97. * @param string $expectedMessage
  98. */
  99. public function testGet($configNames, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage) {
  100. if (is_array($configNames)) {
  101. $configName = $configNames[0];
  102. } else {
  103. $configName = $configNames;
  104. $configNames = [$configName];
  105. }
  106. $this->systemConfig->expects($this->atLeastOnce())
  107. ->method('getKeys')
  108. ->willReturn($configExists ? [$configName] : []);
  109. if (!$expectedReturn) {
  110. if ($configExists) {
  111. $this->systemConfig->expects($this->once())
  112. ->method('getValue')
  113. ->with($configName)
  114. ->willReturn($value);
  115. }
  116. }
  117. $this->consoleInput->expects($this->once())
  118. ->method('getArgument')
  119. ->with('name')
  120. ->willReturn($configNames);
  121. $this->consoleInput->expects($this->any())
  122. ->method('getOption')
  123. ->willReturnMap([
  124. ['default-value', $defaultValue],
  125. ['output', $outputFormat],
  126. ]);
  127. $this->consoleInput->expects($this->any())
  128. ->method('hasParameterOption')
  129. ->willReturnMap([
  130. ['--output', false, true],
  131. ['--default-value', false,$hasDefault],
  132. ]);
  133. if ($expectedMessage !== null) {
  134. global $output;
  135. $output = '';
  136. $this->consoleOutput->expects($this->any())
  137. ->method('writeln')
  138. ->willReturnCallback(function ($value) {
  139. global $output;
  140. $output .= $value . "\n";
  141. return $output;
  142. });
  143. }
  144. $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  145. if ($expectedMessage !== null) {
  146. global $output;
  147. // Remove the trailing newline
  148. $this->assertSame($expectedMessage, substr($output, 0, -1));
  149. }
  150. }
  151. }