FileTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\File;
  23. use OCP\IConfig;
  24. use Symfony\Component\Console\Input\InputInterface;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Test\TestCase;
  27. class FileTest 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 File($config);
  44. }
  45. public function testEnable() {
  46. $this->consoleInput->method('getOption')
  47. ->will($this->returnValueMap([
  48. ['enable', 'true']
  49. ]));
  50. $this->config->expects($this->once())
  51. ->method('setSystemValue')
  52. ->with('log_type', 'file');
  53. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  54. }
  55. public function testChangeFile() {
  56. $this->consoleInput->method('getOption')
  57. ->will($this->returnValueMap([
  58. ['file', '/foo/bar/file.log']
  59. ]));
  60. $this->config->expects($this->once())
  61. ->method('setSystemValue')
  62. ->with('logfile', '/foo/bar/file.log');
  63. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  64. }
  65. public function changeRotateSizeProvider() {
  66. return [
  67. ['42', 42],
  68. ['0', 0],
  69. ['1 kB', 1024],
  70. ['5MB', 5 * 1024 * 1024],
  71. ];
  72. }
  73. /**
  74. * @dataProvider changeRotateSizeProvider
  75. */
  76. public function testChangeRotateSize($optionValue, $configValue) {
  77. $this->consoleInput->method('getOption')
  78. ->will($this->returnValueMap([
  79. ['rotate-size', $optionValue]
  80. ]));
  81. $this->config->expects($this->once())
  82. ->method('setSystemValue')
  83. ->with('log_rotate_size', $configValue);
  84. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  85. }
  86. public function testGetConfiguration() {
  87. $this->config->method('getSystemValue')
  88. ->will($this->returnValueMap([
  89. ['log_type', 'file', 'log_type_value'],
  90. ['datadirectory', \OC::$SERVERROOT.'/data', '/data/directory/'],
  91. ['logfile', '/data/directory/nextcloud.log', '/var/log/nextcloud.log'],
  92. ['log_rotate_size', 100 * 1024 * 1024, 5 * 1024 * 1024],
  93. ]));
  94. $this->consoleOutput->expects($this->at(0))
  95. ->method('writeln')
  96. ->with('Log backend file: disabled');
  97. $this->consoleOutput->expects($this->at(1))
  98. ->method('writeln')
  99. ->with('Log file: /var/log/nextcloud.log');
  100. $this->consoleOutput->expects($this->at(2))
  101. ->method('writeln')
  102. ->with('Rotate at: 5 MB');
  103. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  104. }
  105. }