Skip to content

Commit

Permalink
Modernize code and test setup, prepare for Symfony 5.x and PHP 8.x (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude authored Apr 6, 2022
1 parent b3352f1 commit 2ab6d38
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 135 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/dependencies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Dependencies

permissions:
contents: read

on:
push:
branches:
- master
pull_request:

env:
PHP_VERSION: 7.4

jobs:
composer-require-checker:
name: Check missing composer requirements
runs-on: ubuntu-20.04
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.PHP_VERSION }}
tools: composer:v2
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: vendor
key: composer-${{ runner.os }}-${{ env.PHP_VERSION }}-${{ hashFiles('composer.json') }}
restore-keys: |
composer-${{ runner.os }}-${{ env.PHP_VERSION }}-
composer-${{ runner.os }}-
- run: |
composer install --no-interaction --no-progress --ansi --no-scripts
composer show
- uses: docker://webfactory/composer-require-checker:3.2.0
4 changes: 0 additions & 4 deletions .github/workflows/fix-cs-php.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Update this by running
# curl https://gist.githubusercontent.com/mpdude/ca93a185bcbf56eb7e341632ad4f8263/raw/fix-cs-php.yml > .github/workflows/fix-cs-php.yml

on:
push:
branches:
Expand All @@ -13,7 +10,6 @@ jobs:
open-pr-for-cs-violations:
name: PHP-CS-Fixer
runs-on: ubuntu-20.04
if: github.actor != 'dependabot[bot]'
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/lock-symfony-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

cat <<< $(jq --arg version $VERSION '.require |= with_entries(if ((.key|test("^symfony/"))) then .value=$version else . end)' < composer.json) > composer.json
40 changes: 40 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Tests

on:
push:
branches:
- master
pull_request:

env:
SYMFONY_DEPRECATIONS_HELPER: weak

