ManageTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @author Robin McCorkell <rmccorkell@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\Log;
  22. use OC\Core\Command\Log\Manage;
  23. use OCP\IConfig;
  24. use Symfony\Component\Console\Input\InputInterface;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Test\TestCase;
  27. class ManageTest 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() {
  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. $this->command = new Manage($config);
  44. }
  45. public function testChangeBackend() {
  46. $this->consoleInput->method('getOption')
  47. ->will($this->returnValueMap([
  48. ['backend', 'syslog']
  49. ]));
  50. $this->config->expects($this->once())
  51. ->method('setSystemValue')
  52. ->with('log_type', 'syslog');
  53. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  54. }
  55. public function testChangeLevel() {
  56. $this->consoleInput->method('getOption')
  57. ->will($this->returnValueMap([
  58. ['level', 'debug']
  59. ]));
  60. $this->config->expects($this->once())
  61. ->method('setSystemValue')
  62. ->with('loglevel', 0);
  63. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  64. }
  65. public function testChangeTimezone() {
  66. $this->consoleInput->method('getOption')
  67. ->will($this->returnValueMap([
  68. ['timezone', 'UTC']
  69. ]));
  70. $this->config->expects($this->once())
  71. ->method('setSystemValue')
  72. ->with('logtimezone', 'UTC');
  73. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  74. }
  75. /**
  76. * @expectedException \InvalidArgumentException
  77. */
  78. public function testValidateBackend() {
  79. self::invokePrivate($this->command, 'validateBackend', ['notabackend']);
  80. }
  81. /**
  82. * @expectedException \Exception
  83. */
  84. public function testValidateTimezone() {
  85. // this might need to be changed when humanity colonises Mars
  86. self::invokePrivate($this->command, 'validateTimezone', ['Mars/OlympusMons']);
  87. }
  88. public function convertLevelStringProvider() {
  89. return [
  90. ['dEbug', 0],
  91. ['inFO', 1],
  92. ['Warning', 2],
  93. ['wArn', 2],
  94. ['error', 3],
  95. ['eRr', 3],
  96. ];
  97. }
  98. /**
  99. * @dataProvider convertLevelStringProvider
  100. */
  101. public function testConvertLevelString($levelString, $expectedInt) {
  102. $this->assertEquals($expectedInt,
  103. self::invokePrivate($this->command, 'convertLevelString', [$levelString])
  104. );
  105. }
  106. /**
  107. * @expectedException \InvalidArgumentException
  108. */
  109. public function testConvertLevelStringInvalid() {
  110. self::invokePrivate($this->command, 'convertLevelString', ['abc']);
  111. }
  112. public function convertLevelNumberProvider() {
  113. return [
  114. [0, 'Debug'],
  115. [1, 'Info'],
  116. [2, 'Warning'],
  117. [3, 'Error'],
  118. ];
  119. }
  120. /**
  121. * @dataProvider convertLevelNumberProvider
  122. */
  123. public function testConvertLevelNumber($levelNum, $expectedString) {
  124. $this->assertEquals($expectedString,
  125. self::invokePrivate($this->command, 'convertLevelNumber', [$levelNum])
  126. );
  127. }
  128. /**
  129. * @expectedException \InvalidArgumentException
  130. */
  131. public function testConvertLevelNumberInvalid() {
  132. self::invokePrivate($this->command, 'convertLevelNumber', [11]);
  133. }
  134. public function testGetConfiguration() {
  135. $this->config->expects($this->at(0))
  136. ->method('getSystemValue')
  137. ->with('log_type', 'file')
  138. ->willReturn('log_type_value');
  139. $this->config->expects($this->at(1))
  140. ->method('getSystemValue')
  141. ->with('loglevel', 2)
  142. ->willReturn(0);
  143. $this->config->expects($this->at(2))
  144. ->method('getSystemValue')
  145. ->with('logtimezone', 'UTC')
  146. ->willReturn('logtimezone_value');
  147. $this->consoleOutput->expects($this->at(0))
  148. ->method('writeln')
  149. ->with('Enabled logging backend: log_type_value');
  150. $this->consoleOutput->expects($this->at(1))
  151. ->method('writeln')
  152. ->with('Log level: Debug (0)');
  153. $this->consoleOutput->expects($this->at(2))
  154. ->method('writeln')
  155. ->with('Log timezone: logtimezone_value');
  156. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  157. }
  158. }