LogFactoryTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Log;
  24. use OC\Log\Errorlog;
  25. use OC\Log\File;
  26. use OC\Log\LogFactory;
  27. use OC\Log\Syslog;
  28. use OC\SystemConfig;
  29. use OCP\IConfig;
  30. use OCP\IServerContainer;
  31. use Test\TestCase;
  32. /**
  33. * Class LogFactoryTest
  34. *
  35. * @package Test\Log
  36. */
  37. class LogFactoryTest extends TestCase {
  38. /** @var IServerContainer|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $c;
  40. /** @var LogFactory */
  41. protected $factory;
  42. /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
  43. protected $systemConfig;
  44. protected function setUp() {
  45. parent::setUp();
  46. $this->c = $this->createMock(IServerContainer::class);
  47. $this->systemConfig = $this->createMock(SystemConfig::class);
  48. $this->factory = new LogFactory($this->c, $this->systemConfig);
  49. }
  50. public function fileTypeProvider(): array {
  51. return [
  52. [
  53. 'file'
  54. ],
  55. [
  56. 'nextcloud'
  57. ],
  58. [
  59. 'owncloud'
  60. ],
  61. [
  62. 'krzxkyr_default'
  63. ]
  64. ];
  65. }
  66. /**
  67. * @param string $type
  68. * @dataProvider fileTypeProvider
  69. * @throws \OCP\AppFramework\QueryException
  70. */
  71. public function testFile(string $type) {
  72. $datadir = \OC::$SERVERROOT.'/data';
  73. $defaultLog = $datadir . '/nextcloud.log';
  74. $this->systemConfig->expects($this->exactly(2))
  75. ->method('getValue')
  76. ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog])
  77. ->willReturnOnConsecutiveCalls($datadir, $defaultLog);
  78. $log = $this->factory->get($type);
  79. $this->assertInstanceOf(File::class, $log);
  80. }
  81. public function logFilePathProvider():array {
  82. return [
  83. [
  84. '/dev/null',
  85. '/dev/null'
  86. ],
  87. [
  88. '/xdev/youshallfallback',
  89. \OC::$SERVERROOT.'/data/nextcloud.log'
  90. ]
  91. ];
  92. }
  93. /**
  94. * @dataProvider logFilePathProvider
  95. * @throws \OCP\AppFramework\QueryException
  96. */
  97. public function testFileCustomPath($path, $expected) {
  98. $datadir = \OC::$SERVERROOT.'/data';
  99. $defaultLog = $datadir . '/nextcloud.log';
  100. $this->systemConfig->expects($this->exactly(2))
  101. ->method('getValue')
  102. ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog])
  103. ->willReturnOnConsecutiveCalls($datadir, $path);
  104. $log = $this->factory->get('file');
  105. $this->assertInstanceOf(File::class, $log);
  106. $this->assertSame($expected, $log->getLogFilePath());
  107. }
  108. /**
  109. * @throws \OCP\AppFramework\QueryException
  110. */
  111. public function testErrorLog() {
  112. $log = $this->factory->get('errorlog');
  113. $this->assertInstanceOf(Errorlog::class, $log);
  114. }
  115. /**
  116. * @throws \OCP\AppFramework\QueryException
  117. */
  118. public function testSystemLog() {
  119. $this->c->expects($this->once())
  120. ->method('resolve')
  121. ->with(Syslog::class)
  122. ->willReturn($this->createMock(Syslog::class));
  123. $log = $this->factory->get('syslog');
  124. $this->assertInstanceOf(Syslog::class, $log);
  125. }
  126. }