Skip to content

Commit

Permalink
fix(test-src): adjust tests and locator for using safe lib
Browse files Browse the repository at this point in the history
  • Loading branch information
mallardduck committed Dec 2, 2022
1 parent 013b36d commit b7c37f8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
9 changes: 9 additions & 0 deletions src/ServerLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Throwable;

use function Safe\file_get_contents;
use function Safe\realpath;
use function json_decode;
use function sprintf;
use function strtolower;

use const JSON_THROW_ON_ERROR;
Expand Down Expand Up @@ -40,6 +42,13 @@ abstract public function getServerListPath(): string;
*/
public function __construct()
{
try {
realpath($this->getServerListPath());
} catch (FilesystemException $e) {
throw new FilesystemException(
sprintf('Cannot get source file from path: `%s`', $this->getServerListPath()),
);
}
$fileContents = file_get_contents($this->getServerListPath());

/**
Expand Down
27 changes: 15 additions & 12 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,42 @@
use MallardDuck\Test\WhoisDomainList\MockLocators\CorruptJsonFileTestLocator;
use MallardDuck\Test\WhoisDomainList\MockLocators\EmptyFileTestLocator;
use MallardDuck\Test\WhoisDomainList\MockLocators\InvalidFileTestLocator;
use MallardDuck\Test\WhoisDomainList\MockLocators\UrlPathTestLocator;
use MallardDuck\WhoisDomainList\Exceptions\MissingArgument;
use MallardDuck\WhoisDomainList\Exceptions\UnknownTopLevelDomain;
use MallardDuck\WhoisDomainList\IanaServerLocator;
use MallardDuck\WhoisDomainList\PslServerLocator;
use Safe\Exceptions\FilesystemException;

it('will throw an exception with URL provided as path', function () {
it('throws an exception with invalid path', function () {
if (!is_dir(PROJ_PARENT_TMP)) {
mkdir(PROJ_PARENT_TMP);
}
if (!is_file(PROJ_PARENT_TMP . '/empty.json')) {
copy(__DIR__ . '/stubs/empty.json', PROJ_PARENT_TMP . '/empty.json');
}

ini_set('open_basedir', dirname(__DIR__));
$this->expectException(JsonException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessageMatches(
'#Cannot get source file from path: ([a-zA-Z\-\:\/\\\\]+)[\/\\\\]tmp[\/\\\\]empty.json#',
$this->expectDeprecationMessageMatches(
'#Cannot get source file from path: `([a-zA-Z\-\:\/\\\\]+)[\/\\\\]tmp[\/\\\\]empty.json`#',
);

new InvalidFileTestLocator();
});
})->throws(FilesystemException::class);

it('throws an exception with URL provided as path', function () {
new UrlPathTestLocator();
})->throws(
FilesystemException::class,
'Cannot get source file from path: `https://api.plos.org/search?q=title:DNA`',
);

it('will throw an exception with empty file provided as path', function () {
$this->expectException(JsonException::class);
new EmptyFileTestLocator();
});
})->throws(JsonException::class);

it('will throw an exception with corrupt JSON file provided as path', function () {
$this->expectException(JsonException::class);
new CorruptJsonFileTestLocator();
});

})->throws(JsonException::class);

it('will throw an exception for empty input using IANA', function () {
$ianaLocator = new IanaServerLocator();
Expand Down
15 changes: 15 additions & 0 deletions tests/MockLocators/UrlPathTestLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace MallardDuck\Test\WhoisDomainList\MockLocators;

use MallardDuck\WhoisDomainList\ServerLocator;

class UrlPathTestLocator extends ServerLocator
{
public function getServerListPath(): string
{
return 'https://api.plos.org/search?q=title:DNA';
}
}
6 changes: 4 additions & 2 deletions tests/ServerListPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
expect($ianaLocator)
->toBeObject()
->toBeInstanceOf(IanaServerLocator::class)
->toHaveProperty('whoisServerListMetadata')
->toHaveProperty('whoisServerCollection');
expect(getProperty($ianaLocator, 'whoisServerCollection'))->toBeArray()->toHaveKey('_meta');
expect(getProperty($ianaLocator, 'whoisServerCollection'))->toBeArray()->toHaveKey('com');
});

it('can find the expected whois server list using PSL', function () use ($pslLocator) {
Expand All @@ -39,8 +40,9 @@
expect($pslLocator)
->toBeObject()
->toBeInstanceOf(PslServerLocator::class)
->toHaveProperty('whoisServerListMetadata')
->toHaveProperty('whoisServerCollection');
expect(getProperty($pslLocator, 'whoisServerCollection'))->toBeArray()->toHaveKey('_meta');
expect(getProperty($pslLocator, 'whoisServerCollection'))->toBeArray()->toHaveKey('co.uk');
});

it('can find the expected whois server list for IPv4', function () {
Expand Down

0 comments on commit b7c37f8

Please sign in to comment.