diff options
| author | Greg Roach <greg@subaqua.co.uk> | 2025-06-16 23:36:29 +0100 |
|---|---|---|
| committer | Greg Roach <greg@subaqua.co.uk> | 2025-06-24 17:08:41 +0100 |
| commit | 42c8c31daf4b42a02cd3be82cab55dea15379874 (patch) | |
| tree | d34cb8ef2b84db4f323ad0d4dd49a0ae6c1d09ca /tests/app/Services | |
| parent | c48495e60c191f2179eaded08df4bfd8d69d0af1 (diff) | |
| download | webtrees-42c8c31daf4b42a02cd3be82cab55dea15379874.tar.gz webtrees-42c8c31daf4b42a02cd3be82cab55dea15379874.tar.bz2 webtrees-42c8c31daf4b42a02cd3be82cab55dea15379874.zip | |
Create new service for handling maintenance mode
Diffstat (limited to 'tests/app/Services')
| -rw-r--r-- | tests/app/Services/MaintenanceModeServiceTest.php | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/tests/app/Services/MaintenanceModeServiceTest.php b/tests/app/Services/MaintenanceModeServiceTest.php new file mode 100644 index 0000000000..6e11385e59 --- /dev/null +++ b/tests/app/Services/MaintenanceModeServiceTest.php @@ -0,0 +1,115 @@ +<?php + +/** + * webtrees: online genealogy + * Copyright (C) 2025 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +declare(strict_types=1); + +namespace Fisharebest\Webtrees\Services; + +use Fisharebest\Webtrees\TestCase; +use InvalidArgumentException; +use PHPUnit\Framework\Attributes\CoversClass; + +use function file_put_contents; + +#[CoversClass(MaintenanceModeService::class)] +class MaintenanceModeServiceTest extends TestCase +{ + private const string TEST_DATA_DIR = __DIR__ . '/../../data/'; + + public function testInvalidFolder(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage(__DIR__ . '/non-existent does not exist'); + + new MaintenanceModeService(__DIR__ . '/non-existent'); + } + + public function testOfflineAndOnline(): void + { + $service = new MaintenanceModeService(self::TEST_DATA_DIR); + + $this->assertFalse($service->isOffline()); + + $service->offline(); + + $this->assertTrue($service->isOffline()); + + $service->online(); + + $this->assertFalse($service->isOffline()); + } + + public function testMessage(): void + { + $service = new MaintenanceModeService(self::TEST_DATA_DIR); + + $service->offline('foo bar'); + + $this->assertSame('foo bar', $service->message()); + + $service->online(); + } + + public function testOfflineFileIsAFolder(): void + { + $service = new MaintenanceModeService(self::TEST_DATA_DIR); + + mkdir(self::TEST_DATA_DIR . 'offline.txt', 0777, true); + + $this->assertTrue($service->isOffline()); + $this->assertSame('', $service->message()); + + $service->online(); + + $this->assertFalse($service->isOffline()); + } + + public function testOfflineFileIsUnreadable(): void + { + $service = new MaintenanceModeService(self::TEST_DATA_DIR); + + file_put_contents(self::TEST_DATA_DIR . 'offline.txt', 'foo'); + chmod(self::TEST_DATA_DIR . 'offline.txt', 0); + + $this->assertTrue($service->isOffline()); + $this->assertSame('', $service->message()); + + $service->online(); + + $this->assertFalse($service->isOffline()); + } + + public function testOfflineFileIsSymbolicLink(): void + { + $service = new MaintenanceModeService(self::TEST_DATA_DIR); + + file_put_contents(self::TEST_DATA_DIR . 'foo', 'foo'); + symlink(self::TEST_DATA_DIR . 'foo', self::TEST_DATA_DIR . 'offline.txt'); + + $this->assertTrue($service->isOffline()); + $this->assertSame('foo', $service->message()); + + unlink(self::TEST_DATA_DIR . 'foo'); + + $this->assertTrue($service->isOffline()); + $this->assertSame('', $service->message()); + + $service->online(); + + $this->assertFalse($service->isOffline()); + } +} |
