Skip to content

Commit

Permalink
compatibility with symfony 7
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Nov 30, 2023
1 parent 6fe87e9 commit af58a27
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
"symfony/phpunit-bridge": "^5.0 || ^6.0"
},
"conflict": {
"toflar/psr6-symfony-http-cache-store": "<2.2.1",
"symfony/http-foundation": ">=7"
"toflar/psr6-symfony-http-cache-store": "<2.2.1"
},
"suggest": {
"friendsofsymfony/http-cache-bundle": "For integration with the Symfony framework",
Expand Down
8 changes: 6 additions & 2 deletions src/SymfonyCache/AccessControlledListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\IpsRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -26,7 +27,7 @@
abstract class AccessControlledListener implements EventSubscriberInterface
{
/**
* @var RequestMatcher
* @var RequestMatcherInterface
*/
private $clientMatcher;

Expand Down Expand Up @@ -54,7 +55,10 @@ public function __construct(array $options = [])
throw new \InvalidArgumentException('You may not set both a request matcher and an IP.');
}
if (!$clientMatcher) {
$clientMatcher = new RequestMatcher(null, null, null, $options['client_ips'] ?: '127.0.0.1');
$clientMatcher = class_exists(IpsRequestMatcher::class)
? new IpsRequestMatcher($options['client_ips'] ?: '127.0.0.1')
: new RequestMatcher(null, null, null, $options['client_ips'] ?: '127.0.0.1')
;
}

$this->clientMatcher = $clientMatcher;
Expand Down
16 changes: 13 additions & 3 deletions tests/Unit/SymfonyCache/PurgeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Toflar\Psr6HttpCacheStore\Psr6Store;
Expand All @@ -35,7 +37,7 @@ public function testConstructorOverspecified()
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('You may not set both a request matcher and an IP');
new PurgeListener([
'client_matcher' => new RequestMatcher('/forbidden'),
'client_matcher' => $this->createRequestMatcher('/forbidden'),
'client_ips' => ['1.2.3.4'],
]);
}
Expand Down Expand Up @@ -129,7 +131,7 @@ public function testPurgeForbiddenMatcher()
{
$kernel = $this->getUnusedKernelMock();

$matcher = new RequestMatcher('/forbidden');
$matcher = $this->createRequestMatcher('/forbidden');
$purgeListener = new PurgeListener(['client_matcher' => $matcher]);
$request = Request::create('http://example.com/foo', 'PURGE');
$event = new CacheEvent($kernel, $request);
Expand Down Expand Up @@ -162,7 +164,7 @@ public function testPurgeForbiddenIp()
public function testOtherMethod()
{
$kernel = $this->getUnusedKernelMock();
$matcher = \Mockery::mock(RequestMatcher::class)
$matcher = \Mockery::mock(RequestMatcherInterface::class)
->shouldNotReceive('isRequestAllowed')
->getMock();

Expand All @@ -184,6 +186,14 @@ public function testInvalidConfiguration()
new PurgeListener(['stuff' => '1.2.3.4']);
}

private function createRequestMatcher(string $path): RequestMatcherInterface
{
return class_exists(PathRequestMatcher::class)
? new PathRequestMatcher($path)
: new RequestMatcher($path)
;
}

/**
* @return CacheInvalidation|MockInterface
*/
Expand Down
8 changes: 6 additions & 2 deletions tests/Unit/SymfonyCache/RefreshListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
use Symfony\Component\HttpFoundation\Response;

class RefreshListenerTest extends TestCase
Expand All @@ -42,7 +43,7 @@ public function testRefreshAllowed()
$this->kernel->expects($this->once())
->method('fetch')
->with($request)
->will($this->returnValue($response))
->willReturn($response)
;

$refreshListener = new RefreshListener();
Expand All @@ -57,7 +58,10 @@ public function testRefreshForbiddenMatcher()
->method('fetch')
;

$matcher = new RequestMatcher('/forbidden');
$matcher = class_exists(PathRequestMatcher::class)
? new PathRequestMatcher('/forbidden')
: new RequestMatcher('/forbidden')
;
$refreshListener = new RefreshListener(['client_matcher' => $matcher]);
$request = Request::create('http://example.com/foo');
$request->headers->addCacheControlDirective('no-cache');
Expand Down

0 comments on commit af58a27

Please sign in to comment.