FileTest.php 3.9 KB

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