Skip to content

Commit

Permalink
refactor: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mallardduck committed Jul 29, 2021
1 parent dfd2044 commit 38fcce3
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 11 deletions.
11 changes: 10 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@

<rule ref="Ramsey"/>

<!-- This test file specifically *needs* Windows line endings for testing purposes. -->
<!-- Excludes... -->
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint">
<exclude-pattern>generator/TopLevelDomain\.php</exclude-pattern>
</rule>
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>tests/Pest.php</exclude-pattern>
</rule>
<rule ref="Squiz.Functions.GlobalFunction.Found">
<exclude-pattern>tests/Pest.php</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint">
<exclude-pattern>tests/Pest.php</exclude-pattern>
</rule>

</ruleset>
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ parameters:
- ./src
- ./tests
excludePaths:
- tests/Pest.php
- tests/*Test.php
9 changes: 7 additions & 2 deletions src/ServerLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use MallardDuck\WhoisDomainList\Exceptions\UnknownTopLevelDomain;
use Throwable;

use function assert;
use function file_get_contents;
use function is_string;
use function json_decode;
use function strtolower;

Expand All @@ -31,10 +33,13 @@ abstract public function getServerListPath(): string;
*/
public function __construct()
{
$fileContents = file_get_contents($this->getServerListPath());
if ($fileContents === false) {
try {
$fileContents = file_get_contents($this->getServerListPath());
assert(is_string($fileContents));
} catch (Throwable $throwable) {
throw new JsonException('Cannot get source file from path: ' . $this->getServerListPath());
}

/**
* @var array<string, string> $parseResults
*/
Expand Down
45 changes: 39 additions & 6 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,66 @@

declare(strict_types=1);

use MallardDuck\Test\WhoisDomainList\MockLocators\CorruptJsonFileTestLocator;
use MallardDuck\Test\WhoisDomainList\MockLocators\EmptyFileTestLocator;
use MallardDuck\Test\WhoisDomainList\MockLocators\InvalidFileTestLocator;
use MallardDuck\WhoisDomainList\Exceptions\MissingArgument;
use MallardDuck\WhoisDomainList\Exceptions\UnknownTopLevelDomain;
use MallardDuck\WhoisDomainList\IanaServerLocator;
use MallardDuck\WhoisDomainList\PslServerLocator;

$ianaLocator = new IanaServerLocator();
$pslLocator = new PslServerLocator();
it('will throw an exception with URL provided as 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');
}

it('will throw an exception for empty input using IANA', function () use ($ianaLocator) {
ini_set('open_basedir', dirname(__DIR__));
$this->expectException(JsonException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessageMatches(
'#Cannot get source file from path: ([\/a-zA-Z\-]+)/tests/MockLocators/../../tmp/empty.json#',
);

new InvalidFileTestLocator();
});

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

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


it('will throw an exception for empty input using IANA', function () {
$ianaLocator = new IanaServerLocator();
expect($ianaLocator)->toBeObject()->toBeInstanceOf(IanaServerLocator::class);
$this->expectException(MissingArgument::class);
$ianaLocator->getWhoisServer('');
});

it('will throw an exception for a fake TLD using IANA', function () use ($ianaLocator) {
it('will throw an exception for a fake TLD using IANA', function () {
$ianaLocator = new IanaServerLocator();
expect($ianaLocator)->toBeObject()->toBeInstanceOf(IanaServerLocator::class);
$this->expectException(UnknownTopLevelDomain::class);
$ianaLocator->getWhoisServer('bebop');
});

it('will throw an exception for empty input using PSL', function () use ($pslLocator) {
it('will throw an exception for empty input using PSL', function () {
$pslLocator = new PslServerLocator();
expect($pslLocator)->toBeObject()->toBeInstanceOf(PslServerLocator::class);
$this->expectException(MissingArgument::class);
$pslLocator->getWhoisServer('');
});

it('will throw an exception for a fake TLD using PSL', function () use ($pslLocator) {
it('will throw an exception for a fake TLD using PSL', function () {
$pslLocator = new PslServerLocator();
expect($pslLocator)->toBeObject()->toBeInstanceOf(PslServerLocator::class);
$this->expectException(UnknownTopLevelDomain::class);
$pslLocator->getWhoisServer('bebop');
Expand Down
15 changes: 15 additions & 0 deletions tests/MockLocators/CorruptJsonFileTestLocator.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 CorruptJsonFileTestLocator extends ServerLocator
{
public function getServerListPath(): string
{
return __DIR__ . '/../stubs/corrupt.json';
}
}
15 changes: 15 additions & 0 deletions tests/MockLocators/EmptyFileTestLocator.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 EmptyFileTestLocator extends ServerLocator
{
public function getServerListPath(): string
{
return __DIR__ . '/../stubs/empty.json';
}
}
15 changes: 15 additions & 0 deletions tests/MockLocators/InvalidFileTestLocator.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 InvalidFileTestLocator extends ServerLocator
{
public function getServerListPath(): string
{
return __DIR__ . '/../../tmp/empty.json';
}
}
27 changes: 27 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

define('PROJ_PARENT_TMP', dirname(__DIR__, 2) . '/php-whois-domain-tmp');

/*
|--------------------------------------------------------------------------
| Test Case
Expand Down Expand Up @@ -37,3 +39,28 @@
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

// phpstan:disable
function getProperty(object $object, string $property)
{
$reflection = new ReflectionObject($object);
$reflectionProperty = $reflection->getProperty($property);
if (!$reflectionProperty->isPublic()) {
$reflectionProperty->setAccessible(true);
}

if (!$reflectionProperty->isInitialized($object)) {
return null;
}

return $reflectionProperty->getValue($object);
}

afterAll(static function () {
if (is_file(__DIR__ . '/../../tmp/empty.json')) {
unlink(__DIR__ . '/../../tmp/empty.json');
}
if (is_dir(PROJ_PARENT_TMP)) {
rmdir(PROJ_PARENT_TMP);
}
});
24 changes: 22 additions & 2 deletions tests/ServerListPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,31 @@
$pslLocator = new PslServerLocator();

it('can find the expected whois server using IANA', function () use ($ianaLocator) {
expect($ianaLocator)->toBeObject()->toBeInstanceOf(IanaServerLocator::class);
expect($ianaLocator)
->toBeObject()
->toBeInstanceOf(IanaServerLocator::class);
expect($ianaLocator->getServerListPath())->toBeString()->toEndWith('/../resources/iana-servers.json');
});

it('has proper value set for collection using IANA', function () use ($ianaLocator) {
expect($ianaLocator)
->toBeObject()
->toBeInstanceOf(IanaServerLocator::class)
->toHaveProperty('whoisServerCollection');
expect(getProperty($ianaLocator, 'whoisServerCollection'))->toBeArray()->toHaveKey('_meta');
});

it('can find the expected whois server using PSL', function () use ($pslLocator) {
expect($pslLocator)->toBeObject()->toBeInstanceOf(PslServerLocator::class);
expect($pslLocator)
->toBeObject()
->toBeInstanceOf(PslServerLocator::class);
expect($pslLocator->getServerListPath())->toBeString()->toEndWith('/../resources/psl-servers.json');
});

it('has proper value set for collection using PSL', function () use ($pslLocator) {
expect($pslLocator)
->toBeObject()
->toBeInstanceOf(PslServerLocator::class)
->toHaveProperty('whoisServerCollection');
expect(getProperty($pslLocator, 'whoisServerCollection'))->toBeArray()->toHaveKey('_meta');
});
4 changes: 4 additions & 0 deletions tests/stubs/corrupt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"yeet": "fleet",
2: "broke AF"
}
Empty file added tests/stubs/empty.json
Empty file.

0 comments on commit 38fcce3

Please sign in to comment.