Skip to content

Commit

Permalink
Update test suite to supports PHP8.3 (#354)
Browse files Browse the repository at this point in the history
Update package to work in PHP8.3
  • Loading branch information
nyamsprod authored Oct 17, 2023
1 parent 43bddff commit 253c754
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 61 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
php: ['8.1', '8.2']
php: ['8.1', '8.2', '8.3']
stability: [prefer-lowest, prefer-stable]
steps:
- name: Checkout code
Expand Down Expand Up @@ -52,4 +52,4 @@ jobs:

- name: Run Coding style rules
run: composer phpcs:fix
if: ${{ matrix.php == '8.1' && matrix.stability == 'prefer-stable'}}
if: ${{ matrix.php == '8.2' && matrix.stability == 'prefer-stable'}}
4 changes: 2 additions & 2 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'import_functions' => true,
],
'list_syntax' => ['syntax' => 'short'],
'new_with_braces' => true,
'new_with_parentheses' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_phpdoc' => true,
'no_empty_comment' => true,
Expand All @@ -39,7 +39,7 @@
'phpdoc_summary' => true,
'psr_autoloading' => true,
'return_type_declaration' => ['space_before' => 'none'],
'single_blank_line_before_namespace' => true,
'blank_lines_before_namespace' => true,
'single_quote' => true,
'space_after_semicolon' => true,
'ternary_operator_spaces' => true,
Expand Down
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@
"ext-json": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^v3.13.2",
"guzzlehttp/guzzle": "^7.5",
"guzzlehttp/psr7": "^1.6 || ^2.4.3",
"phpstan/phpstan": "^1.10.3",
"phpstan/phpstan-phpunit": "^1.3.8",
"phpstan/phpstan-strict-rules": "^1.5.0",
"phpunit/phpunit": "^10.0.12",
"psr/http-factory": "^1.0.1",
"friendsofphp/php-cs-fixer": "^v3.35.1",
"guzzlehttp/guzzle": "^7.8",
"guzzlehttp/psr7": "^1.6 || ^2.6.1",
"phpstan/phpstan": "^1.10.39",
"phpstan/phpstan-phpunit": "^1.3.15",
"phpstan/phpstan-strict-rules": "^1.5.1",
"phpunit/phpunit": "^10.4.1",
"psr/http-factory": "^1.0.2",
"psr/simple-cache": "^1.0.1",
"symfony/cache": "^v5.0.0 || ^v6.0.0"
"symfony/cache": "^v5.0.0 || ^v6.3.5"
},
"suggest": {
"psr/http-client-implementation": "To use the storage functionnality which depends on PSR-18",
Expand All @@ -71,7 +71,7 @@
"scripts": {
"phpcs": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -vvv --diff --dry-run --allow-risky=yes --ansi",
"phpcs:fix": "php-cs-fixer fix -vvv --allow-risky=yes --ansi",
"phpstan": "phpstan analyse -l max -c phpstan.neon src --xdebug --memory-limit=256M --ansi",
"phpstan": "phpstan analyse -l max -c phpstan.neon src --memory-limit=256M --ansi",
"phpunit": "XDEBUG_MODE=coverage phpunit --coverage-text",
"test": [
"@phpunit",
Expand Down
26 changes: 9 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix="Test.php">src</directory>
</exclude>
<report>
<clover outputFile="build/clover.xml"/>
<html outputDirectory="build/coverage"/>
Expand All @@ -31,4 +15,12 @@
<logging>
<junit outputFile="build/junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix="Test.php">src</directory>
</exclude>
</source>
</phpunit>
6 changes: 3 additions & 3 deletions src/Storage/PsrStorageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ final class PsrStorageFactoryTest extends TestCase

public function setUp(): void
{
$cache = $this->createStub(CacheInterface::class);
$requestFactory = $this->createStub(RequestFactoryInterface::class);
$client = $this->createStub(ClientInterface::class);
$cache = self::createStub(CacheInterface::class);
$requestFactory = self::createStub(RequestFactoryInterface::class);
$client = self::createStub(ClientInterface::class);

$this->factory = new PsrStorageFactory($cache, $client, $requestFactory);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Storage/PublicSuffixListPsr16CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class PublicSuffixListPsr16CacheTest extends TestCase
{
public function testItReturnsNullIfTheCacheDoesNotExists(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('get')->willReturn(null);

$pslCache = new PublicSuffixListPsr16Cache($cache, 'pdp_', '1 DAY');
Expand All @@ -29,7 +29,7 @@ public function testItReturnsNullIfTheCacheDoesNotExists(): void
public function testItReturnsAnInstanceIfTheCorrectCacheExists(): void
{
$rules = Rules::fromPath(dirname(__DIR__, 2).'/test_data/public_suffix_list.dat');
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('get')->willReturn($rules);

$pslCache = new PublicSuffixListPsr16Cache($cache, 'pdp_', 86400);
Expand All @@ -39,7 +39,7 @@ public function testItReturnsAnInstanceIfTheCorrectCacheExists(): void

public function testItReturnsNullIfTheCacheContentContainsInvalidJsonData(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('get')->willReturn('foobar');

$pslCache = new PublicSuffixListPsr16Cache($cache, 'pdp_', 86400);
Expand All @@ -48,7 +48,7 @@ public function testItReturnsNullIfTheCacheContentContainsInvalidJsonData(): voi

public function testItReturnsNullIfTheCacheContentCannotBeConvertedToTheCorrectInstance(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('get')->willReturn('{"foo":"bar"}');

$pslCache = new PublicSuffixListPsr16Cache($cache, 'pdp_', new DateTimeImmutable('+1 DAY'));
Expand All @@ -58,7 +58,7 @@ public function testItReturnsNullIfTheCacheContentCannotBeConvertedToTheCorrectI

public function testItCanStoreAPublicSuffixListInstance(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('set')->willReturn(true);

$psl = Rules::fromPath(dirname(__DIR__, 2).'/test_data/public_suffix_list.dat');
Expand All @@ -69,7 +69,7 @@ public function testItCanStoreAPublicSuffixListInstance(): void

public function testItReturnsFalseIfItCantStoreAPublicSuffixListInstance(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('set')->willReturn(false);

$psl = Rules::fromPath(dirname(__DIR__, 2).'/test_data/public_suffix_list.dat');
Expand All @@ -82,7 +82,7 @@ public function testItReturnsFalseIfItCantCacheAPublicSuffixListInstance(): void
{
$exception = new class('Something went wrong.', 0) extends RuntimeException implements CacheException {
};
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('set')->will(self::throwException($exception));

$psl = Rules::fromPath(dirname(__DIR__, 2).'/test_data/public_suffix_list.dat');
Expand All @@ -95,7 +95,7 @@ public function testItWillThrowIfItCantCacheAPublicSuffixListInstance(): void
{
$exception = new class('Something went wrong.', 0) extends RuntimeException {
};
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('set')->will(self::throwException($exception));

$psl = Rules::fromPath(dirname(__DIR__, 2).'/test_data/public_suffix_list.dat');
Expand All @@ -114,7 +114,7 @@ public function testItCanDeleteTheCachedDatabase(): void
{
$uri = 'http://www.example.com';

$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('delete')->willReturn(true);

$instance = new PublicSuffixListPsr16Cache($cache, 'pdp_', new DateInterval('P1D'));
Expand All @@ -125,7 +125,7 @@ public function testItWillThrowIfTheTTLIsNotParsable(): void
{
$this->expectException(InvalidArgumentException::class);

$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
new PublicSuffixListPsr16Cache($cache, 'pdp_', 'foobar');
}
}
28 changes: 21 additions & 7 deletions src/Storage/TimeToLive.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DateTimeInterface;
use InvalidArgumentException;
use Stringable;
use Throwable;
use function filter_var;
use const FILTER_VALIDATE_INT;

Expand All @@ -19,13 +20,26 @@ final class TimeToLive
{
public static function fromDurationString(string $duration): DateInterval
{
set_error_handler(fn () => true);
$interval = DateInterval::createFromDateString($duration);
restore_error_handler();
if (!$interval instanceof DateInterval) {
throw new InvalidArgumentException(
'The ttl value "'.$duration.'" can not be parsable by `DateInterval::createFromDateString`.'
);
try {
set_error_handler(fn () => true);
$interval = DateInterval::createFromDateString($duration);
restore_error_handler();
if (!$interval instanceof DateInterval) {
throw new InvalidArgumentException(
'The ttl value "'.$duration.'" can not be parsable by `DateInterval::createFromDateString`.'
);
}

} catch (Throwable $exception) {
if (!$exception instanceof InvalidArgumentException) {
throw new InvalidArgumentException(
'The ttl value "'.$duration.'" can not be parsable by `DateInterval::createFromDateString`.',
0,
$exception
);
}

throw $exception;
}

return $interval;
Expand Down
20 changes: 10 additions & 10 deletions src/Storage/TopLevelDomainListPsr16CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class TopLevelDomainListPsr16CacheTest extends TestCase
{
public function testItReturnsNullIfTheCacheDoesNotExists(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('get')->willReturn(null);

$instance = new TopLevelDomainListPsr16Cache($cache, 'pdp_', '1 DAY');
Expand All @@ -29,7 +29,7 @@ public function testItReturnsNullIfTheCacheDoesNotExists(): void
public function testItReturnsAnInstanceIfTheCorrectCacheExists(): void
{
$topLevelDomainList = TopLevelDomains::fromPath(dirname(__DIR__, 2).'/test_data/tlds-alpha-by-domain.txt');
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('get')->willReturn($topLevelDomainList);

$instance = new TopLevelDomainListPsr16Cache($cache, 'pdp_', 86400);
Expand All @@ -39,7 +39,7 @@ public function testItReturnsAnInstanceIfTheCorrectCacheExists(): void

public function testItReturnsNullIfTheCacheContentContainsInvalidJsonData(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('get')->willReturn('foobar');

$instance = new TopLevelDomainListPsr16Cache($cache, 'pdp_', new DateInterval('P1D'));
Expand All @@ -49,7 +49,7 @@ public function testItReturnsNullIfTheCacheContentContainsInvalidJsonData(): voi

public function testItReturnsNullIfTheCacheContentCannotBeConvertedToTheCorrectInstance(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('get')->willReturn('{"foo":"bar"}');

$instance = new TopLevelDomainListPsr16Cache($cache, 'pdp_', new DateTimeImmutable('+1 DAY'));
Expand All @@ -59,7 +59,7 @@ public function testItReturnsNullIfTheCacheContentCannotBeConvertedToTheCorrectI

public function testItCanStoreAPublicSuffixListInstance(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('set')->willReturn(true);

$rzd = TopLevelDomains::fromPath(dirname(__DIR__, 2).'/test_data/tlds-alpha-by-domain.txt');
Expand All @@ -70,7 +70,7 @@ public function testItCanStoreAPublicSuffixListInstance(): void

public function testItReturnsFalseIfItCantStoreAPublicSuffixListInstance(): void
{
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('set')->willReturn(false);

$rzd = TopLevelDomains::fromPath(dirname(__DIR__, 2).'/test_data/tlds-alpha-by-domain.txt');
Expand All @@ -83,7 +83,7 @@ public function testItReturnsFalseIfItCantCacheATopLevelDomainListInstance(): vo
{
$exception = new class('Something went wrong.', 0) extends RuntimeException implements CacheException {
};
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('set')->will(self::throwException($exception));

$rzd = TopLevelDomains::fromPath(dirname(__DIR__, 2).'/test_data/tlds-alpha-by-domain.txt');
Expand All @@ -96,7 +96,7 @@ public function testItThrowsIfItCantCacheATopLevelDomainListInstance(): void
{
$exception = new class('Something went wrong.', 0) extends RuntimeException {
};
$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('set')->will(self::throwException($exception));

$rzd = TopLevelDomains::fromPath(dirname(__DIR__, 2).'/test_data/tlds-alpha-by-domain.txt');
Expand All @@ -111,7 +111,7 @@ public function testItCanDeleteTheCachedDatabase(): void
{
$uri = 'http://www.example.com';

$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
$cache->method('delete')->willReturn(true);

$instance = new TopLevelDomainListPsr16Cache($cache, 'pdp_', new DateInterval('P1D'));
Expand All @@ -122,7 +122,7 @@ public function testItWillThrowIfTheTTLIsNotParsable(): void
{
$this->expectException(InvalidArgumentException::class);

$cache = $this->createStub(CacheInterface::class);
$cache = self::createStub(CacheInterface::class);
new TopLevelDomainListPsr16Cache($cache, 'pdp_', 'foobar');
}
}

0 comments on commit 253c754

Please sign in to comment.