Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

permissions:
contents: read

jobs:
tests:
name: Symfony ${{ matrix.symfony }} / PHP ${{ matrix.php }}${{ matrix.deps == 'lowest' && ' / lowest deps' || '' }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- php: '7.4'
symfony: '3.4'
- php: '8.1'
symfony: '3.4'
- php: '7.4'
symfony: '4.4'
- php: '8.0'
symfony: '4.4'
- php: '8.3'
symfony: '4.4'
- php: '7.4'
symfony: '5.4'
- php: '8.0'
symfony: '5.4'
- php: '8.2'
symfony: '5.4'
- php: '8.4'
symfony: '5.4'
- php: '8.1'
symfony: '6.4'
- php: '8.2'
symfony: '6.4'
- php: '8.3'
symfony: '6.4'
- php: '8.4'
symfony: '6.4'
- php: '7.4'
symfony: '4.4'
deps: lowest
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
tools: composer:v2

- name: Install dependencies
run: |
composer require \
'symfony/config:^${{ matrix.symfony }}' \
'symfony/console:^${{ matrix.symfony }}' \
'symfony/dependency-injection:^${{ matrix.symfony }}' \
'symfony/http-kernel:^${{ matrix.symfony }}' \
'symfony/yaml:^${{ matrix.symfony }}' \
--no-update
composer require --dev 'symfony/framework-bundle:^${{ matrix.symfony }}' --no-update
composer update --prefer-dist --no-progress --no-security-blocking ${{ matrix.deps == 'lowest' && '--prefer-lowest --prefer-stable' || '' }}

- name: PHPUnit
run: composer test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
vendor
composer.phar
composer.lock
.phpunit.result.cache
4 changes: 3 additions & 1 deletion CacheTagsBundle/Command/InvalidateTagCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function configure()
$this->setDescription('Invalidates tag(s)');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$verbosity = $output->getVerbosity();
foreach ($input->getArgument(self::ARGUMENT_TAG) as $tag)
Expand All @@ -51,5 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->invalidator->invalidate(new Plain($tag));
}

return 0;
}
}
14 changes: 13 additions & 1 deletion CacheTagsBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lbarulski\CacheTagsBundle\DependencyInjection;

use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand Down Expand Up @@ -78,7 +79,18 @@ private function addVarnishNode(ArrayNodeDefinition $proxiesNode)
$varnishOptions->children()->scalarNode('path')->defaultValue('/')->end();
$varnishOptions->children()->integerNode('timeout')->defaultValue(1)->end();
$varnishOptions->children()->scalarNode('header')->defaultValue('X-CACHE-TAG')->end();
$varnishOptions->children()->scalarNode('host_header')->setDeprecated('The child node "%node%" at path "%path%" is deprecated. Add "Host" header via "headers" instead')->defaultNull()->end();
$hostHeaderNode = $varnishOptions->children()->scalarNode('host_header');
$hostHeaderDeprecation = 'The child node "%node%" at path "%path%" is deprecated. Add "Host" header via "headers" instead';
// Symfony 5.1 added the package and version arguments; 6.0 requires them
if (method_exists(BaseNode::class, 'getDeprecation'))
{
$hostHeaderNode->setDeprecated('lbarulski/cache-tags-bundle', '2.0', $hostHeaderDeprecation);
}
else
{
$hostHeaderNode->setDeprecated($hostHeaderDeprecation);
}
$hostHeaderNode->defaultNull()->end();
$varnishOptions->children()->booleanNode('ssl_verify_peer')->defaultFalse()->end();
$varnishOptions->children()->arrayNode('headers')
->prototype('array')
Expand Down
9 changes: 5 additions & 4 deletions CacheTagsBundle/Listener/CacheTagListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use lbarulski\CacheTagsBundle\Annotation\CacheTag\RequestAttribute;
use lbarulski\CacheTagsBundle\Service\Repository;
use lbarulski\CacheTagsBundle\Tag\Plain as PlainTag;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

class CacheTagListener
Expand All @@ -24,9 +25,9 @@ public function __construct(Repository $repository)
}

/**
* @param FilterControllerEvent $event
* @param FilterControllerEvent|ControllerEvent $event
*/
public function requestAttribute(FilterControllerEvent $event)
public function requestAttribute($event)
{
$request = $event->getRequest();
$aliasName = $this->getAliasName(RequestAttribute::ALIAS);
Expand All @@ -50,9 +51,9 @@ public function requestAttribute(FilterControllerEvent $event)
}

