From 82faeff310cf4380015307ff0d58d2436da17fab Mon Sep 17 00:00:00 2001 From: Eser DENIZ Date: Mon, 9 Dec 2024 17:27:45 +0100 Subject: [PATCH] test: added multiple tests --- README.md | 1 - src/Exceptions/NoProviderSpecified.php | 5 +++ src/Fetchers/CurlFetcher.php | 8 +++++ src/Fetchers/DigFetcher.php | 8 +++++ src/Fetchers/DnsGetRecordFetcher.php | 9 ++++++ src/Fetchers/FileGetContentsFetcher.php | 10 +++++- tests/Feature/CommandLineToolTest.php | 1 - tests/Feature/DefaultUsageTest.php | 32 +++++++++++++++++++ tests/Feature/ExampleTest.php | 5 --- tests/Feature/Fetchers/CurlFetcherTest.php | 28 ++++++++++++++++ tests/Feature/Fetchers/DigFetcherTest.php | 28 ++++++++++++++++ .../Fetchers/FileGetContentsFetcherTest.php | 19 +++++++++++ tests/Feature/Providers/DnsProvidersTest.php | 20 ++++++++++++ tests/Feature/Providers/HttpProvidersTest.php | 19 +++++++++++ 14 files changed, 185 insertions(+), 8 deletions(-) create mode 100644 src/Exceptions/NoProviderSpecified.php create mode 100644 tests/Feature/DefaultUsageTest.php delete mode 100644 tests/Feature/ExampleTest.php create mode 100644 tests/Feature/Fetchers/CurlFetcherTest.php create mode 100644 tests/Feature/Fetchers/DigFetcherTest.php create mode 100644 tests/Feature/Fetchers/FileGetContentsFetcherTest.php create mode 100644 tests/Feature/Providers/DnsProvidersTest.php create mode 100644 tests/Feature/Providers/HttpProvidersTest.php diff --git a/README.md b/README.md index c15327e..88bdbe7 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,6 @@ HTTP whoami provider list ## 📋 TODO Contributions are welcome! -- Write tests - use Symfony ExecutableFinder to find dig - use Symfony Process to run dig - PSR-18 HTTP fetcher with a way to choose psr compatible client so other tools can monitor outgoing requests diff --git a/src/Exceptions/NoProviderSpecified.php b/src/Exceptions/NoProviderSpecified.php new file mode 100644 index 0000000..d74c7db --- /dev/null +++ b/src/Exceptions/NoProviderSpecified.php @@ -0,0 +1,5 @@ +providers)) { + throw new NoProviderSpecified; + } + foreach ($this->providers as $provider) { $ip = $this->fetchFrom($provider, $versionToResolve); if ($ip) { diff --git a/src/Fetchers/DigFetcher.php b/src/Fetchers/DigFetcher.php index 9ae717d..d80deca 100644 --- a/src/Fetchers/DigFetcher.php +++ b/src/Fetchers/DigFetcher.php @@ -5,6 +5,7 @@ use KnotsPHP\PublicIP\Contracts\FetcherContract; use KnotsPHP\PublicIP\Enums\DnsProvider; use KnotsPHP\PublicIP\Enums\IpVersion; +use KnotsPHP\PublicIP\Exceptions\NoProviderSpecified; use KnotsPHP\PublicIP\Traits\HasDnsProviders; class DigFetcher implements FetcherContract @@ -15,8 +16,15 @@ class DigFetcher implements FetcherContract protected int $timeout = 1; + /** + * @throws NoProviderSpecified + */ public function fetch(?IpVersion $versionToResolve = null): ?string { + if (empty($this->providers)) { + throw new NoProviderSpecified; + } + foreach ($this->providers as $provider) { $ip = $this->fetchFrom($provider, $versionToResolve); if ($ip) { diff --git a/src/Fetchers/DnsGetRecordFetcher.php b/src/Fetchers/DnsGetRecordFetcher.php index 2c08732..4348e12 100644 --- a/src/Fetchers/DnsGetRecordFetcher.php +++ b/src/Fetchers/DnsGetRecordFetcher.php @@ -6,6 +6,7 @@ use KnotsPHP\PublicIP\Enums\DnsProvider; use KnotsPHP\PublicIP\Enums\IpVersion; use KnotsPHP\PublicIP\Exceptions\Exception; +use KnotsPHP\PublicIP\Exceptions\NoProviderSpecified; use KnotsPHP\PublicIP\Traits\HasDnsProviders; /** @@ -37,8 +38,16 @@ public function onlyIPv6(): self return $this; } + /** + * @throws Exception + * @throws NoProviderSpecified + */ public function fetch(?IpVersion $versionToResolve = null): ?string { + if (empty($this->providers)) { + throw new NoProviderSpecified; + } + foreach ($this->providers as $provider) { $ip = $this->fetchFrom($provider, $versionToResolve); if ($ip) { diff --git a/src/Fetchers/FileGetContentsFetcher.php b/src/Fetchers/FileGetContentsFetcher.php index e33a4d5..7374332 100644 --- a/src/Fetchers/FileGetContentsFetcher.php +++ b/src/Fetchers/FileGetContentsFetcher.php @@ -5,6 +5,7 @@ use KnotsPHP\PublicIP\Contracts\FetcherContract; use KnotsPHP\PublicIP\Enums\HttpProvider; use KnotsPHP\PublicIP\Enums\IpVersion; +use KnotsPHP\PublicIP\Exceptions\NoProviderSpecified; use KnotsPHP\PublicIP\Traits\HasHttpProviders; class FileGetContentsFetcher implements FetcherContract @@ -13,10 +14,17 @@ class FileGetContentsFetcher implements FetcherContract public static bool $forceHTTP = false; - protected int $timeout; + protected int $timeout = 1; + /** + * @throws NoProviderSpecified + */ public function fetch(?IpVersion $versionToResolve = null): ?string { + if (empty($this->providers)) { + throw new NoProviderSpecified; + } + foreach ($this->providers as $provider) { $ip = $this->fetchFrom($provider, $versionToResolve); if ($ip) { diff --git a/tests/Feature/CommandLineToolTest.php b/tests/Feature/CommandLineToolTest.php index 817ec83..00fd5a5 100644 --- a/tests/Feature/CommandLineToolTest.php +++ b/tests/Feature/CommandLineToolTest.php @@ -3,7 +3,6 @@ it('returns an ip', function () { exec('php cli/publicip.php', $output, $result_code); - dump(trim($output[0])); $filtered = filter_var(trim($output[0]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6); expect($result_code)->toBe(0) diff --git a/tests/Feature/DefaultUsageTest.php b/tests/Feature/DefaultUsageTest.php new file mode 100644 index 0000000..1a8fc22 --- /dev/null +++ b/tests/Feature/DefaultUsageTest.php @@ -0,0 +1,32 @@ +toBeScalar(); +}); + +it('returns an ipv4 address', function () { + $ip = PublicIPv4::get(); + + $filtered = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); + + expect($filtered)->toBeString(); +}); + +it('returns an ipv6 address', function () { + $ip = PublicIPv6::get(); + + $filtered = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); + + expect($filtered)->toBeString(); +})->skip( + conditionOrMessage: fn () => getenv('CI') === 'true', + message: 'This test is skipped because the CI environment does not have an IPv6 address.' +); diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 61cd84c..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,5 +0,0 @@ -toBeTrue(); -}); diff --git a/tests/Feature/Fetchers/CurlFetcherTest.php b/tests/Feature/Fetchers/CurlFetcherTest.php new file mode 100644 index 0000000..914414b --- /dev/null +++ b/tests/Feature/Fetchers/CurlFetcherTest.php @@ -0,0 +1,28 @@ +all() + ->fetch(); + + $filtered = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6); + + expect($filtered)->toBeString(); +}) + ->skip( + conditionOrMessage: ! CurlFetcher::isSupported(), + message: 'curl is not installed on this system.' + ); + +it('throws an exception if no providers are set', function () { + (new CurlFetcher) + ->fetch(); +}) + ->throws(NoProviderSpecified::class) + ->skip( + conditionOrMessage: ! CurlFetcher::isSupported(), + message: 'curl is not installed on this system.' + ); diff --git a/tests/Feature/Fetchers/DigFetcherTest.php b/tests/Feature/Fetchers/DigFetcherTest.php new file mode 100644 index 0000000..7c07998 --- /dev/null +++ b/tests/Feature/Fetchers/DigFetcherTest.php @@ -0,0 +1,28 @@ +all() + ->fetch(); + + $filtered = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6); + + expect($filtered)->toBeString(); +}) + ->skip( + conditionOrMessage: ! DigFetcher::isSupported(), + message: 'curl is not installed on this system.' + ); + +it('throws an exception if no providers are set', function () { + (new DigFetcher) + ->fetch(); +}) + ->throws(NoProviderSpecified::class) + ->skip( + conditionOrMessage: ! DigFetcher::isSupported(), + message: 'curl is not installed on this system.' + ); diff --git a/tests/Feature/Fetchers/FileGetContentsFetcherTest.php b/tests/Feature/Fetchers/FileGetContentsFetcherTest.php new file mode 100644 index 0000000..3d6c411 --- /dev/null +++ b/tests/Feature/Fetchers/FileGetContentsFetcherTest.php @@ -0,0 +1,19 @@ +all() + ->fetch(); + + $filtered = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6); + + expect($filtered)->toBeString(); +}); + +it('throws an exception if no providers are set', function () { + (new FileGetContentsFetcher) + ->fetch(); +})->throws(NoProviderSpecified::class); diff --git a/tests/Feature/Providers/DnsProvidersTest.php b/tests/Feature/Providers/DnsProvidersTest.php new file mode 100644 index 0000000..f25026d --- /dev/null +++ b/tests/Feature/Providers/DnsProvidersTest.php @@ -0,0 +1,20 @@ +addProvider($dnsProvider) + ->fetch(); + + expect($ip)->not->toBeNull(); + +}) + ->with(DnsProvider::cases()) + // ->skip('this test is not important for now') + ->skip( + conditionOrMessage: ! DigFetcher::isSupported(), + message: 'dig is not installed on this system.' + ); diff --git a/tests/Feature/Providers/HttpProvidersTest.php b/tests/Feature/Providers/HttpProvidersTest.php new file mode 100644 index 0000000..911a791 --- /dev/null +++ b/tests/Feature/Providers/HttpProvidersTest.php @@ -0,0 +1,19 @@ +addProvider($httpProvider) + ->fetch(); + + expect($ip)->not->toBeNull(); + +}) + ->with(HttpProvider::cases()) + ->skip( + conditionOrMessage: ! CurlFetcher::isSupported(), + message: 'curl is not installed on this system.' + );