getconfigtest.php 5.2 KB

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