From 7f8e087f70b983ca0d5c4b671cdd33d0cb01536b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 24 Oct 2023 12:17:32 +0200 Subject: [PATCH] Add unit test for the setup check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- tests/Unit/SetupChecks/LogErrorsTest.php | 85 ++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/Unit/SetupChecks/LogErrorsTest.php diff --git a/tests/Unit/SetupChecks/LogErrorsTest.php b/tests/Unit/SetupChecks/LogErrorsTest.php new file mode 100644 index 000000000..a4e0bf6d1 --- /dev/null +++ b/tests/Unit/SetupChecks/LogErrorsTest.php @@ -0,0 +1,85 @@ + + * + * @author Côme Chilliet + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\LogReader\Tests\Unit\SetupChecks; + +use OCA\LogReader\Log\LogIteratorFactory; +use OCA\LogReader\SetupChecks\LogErrors; +use OCP\IConfig; +use OCP\IDateTimeFormatter; +use OCP\IL10N; +use Test\TestCase; + +class LogErrorsTest extends TestCase { + private IL10N $l10n; + private IConfig $config; + private IDateTimeFormatter $dateFormatter; + private LogIteratorFactory $logIteratorFactory; + private LogErrors $logErrorsCheck; + + protected function setUp(): void { + parent::setUp(); + + $this->l10n = $this->createMock(IL10N::class); + $this->config = $this->createMock(IConfig::class); + $this->dateFormatter = $this->createMock(IDateTimeFormatter::class); + $this->logIteratorFactory = $this->createMock(LogIteratorFactory::class); + $this->logErrorsCheck = new LogErrors( + $this->l10n, + $this->config, + $this->dateFormatter, + $this->logIteratorFactory, + ); + } + + public function logProvider(): array { + $now = (new \DateTime())->format(\DateTime::ATOM); + $tooOld = (new \DateTime('1 month ago'))->format(\DateTime::ATOM); + return [ + [[], 'success'], + [[['level' => 2, 'time' => $now]], 'warning'], + [[['level' => 3, 'time' => $now]], 'error'], + [[['level' => 2, 'time' => $now],['level' => 3, 'time' => $now]], 'error'], + [[['level' => 2, 'time' => $now],['level' => 3, 'time' => $tooOld]], 'warning'], + [[['level' => 2, 'time' => $tooOld],['level' => 3, 'time' => $tooOld]], 'success'], + ]; + } + + /** + * @dataProvider logProvider + */ + public function testSetupCheck(array $logContent, string $severity): void { + $logIterator = new \ArrayIterator($logContent); + $this->logIteratorFactory + ->expects(self::once()) + ->method('getLogIterator') + ->willReturn($logIterator); + + $result = $this->logErrorsCheck->run(); + + $this->assertEquals($severity, $result->getSeverity()); + } +}