Skip to content

Commit

Permalink
test: added multiple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SRWieZ committed Dec 9, 2024
1 parent d3fabf7 commit 82faeff
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 8 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/NoProviderSpecified.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace KnotsPHP\PublicIP\Exceptions;

class NoProviderSpecified extends Exception {}
8 changes: 8 additions & 0 deletions src/Fetchers/CurlFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 CurlFetcher implements FetcherContract
Expand All @@ -15,8 +16,15 @@ class CurlFetcher 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) {
Expand Down
8 changes: 8 additions & 0 deletions src/Fetchers/DigFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/Fetchers/DnsGetRecordFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion src/Fetchers/FileGetContentsFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/CommandLineToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions tests/Feature/DefaultUsageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use KnotsPHP\PublicIP\Finders\PublicIP;
use KnotsPHP\PublicIP\Finders\PublicIPv4;
use KnotsPHP\PublicIP\Finders\PublicIPv6;

it('returns an ip address', function () {
$ip = PublicIP::get();

$filtered = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6);

expect($filtered)->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.'
);
5 changes: 0 additions & 5 deletions tests/Feature/ExampleTest.php

This file was deleted.

28 changes: 28 additions & 0 deletions tests/Feature/Fetchers/CurlFetcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use KnotsPHP\PublicIP\Exceptions\NoProviderSpecified;
use KnotsPHP\PublicIP\Fetchers\CurlFetcher;

it('can fetch an ip address', function () {
$ip = (new CurlFetcher)
->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.'
);
28 changes: 28 additions & 0 deletions tests/Feature/Fetchers/DigFetcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use KnotsPHP\PublicIP\Exceptions\NoProviderSpecified;
use KnotsPHP\PublicIP\Fetchers\DigFetcher;

it('can fetch an ip address', function () {
$ip = (new DigFetcher)
->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.'
);
19 changes: 19 additions & 0 deletions tests/Feature/Fetchers/FileGetContentsFetcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use KnotsPHP\PublicIP\Exceptions\NoProviderSpecified;
use KnotsPHP\PublicIP\Fetchers\FileGetContentsFetcher;

it('can fetch an ip address', function () {
$ip = (new FileGetContentsFetcher)
->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);
20 changes: 20 additions & 0 deletions tests/Feature/Providers/DnsProvidersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use KnotsPHP\PublicIP\Enums\DnsProvider;
use KnotsPHP\PublicIP\Fetchers\DigFetcher;

test('every dns provider', function (DnsProvider $dnsProvider) {

$ip = (new DigFetcher)
->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.'
);
19 changes: 19 additions & 0 deletions tests/Feature/Providers/HttpProvidersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use KnotsPHP\PublicIP\Enums\HttpProvider;
use KnotsPHP\PublicIP\Fetchers\CurlFetcher;

test('every http provider', function (HttpProvider $httpProvider) {

$ip = (new CurlFetcher)
->addProvider($httpProvider)
->fetch();

expect($ip)->not->toBeNull();

})
->with(HttpProvider::cases())
->skip(
conditionOrMessage: ! CurlFetcher::isSupported(),
message: 'curl is not installed on this system.'
);

0 comments on commit 82faeff

Please sign in to comment.