Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

218 lines
5.9 KiB

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2020, Friendica
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  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
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Friendica\Test\src\Console;
  22. use Friendica\App;
  23. use Friendica\App\Mode;
  24. use Friendica\Console\Config;
  25. use Friendica\Core\Config\IConfig;
  26. use Mockery\MockInterface;
  27. class ConfigConsoleTest extends ConsoleTest
  28. {
  29. /**
  30. * @var App\Mode|MockInterface $appMode
  31. */
  32. private $appMode;
  33. /** @var IConfig|\Mockery\LegacyMockInterface|MockInterface */
  34. private $configMock;
  35. protected function setUp()
  36. {
  37. parent::setUp();
  38. \Mockery::getConfiguration()->setConstantsMap([
  39. Mode::class => [
  40. 'DBCONFIGAVAILABLE' => 0
  41. ]
  42. ]);
  43. $this->appMode = \Mockery::mock(App\Mode::class);
  44. $this->appMode->shouldReceive('has')
  45. ->andReturn(true);
  46. $this->configMock = \Mockery::mock(IConfig::class);
  47. }
  48. function testSetGetKeyValue()
  49. {
  50. $this->configMock
  51. ->shouldReceive('set')
  52. ->with('config', 'test', 'now')
  53. ->andReturn(true)
  54. ->once();
  55. $this->configMock
  56. ->shouldReceive('get')
  57. ->with('config', 'test')
  58. ->andReturn('now')
  59. ->twice();
  60. $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
  61. $console->setArgument(0, 'config');
  62. $console->setArgument(1, 'test');
  63. $console->setArgument(2, 'now');
  64. $txt = $this->dumpExecute($console);
  65. $this->assertEquals("config.test <= now\n", $txt);
  66. $this->configMock
  67. ->shouldReceive('get')
  68. ->with('config', 'test')
  69. ->andReturn('now')
  70. ->once();
  71. $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]);
  72. $console->setArgument(0, 'config');
  73. $console->setArgument(1, 'test');
  74. $txt = $this->dumpExecute($console);
  75. $this->assertEquals("config.test => now\n", $txt);
  76. $this->configMock
  77. ->shouldReceive('get')
  78. ->with('config', 'test')
  79. ->andReturn(null)
  80. ->once();
  81. $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
  82. $console->setArgument(0, 'config');
  83. $console->setArgument(1, 'test');
  84. $txt = $this->dumpExecute($console);
  85. $this->assertEquals("config.test => \n", $txt);
  86. }
  87. function testSetArrayValue() {
  88. $testArray = [1, 2, 3];
  89. $this->configMock
  90. ->shouldReceive('get')
  91. ->with('config', 'test')
  92. ->andReturn($testArray)
  93. ->once();
  94. $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
  95. $console->setArgument(0, 'config');
  96. $console->setArgument(1, 'test');
  97. $console->setArgument(2, 'now');
  98. $txt = $this->dumpExecute($console);
  99. $this->assertEquals("[Error] config.test is an array and can't be set using this command.\n", $txt);
  100. }
  101. function testTooManyArguments() {
  102. $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
  103. $console->setArgument(0, 'config');
  104. $console->setArgument(1, 'test');
  105. $console->setArgument(2, 'it');
  106. $console->setArgument(3, 'now');
  107. $txt = $this->dumpExecute($console);
  108. $assertion = '[Warning] Too many arguments';
  109. $firstline = substr($txt, 0, strlen($assertion));
  110. $this->assertEquals($assertion, $firstline);
  111. }
  112. function testVerbose() {
  113. $this->configMock
  114. ->shouldReceive('get')
  115. ->with('test', 'it')
  116. ->andReturn('now')
  117. ->once();
  118. $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
  119. $console->setArgument(0, 'test');
  120. $console->setArgument(1, 'it');
  121. $console->setOption('v', 1);
  122. $executable = $this->consoleArgv[0];
  123. $assertion = <<<CONF
  124. Executable: {$executable}
  125. Class: Friendica\Console\Config
  126. Arguments: array (
  127. 0 => 'test',
  128. 1 => 'it',
  129. )
  130. Options: array (
  131. 'v' => 1,
  132. )
  133. test.it => now
  134. CONF;
  135. $txt = $this->dumpExecute($console);
  136. $this->assertEquals($assertion, $txt);
  137. }
  138. function testUnableToSet() {
  139. $this->configMock
  140. ->shouldReceive('set')
  141. ->with('test', 'it', 'now')
  142. ->andReturn(false)
  143. ->once();
  144. $this->configMock
  145. ->shouldReceive('get')
  146. ->with('test', 'it')
  147. ->andReturn(NULL)
  148. ->once();
  149. $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]);
  150. $console->setArgument(0, 'test');
  151. $console->setArgument(1, 'it');
  152. $console->setArgument(2, 'now');
  153. $txt = $this->dumpExecute($console);
  154. $this->assertSame("Unable to set test.it\n", $txt);
  155. }
  156. public function testGetHelp()
  157. {
  158. // Usable to purposely fail if new commands are added without taking tests into account
  159. $theHelp = <<<HELP
  160. console config - Manage site configuration
  161. Synopsis
  162. bin/console config [-h|--help|-?] [-v]
  163. bin/console config <category> [-h|--help|-?] [-v]
  164. bin/console config <category> <key> [-h|--help|-?] [-v]
  165. bin/console config <category> <key> <value> [-h|--help|-?] [-v]
  166. Description
  167. bin/console config
  168. Lists all config values
  169. bin/console config <category>
  170. Lists all config values in the provided category
  171. bin/console config <category> <key>
  172. Shows the value of the provided key in the category
  173. bin/console config <category> <key> <value>
  174. Sets the value of the provided key in the category
  175. Notes:
  176. Setting config entries which are manually set in config/local.config.php may result in
  177. conflict between database settings and the manual startup settings.
  178. Options
  179. -h|--help|-? Show help information
  180. -v Show more debug information.
  181. HELP;
  182. $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]);
  183. $console->setOption('help', true);
  184. $txt = $this->dumpExecute($console);
  185. $this->assertEquals($txt, $theHelp);
  186. }
  187. }