Skip to content

Commit d06ce3f

Browse files
committed
refactor to phpunit 10 event system
1 parent ce639ba commit d06ce3f

29 files changed

+168
-650
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
"php-http/mock-client": "^1.6.0",
3838
"symfony/process": "^6.4|| ^7.0",
3939
"symfony/http-kernel": "^6.4|| ^7.0",
40-
"phpunit/phpunit": "^9"
40+
"phpunit/phpunit": "^10.5"
4141
},
4242
"conflict": {
4343
"toflar/psr6-symfony-http-cache-store": "<2.2.1",
44-
"phpunit/phpunit": "<8"
44+
"phpunit/phpunit": "<10"
4545
},
4646
"suggest": {
4747
"friendsofsymfony/http-cache-bundle": "For integration with the Symfony framework",

doc/testing-your-application.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ Web Server
3636

3737
You will need to run a web server to provide the PHP application you want to
3838
test. The test cases only handle running the proxy server. It’s easiest to
39-
use PHP’s built in web server. Include the WebServerListener in your
40-
``phpunit.xml``:
39+
use PHP’s built in web server. Include the WebServerSubscriber in your
40+
``phpunit.xml``, either using the extension provided by this package or your own:
4141

4242
.. literalinclude:: ../phpunit.xml.dist
4343
:prepend:
4444
<?xml version="1.0" encoding="UTF-8"?>
4545
<phpunit>
46-
<listeners>
46+
<extensions>
4747
:language: xml
48-
:start-after: <listeners>
49-
:end-before: </listeners>
48+
:start-after: <extensions>
49+
:end-before: </extensions>
5050
:append:
51-
</listeners>
51+
</extensions>
5252
</phpunit>
5353

5454
Then set the ``webserver`` group on your test to start PHP’s web server before

phpunit.xml.dist

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit bootstrap="./tests/bootstrap.php"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
5+
bootstrap="./tests/bootstrap.php"
6+
colors="true"
87
>
9-
<testsuites>
10-
<testsuite name="FOSHttpCache tests">
11-
<directory suffix="Test.php">./tests/</directory>
12-
</testsuite>
13-
</testsuites>
148

15-
<filter>
16-
<whitelist>
17-
<directory>./src</directory>
18-
</whitelist>
19-
</filter>
9+
<testsuites>
10+
<testsuite name="FOSHttpCache tests">
11+
<directory suffix="Test.php">./tests/</directory>
12+
</testsuite>
13+
</testsuites>
2014

21-
<listeners>
22-
<listener class="\FOS\HttpCache\Test\WebServerListener" />
23-
</listeners>
15+
<extensions>
16+
<bootstrap class="FOS\HttpCache\Test\WebServerSubscriberExtension"/>
17+
</extensions>
2418

2519
<php>
26-
<const name="NGINX_FILE" value="./tests/Functional/Fixtures/nginx/fos.conf" />
27-
<const name="WEB_SERVER_HOSTNAME" value="localhost" />
28-
<const name="WEB_SERVER_PORT" value="8080" />
29-
<const name="WEB_SERVER_DOCROOT" value="./tests/Functional/Fixtures/web" />
30-
</php>
20+
<const name="NGINX_FILE" value="./tests/Functional/Fixtures/nginx/fos.conf"/>
21+
<const name="WEB_SERVER_HOSTNAME" value="localhost"/>
22+
<const name="WEB_SERVER_PORT" value="8080"/>
23+
<const name="WEB_SERVER_DOCROOT" value="./tests/Functional/Fixtures/web"/>
24+
</php>
25+
26+
<source>
27+
<include>
28+
<directory>./src</directory>
29+
</include>
30+
</source>
3131
</phpunit>

src/Test/EventDispatchingHttpCacheTestCase.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use FOS\HttpCache\SymfonyCache\CacheEvent;
1515
use FOS\HttpCache\SymfonyCache\CacheInvalidation;
16-
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
1716
use FOS\HttpCache\SymfonyCache\Events;
1817
use PHPUnit\Framework\MockObject\MockObject;
1918
use PHPUnit\Framework\TestCase;
@@ -41,14 +40,9 @@ abstract protected function getCacheClass(): string;
4140
*
4241
* @param string[] $mockedMethods List of methods to mock
4342
*/
44-
protected function getHttpCachePartialMock(?array $mockedMethods = null): MockObject|CacheInvalidation|EventDispatchingHttpCache
43+
protected function getHttpCachePartialMock(array $mockedMethods = []): MockObject&CacheInvalidation
4544
{
46-
$mock = $this
47-
->getMockBuilder($this->getCacheClass())
48-
->setMethods($mockedMethods)
49-
->disableOriginalConstructor()
50-
->getMock()
51-
;
45+
$mock = $this->createPartialMock($this->getCacheClass(), $mockedMethods);
5246

5347
$this->assertInstanceOf(CacheInvalidation::class, $mock);
5448

@@ -103,6 +97,7 @@ public function testHandleCalled(): void
10397

10498
$httpCache = $this->getHttpCachePartialMock(['lookup']);
10599
$testListener = new TestListener($this, $httpCache, $request);
100+
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
106101
$httpCache->addSubscriber($testListener);
107102
$httpCache
108103
->method('lookup')
@@ -128,6 +123,7 @@ public function testPreHandleReturnEarly(): void
128123
$httpCache = $this->getHttpCachePartialMock(['lookup']);
129124
$testListener = new TestListener($this, $httpCache, $request);
130125
$testListener->preHandleResponse = $response;
126+
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
131127
$httpCache->addSubscriber($testListener);
132128
$httpCache
133129
->expects($this->never())
@@ -154,6 +150,7 @@ public function testPostHandleReturn(): void
154150
$httpCache = $this->getHttpCachePartialMock(['lookup']);
155151
$testListener = new TestListener($this, $httpCache, $request);
156152
$testListener->postHandleResponse = $postResponse;
153+
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
157154
$httpCache->addSubscriber($testListener);
158155
$httpCache
159156
->method('lookup')
@@ -182,6 +179,7 @@ public function testPostHandleAfterPreHandle(): void
182179
$testListener = new TestListener($this, $httpCache, $request);
183180
$testListener->preHandleResponse = $preResponse;
184181
$testListener->postHandleResponse = $postResponse;
182+
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
185183
$httpCache->addSubscriber($testListener);
186184
$httpCache
187185
->expects($this->never())
@@ -203,6 +201,7 @@ public function testPreStoreCalled(): void
203201

204202
$httpCache = $this->getHttpCachePartialMock();
205203
$testListener = new TestListener($this, $httpCache, $request);
204+
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
206205
$httpCache->addSubscriber($testListener);
207206

208207
$this->setStoreMock($httpCache, $request, $response);
@@ -225,6 +224,7 @@ public function testPreStoreResponse(): void
225224
$httpCache = $this->getHttpCachePartialMock();
226225
$testListener = new TestListener($this, $httpCache, $request);
227226
$testListener->preStoreResponse = $preStoreResponse;
227+
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
228228
$httpCache->addSubscriber($testListener);
229229

230230
$this->setStoreMock($httpCache, $request, $preStoreResponse);
@@ -247,6 +247,7 @@ public function testPreInvalidateCalled(): void
247247
$httpCache = $this->getHttpCachePartialMock(['pass']);
248248
$testListener = new TestListener($this, $httpCache, $request);
249249
$httpCache->addSubscriber($testListener);
250+
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
250251
$httpCache
251252
->method('pass')
252253
->with($request)
@@ -274,6 +275,7 @@ public function testPreInvalidateReturnEarly(): void
274275
$testListener = new TestListener($this, $httpCache, $request);
275276
$testListener->preInvalidateResponse = $response;
276277
$httpCache->addSubscriber($testListener);
278+
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
277279
$httpCache
278280
->expects($this->never())
279281
->method('pass')

src/Test/Legacy/WebServerListener.php

-145
This file was deleted.

0 commit comments

Comments
 (0)