|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Tests\Unit\Service\Process; |
| 10 | + |
| 11 | +use OCA\Libresign\Service\Process\ListeningPidResolver; |
| 12 | +use OCA\Libresign\Tests\Unit\TestCase; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | + |
| 15 | +class ListeningPidResolverTest extends TestCase { |
| 16 | + #[DataProvider('provideInvalidPorts')] |
| 17 | + public function testFindListeningPidsRejectsInvalidPort(int $port): void { |
| 18 | + $resolver = new ListeningPidResolver(); |
| 19 | + |
| 20 | + $this->expectException(\RuntimeException::class); |
| 21 | + $this->expectExceptionMessage('Invalid port'); |
| 22 | + |
| 23 | + $resolver->findListeningPids($port); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @return array<string, array{0: int}> |
| 28 | + */ |
| 29 | + public static function provideInvalidPorts(): array { |
| 30 | + return [ |
| 31 | + 'zero' => [0], |
| 32 | + 'negative' => [-1], |
| 33 | + ]; |
| 34 | + } |
| 35 | + |
| 36 | + public function testFindListeningPidsMergesUniqueFromAvailableStrategies(): void { |
| 37 | + $resolver = new class() extends ListeningPidResolver { |
| 38 | + protected function findListeningPidsUsingSs(int $port): ?array { |
| 39 | + return [100, 101]; |
| 40 | + } |
| 41 | + |
| 42 | + protected function findListeningPidsUsingLsof(int $port): ?array { |
| 43 | + return [101, 102]; |
| 44 | + } |
| 45 | + |
| 46 | + protected function findListeningPidsUsingProc(int $port): ?array { |
| 47 | + return [102, 103]; |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + $actual = $resolver->findListeningPids(8888); |
| 52 | + sort($actual); |
| 53 | + |
| 54 | + $this->assertSame([100, 101, 102, 103], $actual); |
| 55 | + } |
| 56 | + |
| 57 | + public function testFindListeningPidsThrowsWhenNoStrategyIsAvailable(): void { |
| 58 | + $resolver = new class() extends ListeningPidResolver { |
| 59 | + protected function findListeningPidsUsingSs(int $port): ?array { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + protected function findListeningPidsUsingLsof(int $port): ?array { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + protected function findListeningPidsUsingProc(int $port): ?array { |
| 68 | + return null; |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + $this->expectException(\RuntimeException::class); |
| 73 | + $this->expectExceptionMessage('no strategy available'); |
| 74 | + |
| 75 | + $resolver->findListeningPids(8888); |
| 76 | + } |
| 77 | +} |
0 commit comments