Skip to content

Commit bc3bdc6

Browse files
authored
Merge pull request #551 from FriendsOfSymfony/sf7
compatibility with symfony 7
2 parents 6fe87e9 + af58a27 commit bc3bdc6

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
"symfony/phpunit-bridge": "^5.0 || ^6.0"
4141
},
4242
"conflict": {
43-
"toflar/psr6-symfony-http-cache-store": "<2.2.1",
44-
"symfony/http-foundation": ">=7"
43+
"toflar/psr6-symfony-http-cache-store": "<2.2.1"
4544
},
4645
"suggest": {
4746
"friendsofsymfony/http-cache-bundle": "For integration with the Symfony framework",

src/SymfonyCache/AccessControlledListener.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\RequestMatcher;
17+
use Symfony\Component\HttpFoundation\RequestMatcher\IpsRequestMatcher;
1718
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

@@ -26,7 +27,7 @@
2627
abstract class AccessControlledListener implements EventSubscriberInterface
2728
{
2829
/**
29-
* @var RequestMatcher
30+
* @var RequestMatcherInterface
3031
*/
3132
private $clientMatcher;
3233

@@ -54,7 +55,10 @@ public function __construct(array $options = [])
5455
throw new \InvalidArgumentException('You may not set both a request matcher and an IP.');
5556
}
5657
if (!$clientMatcher) {
57-
$clientMatcher = new RequestMatcher(null, null, null, $options['client_ips'] ?: '127.0.0.1');
58+
$clientMatcher = class_exists(IpsRequestMatcher::class)
59+
? new IpsRequestMatcher($options['client_ips'] ?: '127.0.0.1')
60+
: new RequestMatcher(null, null, null, $options['client_ips'] ?: '127.0.0.1')
61+
;
5862
}
5963

6064
$this->clientMatcher = $clientMatcher;

tests/Unit/SymfonyCache/PurgeListenerTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use PHPUnit\Framework\TestCase;
2020
use Symfony\Component\HttpFoundation\Request;
2121
use Symfony\Component\HttpFoundation\RequestMatcher;
22+
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
23+
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
2224
use Symfony\Component\HttpFoundation\Response;
2325
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
2426
use Toflar\Psr6HttpCacheStore\Psr6Store;
@@ -35,7 +37,7 @@ public function testConstructorOverspecified()
3537
$this->expectException(\InvalidArgumentException::class);
3638
$this->expectExceptionMessage('You may not set both a request matcher and an IP');
3739
new PurgeListener([
38-
'client_matcher' => new RequestMatcher('/forbidden'),
40+
'client_matcher' => $this->createRequestMatcher('/forbidden'),
3941
'client_ips' => ['1.2.3.4'],
4042
]);
4143
}
@@ -129,7 +131,7 @@ public function testPurgeForbiddenMatcher()
129131
{
130132
$kernel = $this->getUnusedKernelMock();
131133

132-
$matcher = new RequestMatcher('/forbidden');
134+
$matcher = $this->createRequestMatcher('/forbidden');
133135
$purgeListener = new PurgeListener(['client_matcher' => $matcher]);
134136
$request = Request::create('http://example.com/foo', 'PURGE');
135137
$event = new CacheEvent($kernel, $request);
@@ -162,7 +164,7 @@ public function testPurgeForbiddenIp()
162164
public function testOtherMethod()
163165
{
164166
$kernel = $this->getUnusedKernelMock();
165-
$matcher = \Mockery::mock(RequestMatcher::class)
167+
$matcher = \Mockery::mock(RequestMatcherInterface::class)
166168
->shouldNotReceive('isRequestAllowed')
167169
->getMock();
168170

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

189+
private function createRequestMatcher(string $path): RequestMatcherInterface
190+
{
191+
return class_exists(PathRequestMatcher::class)
192+
? new PathRequestMatcher($path)
193+
: new RequestMatcher($path)
194+
;
195+
}
196+
187197
/**
188198
* @return CacheInvalidation|MockInterface
189199
*/

tests/Unit/SymfonyCache/RefreshListenerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPUnit\Framework\TestCase;
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\RequestMatcher;
21+
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
2122
use Symfony\Component\HttpFoundation\Response;
2223

2324
class RefreshListenerTest extends TestCase
@@ -42,7 +43,7 @@ public function testRefreshAllowed()
4243
$this->kernel->expects($this->once())
4344
->method('fetch')
4445
->with($request)
45-
->will($this->returnValue($response))
46+
->willReturn($response)
4647
;
4748

4849
$refreshListener = new RefreshListener();
@@ -57,7 +58,10 @@ public function testRefreshForbiddenMatcher()
5758
->method('fetch')
5859
;
5960

60-
$matcher = new RequestMatcher('/forbidden');
61+
$matcher = class_exists(PathRequestMatcher::class)
62+
? new PathRequestMatcher('/forbidden')
63+
: new RequestMatcher('/forbidden')
64+
;
6165
$refreshListener = new RefreshListener(['client_matcher' => $matcher]);
6266
$request = Request::create('http://example.com/foo');
6367
$request->headers->addCacheControlDirective('no-cache');

0 commit comments

Comments
 (0)