Skip to content

Commit

Permalink
refactor to phpunit 10 event system
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Mar 26, 2024
1 parent ce639ba commit 12c3c2e
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 608 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
"php-http/mock-client": "^1.6.0",
"symfony/process": "^6.4|| ^7.0",
"symfony/http-kernel": "^6.4|| ^7.0",
"phpunit/phpunit": "^9"
"phpunit/phpunit": "^10.5"
},
"conflict": {
"toflar/psr6-symfony-http-cache-store": "<2.2.1",
"phpunit/phpunit": "<8"
"phpunit/phpunit": "<10"
},
"suggest": {
"friendsofsymfony/http-cache-bundle": "For integration with the Symfony framework",
Expand Down
12 changes: 6 additions & 6 deletions doc/testing-your-application.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ Web Server

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

.. literalinclude:: ../phpunit.xml.dist
:prepend:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<listeners>
<extensions>
:language: xml
:start-after: <listeners>
:end-before: </listeners>
:start-after: <extensions>
:end-before: </extensions>
:append:
</listeners>
</extensions>
</phpunit>

Then set the ``webserver`` group on your test to start PHP’s web server before
Expand Down
48 changes: 24 additions & 24 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="./tests/bootstrap.php"
colors="true"
>
<testsuites>
<testsuite name="FOSHttpCache tests">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
<testsuites>
<testsuite name="FOSHttpCache tests">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<listeners>
<listener class="\FOS\HttpCache\Test\WebServerListener" />
</listeners>
<extensions>
<bootstrap class="FOS\HttpCache\Test\WebServerSubscriberExtension"/>
</extensions>

<php>
<const name="NGINX_FILE" value="./tests/Functional/Fixtures/nginx/fos.conf" />
<const name="WEB_SERVER_HOSTNAME" value="localhost" />
<const name="WEB_SERVER_PORT" value="8080" />
<const name="WEB_SERVER_DOCROOT" value="./tests/Functional/Fixtures/web" />
</php>
<const name="NGINX_FILE" value="./tests/Functional/Fixtures/nginx/fos.conf"/>
<const name="WEB_SERVER_HOSTNAME" value="localhost"/>
<const name="WEB_SERVER_PORT" value="8080"/>
<const name="WEB_SERVER_DOCROOT" value="./tests/Functional/Fixtures/web"/>
</php>

<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
18 changes: 10 additions & 8 deletions src/Test/EventDispatchingHttpCacheTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use FOS\HttpCache\SymfonyCache\CacheEvent;
use FOS\HttpCache\SymfonyCache\CacheInvalidation;
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
use FOS\HttpCache\SymfonyCache\Events;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -41,14 +40,9 @@ abstract protected function getCacheClass(): string;
*
* @param string[] $mockedMethods List of methods to mock
*/
protected function getHttpCachePartialMock(?array $mockedMethods = null): MockObject|CacheInvalidation|EventDispatchingHttpCache
protected function getHttpCachePartialMock(array $mockedMethods = []): MockObject&CacheInvalidation
{
$mock = $this
->getMockBuilder($this->getCacheClass())
->setMethods($mockedMethods)
->disableOriginalConstructor()
->getMock()
;
$mock = $this->createPartialMock($this->getCacheClass(), $mockedMethods);

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

Expand Down Expand Up @@ -103,6 +97,7 @@ public function testHandleCalled(): void

$httpCache = $this->getHttpCachePartialMock(['lookup']);
$testListener = new TestListener($this, $httpCache, $request);
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
$httpCache->addSubscriber($testListener);
$httpCache
->method('lookup')
Expand All @@ -128,6 +123,7 @@ public function testPreHandleReturnEarly(): void
$httpCache = $this->getHttpCachePartialMock(['lookup']);
$testListener = new TestListener($this, $httpCache, $request);
$testListener->preHandleResponse = $response;
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
$httpCache->addSubscriber($testListener);
$httpCache
->expects($this->never())
Expand All @@ -154,6 +150,7 @@ public function testPostHandleReturn(): void
$httpCache = $this->getHttpCachePartialMock(['lookup']);
$testListener = new TestListener($this, $httpCache, $request);
$testListener->postHandleResponse = $postResponse;
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
$httpCache->addSubscriber($testListener);
$httpCache
->method('lookup')
Expand Down Expand Up @@ -182,6 +179,7 @@ public function testPostHandleAfterPreHandle(): void
$testListener = new TestListener($this, $httpCache, $request);
$testListener->preHandleResponse = $preResponse;
$testListener->postHandleResponse = $postResponse;
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
$httpCache->addSubscriber($testListener);
$httpCache
->expects($this->never())
Expand All @@ -203,6 +201,7 @@ public function testPreStoreCalled(): void

$httpCache = $this->getHttpCachePartialMock();
$testListener = new TestListener($this, $httpCache, $request);
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
$httpCache->addSubscriber($testListener);

$this->setStoreMock($httpCache, $request, $response);
Expand All @@ -225,6 +224,7 @@ public function testPreStoreResponse(): void
$httpCache = $this->getHttpCachePartialMock();
$testListener = new TestListener($this, $httpCache, $request);
$testListener->preStoreResponse = $preStoreResponse;
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
$httpCache->addSubscriber($testListener);

$this->setStoreMock($httpCache, $request, $preStoreResponse);
Expand All @@ -247,6 +247,7 @@ public function testPreInvalidateCalled(): void
$httpCache = $this->getHttpCachePartialMock(['pass']);
$testListener = new TestListener($this, $httpCache, $request);
$httpCache->addSubscriber($testListener);
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
$httpCache
->method('pass')
->with($request)
Expand Down Expand Up @@ -274,6 +275,7 @@ public function testPreInvalidateReturnEarly(): void
$testListener = new TestListener($this, $httpCache, $request);
$testListener->preInvalidateResponse = $response;
$httpCache->addSubscriber($testListener);
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
$httpCache
->expects($this->never())
->method('pass')
Expand Down
145 changes: 0 additions & 145 deletions src/Test/Legacy/WebServerListener.php

This file was deleted.

Loading

0 comments on commit 12c3c2e

Please sign in to comment.