FileTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Tests\Core\Command\Log;
  8. use OC\Core\Command\Log\File;
  9. use OCP\IConfig;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Test\TestCase;
  13. class FileTest extends TestCase {
  14. /** @var \PHPUnit\Framework\MockObject\MockObject */
  15. protected $config;
  16. /** @var \PHPUnit\Framework\MockObject\MockObject */
  17. protected $consoleInput;
  18. /** @var \PHPUnit\Framework\MockObject\MockObject */
  19. protected $consoleOutput;
  20. /** @var \Symfony\Component\Console\Command\Command */
  21. protected $command;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $config = $this->config = $this->getMockBuilder(IConfig::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  28. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  29. $this->command = new File($config);
  30. }
  31. public function testEnable(): void {
  32. $this->config->method('getSystemValue')->willReturnArgument(1);
  33. $this->consoleInput->method('getOption')
  34. ->willReturnMap([
  35. ['enable', 'true']
  36. ]);
  37. $this->config->expects($this->once())
  38. ->method('setSystemValue')
  39. ->with('log_type', 'file');
  40. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  41. }
  42. public function testChangeFile(): void {
  43. $this->config->method('getSystemValue')->willReturnArgument(1);
  44. $this->consoleInput->method('getOption')
  45. ->willReturnMap([
  46. ['file', '/foo/bar/file.log']
  47. ]);
  48. $this->config->expects($this->once())
  49. ->method('setSystemValue')
  50. ->with('logfile', '/foo/bar/file.log');
  51. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  52. }
  53. public function changeRotateSizeProvider() {
  54. return [
  55. ['42', 42],
  56. ['0', 0],
  57. ['1 kB', 1024],
  58. ['5MB', 5 * 1024 * 1024],
  59. ];
  60. }
  61. /**
  62. * @dataProvider changeRotateSizeProvider
  63. */
  64. public function testChangeRotateSize($optionValue, $configValue): void {
  65. $this->config->method('getSystemValue')->willReturnArgument(1);
  66. $this->consoleInput->method('getOption')
  67. ->willReturnMap([
  68. ['rotate-size', $optionValue]
  69. ]);
  70. $this->config->expects($this->once())
  71. ->method('setSystemValue')
  72. ->with('log_rotate_size', $configValue);
  73. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  74. }
  75. public function testGetConfiguration(): void {
  76. $this->config->method('getSystemValue')
  77. ->willReturnMap([
  78. ['log_type', 'file', 'log_type_value'],
  79. ['datadirectory', \OC::$SERVERROOT . '/data', '/data/directory/'],
  80. ['logfile', '/data/directory/nextcloud.log', '/var/log/nextcloud.log'],
  81. ['log_rotate_size', 100 * 1024 * 1024, 5 * 1024 * 1024],
  82. ]);
  83. $this->consoleOutput->expects($this->exactly(3))
  84. ->method('writeln')
  85. ->withConsecutive(
  86. ['Log backend file: disabled'],
  87. ['Log file: /var/log/nextcloud.log'],
  88. ['Rotate at: 5 MB'],
  89. );
  90. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  91. }
  92. }