GetConfigTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\App;
  22. use OC\Core\Command\Config\App\GetConfig;
  23. use OCP\IConfig;
  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 $config;
  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. $config = $this->config = $this->getMockBuilder(IConfig::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  42. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  43. /** @var \OCP\IConfig $config */
  44. $this->command = new GetConfig($config);
  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. ];
  81. }
  82. /**
  83. * @dataProvider getData
  84. *
  85. * @param string $configName
  86. * @param mixed $value
  87. * @param bool $configExists
  88. * @param mixed $defaultValue
  89. * @param bool $hasDefault
  90. * @param string $outputFormat
  91. * @param int $expectedReturn
  92. * @param string $expectedMessage
  93. */
  94. public function testGet($configName, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage) {
  95. $this->config->expects($this->atLeastOnce())
  96. ->method('getAppKeys')
  97. ->with('app-name')
  98. ->willReturn($configExists ? [$configName] : []);
  99. if (!$expectedReturn) {
  100. if ($configExists) {
  101. $this->config->expects($this->once())
  102. ->method('getAppValue')
  103. ->with('app-name', $configName)
  104. ->willReturn($value);
  105. }
  106. }
  107. $this->consoleInput->expects($this->exactly(2))
  108. ->method('getArgument')
  109. ->willReturnMap([
  110. ['app', 'app-name'],
  111. ['name', $configName],
  112. ]);
  113. $this->consoleInput->expects($this->any())
  114. ->method('getOption')
  115. ->willReturnMap([
  116. ['default-value', $defaultValue],
  117. ['output', $outputFormat],
  118. ]);
  119. $this->consoleInput->expects($this->any())
  120. ->method('hasParameterOption')
  121. ->willReturnMap([
  122. ['--output', false, true],
  123. ['--default-value', false, $hasDefault],
  124. ]);
  125. if ($expectedMessage !== null) {
  126. global $output;
  127. $output = '';
  128. $this->consoleOutput->expects($this->any())
  129. ->method('writeln')
  130. ->willReturnCallback(function ($value) {
  131. global $output;
  132. $output .= $value . "\n";
  133. return $output;
  134. });
  135. }
  136. $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  137. if ($expectedMessage !== null) {
  138. global $output;
  139. // Remove the trailing newline
  140. $this->assertSame($expectedMessage, substr($output, 0, -1));
  141. }
  142. }
  143. }