jobs:
PHPUnit:
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
include:
- { php-version: 7.2, symfony-locked-version: none, dependency-version: prefer-lowest }
- { php-version: 7.4, symfony-locked-version: none, dependency-version: prefer-lowest }
- { php-version: 7.4, symfony-locked-version: none, dependency-version: prefer-stable }
- { php-version: 8.1, symfony-locked-version: none, dependency-version: prefer-stable }
name: PHPUnit (PHP ${{matrix.php-version}}, Symfony Version Lock ${{ matrix.symfony-locked-version }}, ${{ matrix.dependency-version }})
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: composer:v2
- uses: actions/cache@v2
with:
path: vendor
key: composer-${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.symfony-locked-version }}-${{ matrix.dependency-version }}-${{ hashFiles('composer.json') }}
restore-keys: |
composer-${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.symfony-locked-version }}-${{ matrix.dependency-version }}-
- run: VERSION=${{ matrix.symfony-locked-version }} .github/workflows/lock-symfony-version.sh
if: matrix.symfony-locked-version != 'none'
- run: composer update --${{ matrix.dependency-version }} --no-interaction --no-scripts --no-progress --ansi
- run: composer show
- run: vendor/bin/phpunit
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor/
.php_cs.cache
.phpunit.result.cache
composer.lock
78 changes: 42 additions & 36 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
{
"name": "webfactory/http-cache-bundle",
"description": "Symfony bundle to ease HTTP cache validation via last modified header",
"keywords": [
"caching",
"http",
"304",
"notmodified",
"ifnotmodifiedsince"
],
"license": "MIT",
"authors": [
{
"name": "webfactory GmbH",
"email": "[email protected]",
"homepage": "http://www.webfactory.de",
"role": "Developer"
}
],
"name": "webfactory/http-cache-bundle",
"description": "Symfony bundle to ease HTTP cache validation via last modified header",
"keywords": [
"caching",
"http",
"304",
"notmodified",
"ifnotmodifiedsince"
],
"license": "MIT",
"authors": [
{
"name": "webfactory GmbH",
"email": "[email protected]",
"homepage": "http://www.webfactory.de",
"role": "Developer"
}
],

"require": {
"php": "^7.1",
"symfony/config": "^2.8 || ^3.0 || ^4.0",
"symfony/dependency-injection": "^2.8 || ^3.0 || ^4.0",
"symfony/http-foundation": "^2.8 || ^3.0 || ^4.0",
"symfony/http-kernel": "^2.8 || ^3.0 || ^4.0",
"doctrine/annotations": "^1.0"
},
"require": {
"php": "^7.1|8.0.*|8.1.*",
"symfony/config": "^4.4 | ^5.0",
"symfony/dependency-injection": "^4.4 | ^5.0",
"symfony/http-foundation": "^4.4 | ^5.0",
"symfony/http-kernel": "^4.4 | ^5.0",
"doctrine/annotations": "^1.0"
},

"require-dev": {
"phpunit/phpunit": "^4.8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.0",
"symfony/phpunit-bridge": ">=5.0"
},

"suggest": {
"webfactory/wfdmeta-bundle": "Invalidate caches based on meta data tracked with wfDynamic"
},
"suggest": {
"webfactory/wfdmeta-bundle": "Invalidate caches based on meta data tracked with wfDynamic"
},

"autoload": {
"psr-4": {
"Webfactory\\HttpCacheBundle\\": ""
"autoload": {
"psr-4": {
"Webfactory\\HttpCacheBundle\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Webfactory\\HttpCacheBundle\\Tests\\": "tests"
}
}
}
}
10 changes: 5 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd">
<testsuites>
<testsuite name="Library Test Suite">
<directory>Tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

<!-- Filter for code coverage -->
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory>vendor</directory>
<directory>Tests</directory>
</exclude>
</whitelist>
</filter>

<listeners>
<listener class="\Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use SplObjectStorage;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Webfactory\HttpCacheBundle\NotModified\Annotation\ReplaceWithNotModifiedResponse;

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ public function __construct(Reader $reader, ContainerInterface $container, bool
* header in the request, replace the determined controller action with a minimal action that just returns an
* "empty" response with a 304 Not Modified HTTP status code.
*/
public function onKernelController(FilterControllerEvent $event)
public function onKernelController(ControllerEvent $event)
{
$annotation = $this->findAnnotation($event->getController());
if (!$annotation) {
Expand Down Expand Up @@ -94,7 +94,7 @@ public function onKernelController(FilterControllerEvent $event)
* If a last modified date was determined for the current (master or sub) request, set it to the response so the
* client can use it for the "If-Modified-Since" header in subsequent requests.
*/
public function onKernelResponse(FilterResponseEvent $event)
public function onKernelResponse(ResponseEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace Webfactory\HttpCacheBundle\Tests\NotModified\Annotation;

use DateTime;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;
use RuntimeException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -21,24 +21,24 @@
/**
* Tests for the ReplaceWithNotModifiedResponse annotation.
*/
final class ReplaceWithNotModifiedResponseTest extends PHPUnit_Framework_TestCase
final class ReplaceWithNotModifiedResponseTest extends TestCase
{
/**
* @test
*/
public function lastModifiedDescriptionsCannotBeEmpty()
{
$this->setExpectedException(RuntimeException::class);
$this->expectException(RuntimeException::class);
$annotation = new ReplaceWithNotModifiedResponse(['value' => []]);
$annotation->determineLastModified(new Request());
}

/**
* @test
* @doesNotPerformAssertions
*/
public function stringAsSimpleLastModifiedDescription()
{
$this->setExpectedException(null);
$annotation = new ReplaceWithNotModifiedResponse(['value' => [MyLastModifedDeterminator::class]]);
$annotation->determineLastModified(new Request());
}
Expand All @@ -49,7 +49,7 @@ public function stringAsSimpleLastModifiedDescription()
public function serviceNameAsLastModifiedDescription()
{
/** @var ContainerInterface|PHPUnit_Framework_MockObject_MockObject $container */
$container = $this->getMock(ContainerInterface::class);
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
->method('get')
->with('my.service')
Expand All @@ -58,16 +58,15 @@ public function serviceNameAsLastModifiedDescription()
$annotation = new ReplaceWithNotModifiedResponse(['value' => ['@my.service']]);
$annotation->setContainer($container);

$this->setExpectedException(null);
$annotation->determineLastModified(new Request());
}

/**
* @test
* @doesNotPerformAssertions
*/
public function arrayAslastModifiedDeterminatorDescriptionWithConstructorArguments()
{
$this->setExpectedException(null);
$annotation = new ReplaceWithNotModifiedResponse(['value' => [[MyLastModifedDeterminator::class => new DateTime('2000-01-01')]]]);
$annotation->determineLastModified(new Request());
}
Expand All @@ -77,35 +76,36 @@ public function arrayAslastModifiedDeterminatorDescriptionWithConstructorArgumen
*/
public function lastModifiedDeterminatorsHaveToImplementInterface()
{
$this->setExpectedException(RuntimeException::class);
$this->expectException(RuntimeException::class);
$annotation = new ReplaceWithNotModifiedResponse(['value' => [FakeLastModifiedDeterminatorWithoutInterface::class]]);
$annotation->determineLastModified(new Request());
}

/**
* @test
* @group time-sensitive
*/
public function determineLastModifiedDeterminesLastModifiedOfOneDeterminator()
{
$annotation = new ReplaceWithNotModifiedResponse(['value' => [MyLastModifedDeterminator::class]]);
$this->assertEquals(
new DateTime(),
$annotation->determineLastModified(new Request()),
'',
$allowedDeltaInSeconds = 3
);

$lastModified = $annotation->determineLastModified(new Request());

self::assertEquals(DateTime::createFromFormat('U', time()), $lastModified);
}

/**
* @test
*/
public function determineLastModifiedDeterminesLastModifiedOfMultipleDeterminators()
{
$annotation = new ReplaceWithNotModifiedResponse(['value' => [
[MyLastModifedDeterminator::class => new DateTime('2001-01-01')],
[MyLastModifedDeterminator::class => new DateTime('2003-01-01')],
[MyLastModifedDeterminator::class => new DateTime('2002-01-01')],
]]);
$annotation = new ReplaceWithNotModifiedResponse([
'value' => [
[MyLastModifedDeterminator::class => new DateTime('2001-01-01')],
[MyLastModifedDeterminator::class => new DateTime('2003-01-01')],
[MyLastModifedDeterminator::class => new DateTime('2002-01-01')],
],
]);
$this->assertEquals(new DateTime('2003-01-01'), $annotation->determineLastModified(new Request()));
}
}
Expand All @@ -121,7 +121,7 @@ final class MyLastModifedDeterminator implements LastModifiedDeterminator

public function __construct(DateTime $lastModified = null)
{
$this->lastModified = $lastModified ?: new DateTime();
$this->lastModified = $lastModified ?: DateTime::createFromFormat('U', time());
}

public function getLastModified(Request $request)
Expand Down
Loading

0 comments on commit 2ab6d38

Please sign in to comment.