/**
* @param FilterControllerEvent $event
* @param FilterControllerEvent|ControllerEvent $event
*/
public function plain(FilterControllerEvent $event)
public function plain($event)
{
$request = $event->getRequest();
$aliasName = $this->getAliasName(Plain::ALIAS);
Expand Down
9 changes: 5 additions & 4 deletions CacheTagsBundle/Listener/ResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use lbarulski\CacheTagsBundle\Service\Repository;
use lbarulski\CacheTagsBundle\Service\Tagger;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

class ResponseListener
{
Expand All @@ -30,11 +30,12 @@ public function __construct(Repository $repository, Tagger $tagger)
}

/**
* @param FilterResponseEvent $event
* @param FilterResponseEvent|ResponseEvent $event
*/
public function onKernelResponse(FilterResponseEvent $event)
public function onKernelResponse($event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType())
// isMasterRequest() is removed in Symfony 6.0, isMainRequest() exists since 5.3
if (!(method_exists($event, 'isMainRequest') ? $event->isMainRequest() : $event->isMasterRequest()))
{
return;
}
Expand Down
23 changes: 19 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,25 @@
"php": "^7.4 || ^8.0",
"doctrine/annotations": "1.*",
"sensio/framework-extra-bundle": "^3 || ^4 || ^5 || ^6",
"symfony/dependency-injection": "^2.8 || ^3.0 || ^4.0",
"symfony/http-kernel": "^2.8 || ^3.0 || ^4.0",
"symfony/config": "^2.8 || ^3.0 || ^4.0",
"symfony/console": "^2.8 || ^3.0 || ^4.0"
"symfony/dependency-injection": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
"symfony/http-kernel": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
"symfony/config": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
"symfony/console": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
"symfony/yaml": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/framework-bundle": "^3.4 || ^4.4 || ^5.4 || ^6.4"
},
"autoload-dev": {
"psr-4": {
"lbarulski\\CacheTagsBundle\\Tests\\": "tests/"
}
},
"scripts": {
"test": [
"vendor/bin/phpunit -c phpunit.xml.dist"
]
},
"extra": {
"branch-alias": {
Expand Down
12 changes: 12 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertDeprecationsToExceptions="false">
<testsuites>
<testsuite name="CacheTagsBundle">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
77 changes: 77 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace lbarulski\CacheTagsBundle\Tests;

use lbarulski\CacheTagsBundle\DependencyInjection\Configuration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;

class ConfigurationTest extends TestCase
{
public function testResponseTagDefault()
{
$config = $this->process([['response' => []]]);

$this->assertSame('X-CACHE-TAGS', $config['response']['tag']);
}

public function testVarnishProxyDefaults()
{
$config = $this->process([['proxies' => ['varnish' => [[]]]]]);

$this->assertSame([
'host' => '127.0.0.1',
'port' => 80,
'path' => '/',
'timeout' => 1,
'header' => 'X-CACHE-TAG',
'host_header' => null,
'ssl_verify_peer' => false,
'headers' => [],
], $config['proxies']['varnish'][0]);
}

public function testHostHeaderAndHeaders()
{
$config = $this->process([[
'response' => ['tag' => 'X-TAGS'],
'proxies' => ['varnish' => [[
'host' => 'varnish.internal',
'port' => 6081,
'host_header' => 'www.example.com',
'headers' => [['name' => 'X-Foo', 'value' => 'bar']],
]]],
]]);

$this->assertSame('X-TAGS', $config['response']['tag']);
$varnish = $config['proxies']['varnish'][0];
$this->assertSame('varnish.internal', $varnish['host']);
$this->assertSame(6081, $varnish['port']);
$this->assertSame('www.example.com', $varnish['host_header']);
$this->assertSame([['name' => 'X-Foo', 'value' => 'bar']], $varnish['headers']);
}

public function testHeaderRequiresNameAndValue()
{
$this->expectException(InvalidConfigurationException::class);

$this->process([['proxies' => ['varnish' => [['headers' => [['name' => 'X-Foo']]]]]]]);
}

private function process(array $configs)
{
// host_header is deprecated; the deprecation notice is expected, not a failure.
set_error_handler(function ($level) {
return E_USER_DEPRECATED === $level;
});
try
{
return (new Processor())->processConfiguration(new Configuration(), $configs);
}
finally
{
restore_error_handler();
}
}
}
30 changes: 30 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace lbarulski\CacheTagsBundle\Tests;

use lbarulski\CacheTagsBundle\Command\InvalidateTagCommand;
use lbarulski\CacheTagsBundle\Invalidator\Proxy\Varnish;

class ContainerTest extends KernelTestCase
{
public function testBundleServicesAreWired()
{
$container = $this->kernel->getContainer();

$this->assertInstanceOf(InvalidateTagCommand::class, $container->get('cache_tags.command.invalidate_tag'));
$this->assertTrue($container->has('cache_tags.tagger'));
$this->assertTrue($container->has('cache_tags.listener.invalidator'));
$this->assertSame('X-CACHE-TAGS', $container->getParameter('cache_tags.response.tag'));
}

public function testEveryConfiguredVarnishProxyIsRegistered()
{
$proxies = $this->kernel->getContainer()->get('cache_tags.invalidator.proxy.manager')->getProxies();

$this->assertCount(2, $proxies);
foreach ($proxies as $proxy)
{
$this->assertInstanceOf(Varnish::class, $proxy);
}
}
}
46 changes: 46 additions & 0 deletions tests/Fixtures/FakeVarnish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace lbarulski\CacheTagsBundle\Tests\Fixtures;

/** Listens on a free local port and records the raw requests sent to it. */
class FakeVarnish
{
/** @var resource */
private $server;

/** @var int */
private $port;

public function __construct()
{
$this->server = stream_socket_server('tcp://127.0.0.1:0', $errNo, $errStr);
if (false === $this->server)
{
throw new \RuntimeException($errStr, $errNo);
}
$this->port = (int) substr(strrchr(stream_socket_get_name($this->server, false), ':'), 1);
}

public function getPort()
{
return $this->port;
}

/** @return string[] raw requests received so far */
public function getRequests()
{
$requests = [];
while ($connection = @stream_socket_accept($this->server, 0))
{
$requests[] = stream_get_contents($connection);
fclose($connection);
}

return $requests;
}

public function close()
{
fclose($this->server);
}
}
Loading
Loading