Skip to content

Commit

Permalink
Add support for php 8.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansuter committed Nov 9, 2022
1 parent 420d3a1 commit 4e2241b
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 88 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.2, 7.3, 7.4, 8.0]
php: [7.2, 7.3, 7.4, 8.0, 8.1, 8.2]
experimental: [false]
include:
- php: 8.0
analysis: true
- php: 8.1
analysis: true
- php: 8.2
experimental: true

steps:
Expand All @@ -37,7 +37,7 @@ jobs:

- name: Static analysis
if: matrix.analysis
run: vendor/bin/phpstan analyse src
run: vendor/bin/phpstan analyse src --memory-limit=-1 --xdebug

- name: Tests
run: vendor/bin/phpunit --coverage-clover clover.xml
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
.idea
.phpunit.result.cache
composer.lock
phpunit.xml
clover.xml
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "adriansuter/twig-cache-busting",
"type": "library",
"description": "Twig Cache Busting is an add-on for Twig to support cache busting",
"description": "Twig Cache Busting is an add-on for Twig to support cache busting on template compilation.",
"keywords": [
"twig",
"cache",
Expand All @@ -12,16 +12,16 @@
{
"name": "Adrian Suter",
"email": "adrian@suter-wirz.ch",
"homepage": "https://www.adriansuter.ch"
"homepage": "https://www.adriansuter.ch/"
}
],
"require": {
"php": "^7.1 || ^8",
"twig/twig": ">=2.11 || ^3"
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.3",
"phpspec/prophecy": "^1.12",
"phpunit/phpunit": "^9.3",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^0.12.80 || ^1.0.0",
"squizlabs/php_codesniffer": "^3.5"
},
Expand All @@ -37,8 +37,8 @@
"@phpcs",
"@phpstan"
],
"phpunit": "php vendor/bin/phpunit",
"phpcs": "php vendor/bin/phpcs",
"phpstan": "php -d memory_limit=-1 vendor/bin/phpstan analyse src"
"phpunit": "phpunit",
"phpcs": "phpcs",
"phpstan": "phpstan analyse src --memory-limit=-1 --xdebug"
}
}
22 changes: 10 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.1/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
Expand All @@ -13,20 +13,18 @@
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php">
<coverage processUncoveredFiles="true">
<include>
<directory>./src/</directory>
</include>
<report>
<html outputDirectory="./coverage" lowUpperBound="20" highLowerBound="50"/>
</report>
</coverage>
<testsuites>
<testsuite name="Twig Cache Busting Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html"
target="./coverage"
lowUpperBound="20"
highLowerBound="50"/>
</logging>
<logging/>
</phpunit>
12 changes: 6 additions & 6 deletions src/CacheBusters/FileNameCacheBuster.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,29 @@ public function __construct(
*/
public function bust(string $path): string
{
$filePath = $this->endPointDirectory.'/'.$path;
$filePath = $this->endPointDirectory . '/' . $path;

$pi = pathinfo($path);
if (!array_key_exists('extension', $pi)) {
return $path;
}

$v = '';
if ($pi['dirname'] !== '.') {
if (array_key_exists('dirname', $pi) && $pi['dirname'] !== '.') {
$v .= $pi['dirname'];
}

if ($pi['filename'] === '') {
return ($v !== '' ? $v.'/.' : '.').$pi['extension'];
return ($v !== '' ? $v . '/.' : '.') . $pi['extension'];
}

$v = ($v !== '' ? $v.'/' : '').$pi['filename'];
$v = ($v !== '' ? $v . '/' : '') . $pi['filename'];

$hash = $this->hashGenerator->generate($filePath);
if ($hash !== null) {
$v .= '.'.$hash;
$v .= '.' . $hash;
}
$v .= '.'.$pi['extension'];
$v .= '.' . $pi['extension'];

return $v;
}
Expand Down
5 changes: 4 additions & 1 deletion tests/CacheBusters/DictionaryCacheBusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use AdrianSuter\TwigCacheBusting\CacheBusters\DictionaryCacheBuster;
use AdrianSuter\TwigCacheBusting\Interfaces\DictionaryInterface;
use AdrianSuter\TwigCacheBusting\Tests\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class DictionaryCacheBusterTest extends TestCase
{
public function testDefaults()
use ProphecyTrait;

public function testDefaults(): void
{
$dictionaryProphecy = $this->prophesize(DictionaryInterface::class);
$dictionaryProphecy->lookup('image.jpg')->willReturn('image-1234.jpg');
Expand Down
9 changes: 6 additions & 3 deletions tests/CacheBusters/FileNameCacheBusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use AdrianSuter\TwigCacheBusting\Interfaces\HashGeneratorInterface;
use AdrianSuter\TwigCacheBusting\Tests\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

class FileNameCacheBusterTest extends TestCase
{
public function testDefaults()
use ProphecyTrait;

public function testDefaults(): void
{
$fileNameCacheBuster = new FileNameCacheBuster($this->publicPath());

Expand All @@ -38,7 +41,7 @@ public function testDefaults()
unset($GLOBALS['filemtime_return']);
}

public function testBustWithCustomHashGenerator()
public function testBustWithCustomHashGenerator(): void
{
$hashGeneratorProphecy = $this->prophesize(HashGeneratorInterface::class);
$hashGeneratorProphecy->generate(Argument::any())->willReturn('1234');
Expand All @@ -50,7 +53,7 @@ public function testBustWithCustomHashGenerator()
$this->assertEquals('image.1234.jpg', $fileNameCacheBuster->bust('image.jpg'));
}

public function testBustWithCustomHashGeneratorGeneratingNull()
public function testBustWithCustomHashGeneratorGeneratingNull(): void
{
$hashGeneratorProphecy = $this->prophesize(HashGeneratorInterface::class);
$hashGeneratorProphecy->generate(Argument::any())->willReturn(null);
Expand Down
9 changes: 6 additions & 3 deletions tests/CacheBusters/QueryParamCacheBusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use AdrianSuter\TwigCacheBusting\Interfaces\HashGeneratorInterface;
use AdrianSuter\TwigCacheBusting\Tests\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

class QueryParamCacheBusterTest extends TestCase
{
public function testDefaults()
use ProphecyTrait;

public function testDefaults(): void
{
$queryParamCacheBuster = new QueryParamCacheBuster($this->publicPath());

Expand All @@ -38,7 +41,7 @@ public function testDefaults()
unset($GLOBALS['filemtime_return']);
}

public function testBustWithCustomHashGenerator()
public function testBustWithCustomHashGenerator(): void
{
$hashGeneratorProphecy = $this->prophesize(HashGeneratorInterface::class);
$hashGeneratorProphecy->generate(Argument::any())->willReturn('1234');
Expand All @@ -50,7 +53,7 @@ public function testBustWithCustomHashGenerator()
$this->assertEquals('image.jpg?h=1234', $queryParamCacheBuster->bust('image.jpg'));
}

public function testBustWithCustomHashGeneratorGeneratingNull()
public function testBustWithCustomHashGeneratorGeneratingNull(): void
{
$hashGeneratorProphecy = $this->prophesize(HashGeneratorInterface::class);
$hashGeneratorProphecy->generate(Argument::any())->willReturn(null);
Expand Down
9 changes: 6 additions & 3 deletions tests/CacheBustingTokenParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@

use AdrianSuter\TwigCacheBusting\CacheBustingTokenParser;
use AdrianSuter\TwigCacheBusting\Interfaces\CacheBusterInterface;
use Prophecy\PhpUnit\ProphecyTrait;
use ReflectionException;
use ReflectionMethod;

class CacheBustingTokenParserTest extends TestCase
{
public function testGetTagDefault()
use ProphecyTrait;

public function testGetTagDefault(): void
{
$cacheBuster = $this->createMock(CacheBusterInterface::class);
$cacheBustingTokenParser = new CacheBustingTokenParser($cacheBuster);

$this->assertEquals('cache_busting', $cacheBustingTokenParser->getTag());
}

public function testGetTagCustomized()
public function testGetTagCustomized(): void
{
$cacheBuster = $this->createMock(CacheBusterInterface::class);
$cacheBustingTokenParser = new CacheBustingTokenParser($cacheBuster, null, 'cb');
Expand Down Expand Up @@ -55,7 +58,7 @@ public function basePathDataProvider(): array
*
* @throws ReflectionException
*/
public function testBasePath(string $basePath, string $assetPath, string $assetBustPath, string $expected)
public function testBasePath(string $basePath, string $assetPath, string $assetBustPath, string $expected): void
{
$cacheBusterProphecy = $this->prophesize(CacheBusterInterface::class);
$cacheBusterProphecy->bust($assetPath)->willReturn($assetBustPath);
Expand Down
10 changes: 7 additions & 3 deletions tests/CacheBustingTwigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class CacheBustingTwigExtensionTest extends TestCase
{
public function testGetTokenParsers()
public function testGetTokenParsers(): void
{
$tokenParser = $this->createMock(CacheBustingTokenParser::class);
$cacheBustingTwigExtension = new CacheBustingTwigExtension($tokenParser);
Expand Down Expand Up @@ -41,8 +41,12 @@ public function createDataProvider(): array
* @param string $expectedTwigTag
* @throws ReflectionException
*/
public function testCreate(?string $basePath, ?string $twigTag, string $expectedBasePath, string $expectedTwigTag)
{
public function testCreate(
?string $basePath,
?string $twigTag,
string $expectedBasePath,
string $expectedTwigTag
): void {
$cacheBuster = $this->createMock(CacheBusterInterface::class);

if ($basePath === null && $twigTag === null) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Dictionaries/ArrayDictionaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

class ArrayDictionaryTest extends TestCase
{
public function testDefault()
public function testDefault(): void
{
$d = new ArrayDictionary(['image.jpg' => 'image-1234.jpg']);
$this->assertEquals('image-1234.jpg', $d->lookup('image.jpg'));
}

public function testNullIfNotArrayKeyExists()
public function testNullIfNotArrayKeyExists(): void
{
$d = new ArrayDictionary();
$this->assertNull($d->lookup('image.jpg'));
Expand Down
32 changes: 20 additions & 12 deletions tests/HashGenerators/FileMD5HashGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,39 @@

class FileMD5HashGeneratorTest extends TestCase
{
public function testDefault()
public function testDefault(): void
{
$hashGenerator = new FileMD5HashGenerator();
$GLOBALS['md5_file_return'] = 'abcd';
$this->assertEquals('abcd', $hashGenerator->generate(
$this->publicPath('bar.js')
));
$this->assertEquals(
'abcd',
$hashGenerator->generate(
$this->publicPath('bar.js')
)
);
unset($GLOBALS['md5_file_return']);
}

public function testNonExistentFile()
public function testNonExistentFile(): void
{
$hashGenerator = new FileMD5HashGenerator();
$this->assertEquals('', $hashGenerator->generate(
$this->publicPath('nonExistent.jpg')
));
$this->assertEquals(
'',
$hashGenerator->generate(
$this->publicPath('nonExistent.jpg')
)
);
}

public function testFileMTimeFailure()
public function testFileMTimeFailure(): void
{
$hashGenerator = new FileMD5HashGenerator();
$GLOBALS['md5_file_return'] = false;
$this->assertNull($hashGenerator->generate(
$this->publicPath('bar.js')
));
$this->assertNull(
$hashGenerator->generate(
$this->publicPath('bar.js')
)
);
unset($GLOBALS['md5_file_return']);
}
}

0 comments on commit 4e2241b

Please sign in to comment.