From 12c3c2e10e49b036796c85246ce000fcd950f093 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Mon, 25 Mar 2024 18:06:55 +0100 Subject: [PATCH] refactor to phpunit 10 event system --- composer.json | 4 +- doc/testing-your-application.rst | 12 +- phpunit.xml.dist | 48 +++--- .../EventDispatchingHttpCacheTestCase.php | 18 +- src/Test/Legacy/WebServerListener.php | 145 ---------------- src/Test/Legacy/WebServerListener6.php | 153 ----------------- src/Test/WebServerListener.php | 162 ------------------ ...tenerTrait.php => WebServerSubscriber.php} | 101 ++++------- src/Test/WebServerSubscriberExtension.php | 25 +++ tests/Unit/CacheInvalidatorTest.php | 2 +- tests/Unit/ProxyClient/HttpDispatcherTest.php | 2 +- .../ProxyClient/MultiplexerClientTest.php | 4 +- tests/Unit/ProxyClient/SymfonyTest.php | 48 ++---- .../SymfonyCache/UserContextListenerTest.php | 12 +- .../MaxHeaderValueLengthFormatterTest.php | 2 +- ...hp => AbstractCacheConstraintTestCase.php} | 2 +- ...t.php => IsCacheHitConstraintTestCase.php} | 2 +- ....php => IsCacheMissConstraintTestCase.php} | 2 +- tests/Unit/Test/Proxy/VarnishProxyTest.php | 2 +- 19 files changed, 138 insertions(+), 608 deletions(-) delete mode 100644 src/Test/Legacy/WebServerListener.php delete mode 100644 src/Test/Legacy/WebServerListener6.php delete mode 100644 src/Test/WebServerListener.php rename src/Test/{WebServerListenerTrait.php => WebServerSubscriber.php} (58%) create mode 100644 src/Test/WebServerSubscriberExtension.php rename tests/Unit/Test/PHPUnit/{AbstractCacheConstraintTest.php => AbstractCacheConstraintTestCase.php} (92%) rename tests/Unit/Test/PHPUnit/{IsCacheHitConstraintTest.php => IsCacheHitConstraintTestCase.php} (96%) rename tests/Unit/Test/PHPUnit/{IsCacheMissConstraintTest.php => IsCacheMissConstraintTestCase.php} (93%) diff --git a/composer.json b/composer.json index 50577c411..782fd2b9c 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/doc/testing-your-application.rst b/doc/testing-your-application.rst index 231d2d362..eb15d6ae1 100644 --- a/doc/testing-your-application.rst +++ b/doc/testing-your-application.rst @@ -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: - + :language: xml - :start-after: - :end-before: + :start-after: + :end-before: :append: - + Then set the ``webserver`` group on your test to start PHP’s web server before diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 30158d098..45a42c5c1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,31 +1,31 @@ - - - - - ./tests/ - - - - - ./src - - + + + ./tests/ + + - - - + + + - - - - - + + + + + + + + + ./src + + diff --git a/src/Test/EventDispatchingHttpCacheTestCase.php b/src/Test/EventDispatchingHttpCacheTestCase.php index c59e79990..7932b184f 100644 --- a/src/Test/EventDispatchingHttpCacheTestCase.php +++ b/src/Test/EventDispatchingHttpCacheTestCase.php @@ -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; @@ -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); @@ -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') @@ -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()) @@ -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') @@ -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()) @@ -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); @@ -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); @@ -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) @@ -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') diff --git a/src/Test/Legacy/WebServerListener.php b/src/Test/Legacy/WebServerListener.php deleted file mode 100644 index 69fe98d93..000000000 --- a/src/Test/Legacy/WebServerListener.php +++ /dev/null @@ -1,145 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\HttpCache\Test\Legacy; - -use FOS\HttpCache\Test\WebServerListenerTrait; -use PHPUnit\Framework\TestListener; - -/** - * A PHPUnit test listener that starts and stops the PHP built-in web server. - * - * This legacy version is for PHPUnit 5.x (min 5.4.4 required, due to FC layer). - * - * This listener is configured with a couple of constants from the phpunit.xml - * file. To define constants in the phpunit file, use this syntax: - * - * - * - * - * WEB_SERVER_HOSTNAME host name of the web server (required) - * WEB_SERVER_PORT port to listen on (required) - * WEB_SERVER_DOCROOT path to the document root for the server (required) - */ -class WebServerListener implements TestListener -{ - /** @var WebServerListenerTrait */ - private $trait; - - public function __construct() - { - $this->trait = new WebServerListenerTrait(); - } - - /** - * Make sure the PHP built-in web server is running for tests with group - * 'webserver'. - */ - public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) - { - $this->trait->startTestSuite($suite); - } - - /** - * We don't need these. - */ - public function endTestSuite(\PHPUnit_Framework_TestSuite $suite) - { - } - - public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time) - { - } - - public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) - { - } - - public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) - { - } - - public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) - { - } - - public function startTest(\PHPUnit_Framework_Test $test) - { - } - - public function endTest(\PHPUnit_Framework_Test $test, $time) - { - } - - public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) - { - } - - /** - * Get web server hostname. - * - * @return string - * - * @throws \Exception - */ - protected function getHostName() - { - return $this->trait->getHostName(); - } - - /** - * Get web server port. - * - * @return int - * - * @throws \Exception - */ - protected function getPort() - { - return $this->trait->getPort(); - } - - /** - * Get web server port. - * - * @return int - * - * @throws \Exception - */ - protected function getDocRoot() - { - return $this->trait->getDocRoot(); - } - - /** - * Start PHP built-in web server. - * - * @return int PID - */ - protected function startPhpWebServer() - { - return $this->trait->startPhpWebServer(); - } - - /** - * Wait for caching proxy to be started up and reachable. - * - * @param string $ip - * @param int $port - * @param int $timeout Timeout in milliseconds - * - * @throws \RuntimeException If proxy is not reachable within timeout - */ - protected function waitFor($ip, $port, $timeout) - { - $this->trait->waitFor($ip, $port, $timeout); - } -} diff --git a/src/Test/Legacy/WebServerListener6.php b/src/Test/Legacy/WebServerListener6.php deleted file mode 100644 index 4689e5bab..000000000 --- a/src/Test/Legacy/WebServerListener6.php +++ /dev/null @@ -1,153 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\HttpCache\Test\Legacy; - -use FOS\HttpCache\Test\WebServerListenerTrait; -use PHPUnit\Framework\AssertionFailedError; -use PHPUnit\Framework\Test; -use PHPUnit\Framework\TestListener; -use PHPUnit\Framework\TestSuite; -use PHPUnit\Framework\Warning; - -/** - * A PHPUnit test listener that starts and stops the PHP built-in web server. - * - * This legacy version is for PHPUnit 6.x. - * - * This listener is configured with a couple of constants from the phpunit.xml - * file. To define constants in the phpunit file, use this syntax: - * - * - * - * - * WEB_SERVER_HOSTNAME host name of the web server (required) - * WEB_SERVER_PORT port to listen on (required) - * WEB_SERVER_DOCROOT path to the document root for the server (required) - */ -class WebServerListener6 implements TestListener -{ - /** @var WebServerListenerTrait */ - private $trait; - - public function __construct() - { - $this->trait = new WebServerListenerTrait(); - } - - /** - * Make sure the PHP built-in web server is running for tests with group - * 'webserver'. - */ - public function startTestSuite(TestSuite $suite) - { - $this->trait->startTestSuite($suite); - } - - /** - * We don't need these. - */ - public function endTestSuite(TestSuite $suite) - { - } - - public function addError(Test $test, \Exception $e, $time) - { - } - - public function addFailure(Test $test, AssertionFailedError $e, $time) - { - } - - public function addIncompleteTest(Test $test, \Exception $e, $time) - { - } - - public function addSkippedTest(Test $test, \Exception $e, $time) - { - } - - public function startTest(Test $test) - { - } - - public function endTest(Test $test, $time) - { - } - - public function addRiskyTest(Test $test, \Exception $e, $time) - { - } - - public function addWarning(Test $test, Warning $e, $time) - { - } - - /** - * Get web server hostname. - * - * @return string - * - * @throws \Exception - */ - protected function getHostName() - { - return $this->trait->getHostName(); - } - - /** - * Get web server port. - * - * @return int - * - * @throws \Exception - */ - protected function getPort() - { - return $this->trait->getPort(); - } - - /** - * Get web server port. - * - * @return int - * - * @throws \Exception - */ - protected function getDocRoot() - { - return $this->trait->getDocRoot(); - } - - /** - * Start PHP built-in web server. - * - * @return int PID - */ - protected function startPhpWebServer() - { - return $this->trait->startPhpWebServer(); - } - - /** - * Wait for caching proxy to be started up and reachable. - * - * @param string $ip - * @param int $port - * @param int $timeout Timeout in milliseconds - * - * @throws \RuntimeException If proxy is not reachable within timeout - */ - protected function waitFor($ip, $port, $timeout) - { - $this->trait->waitFor($ip, $port, $timeout); - } -} diff --git a/src/Test/WebServerListener.php b/src/Test/WebServerListener.php deleted file mode 100644 index c630bd017..000000000 --- a/src/Test/WebServerListener.php +++ /dev/null @@ -1,162 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\HttpCache\Test; - -use FOS\HttpCache\Test\Legacy\WebServerListener6; -use PHPUnit\Framework\AssertionFailedError; -use PHPUnit\Framework\Test; -use PHPUnit\Framework\TestListener as TestListenerInterface; -use PHPUnit\Framework\TestSuite; -use PHPUnit\Framework\Warning; -use PHPUnit\Runner\Version; - -if (class_exists(\PHPUnit_Runner_Version::class) && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { - /* - * Using an early return instead of a else does not work when using the PHPUnit phar due to some weird PHP behavior - * (the class gets defined without executing the code before it and so the definition is not properly conditional) - */ - class_alias('FOS\HttpCache\Test\Legacy\WebServerListener', 'FOS\HttpCache\Test\WebServerListener'); -} elseif (version_compare(Version::id(), '7.0.0', '<')) { - class_alias(WebServerListener6::class, 'FOS\HttpCache\Test\WebServerListener'); -} else { - /** - * A PHPUnit test listener that starts and stops the PHP built-in web server. - * - * This listener is configured with a couple of constants from the phpunit.xml - * file. To define constants in the phpunit file, use this syntax: - * - * - * - * - * WEB_SERVER_HOSTNAME host name of the web server (required) - * WEB_SERVER_PORT port to listen on (required) - * WEB_SERVER_DOCROOT path to the document root for the server (required) - */ - class WebServerListener implements TestListenerInterface - { - /** @var WebServerListenerTrait */ - private $trait; - - public function __construct() - { - $this->trait = new WebServerListenerTrait(); - } - - /** - * Make sure the PHP built-in web server is running for tests with group - * 'webserver'. - */ - public function startTestSuite(TestSuite $suite): void - { - $this->trait->startTestSuite($suite); - } - - /** - * We don't need these. - */ - public function endTestSuite(TestSuite $suite): void - { - } - - public function addError(Test $test, \Throwable $e, float $time): void - { - } - - public function addFailure(Test $test, AssertionFailedError $e, float $time): void - { - } - - public function addIncompleteTest(Test $test, \Throwable $e, float $time): void - { - } - - public function addSkippedTest(Test $test, \Throwable $e, float $time): void - { - } - - public function startTest(Test $test): void - { - } - - public function endTest(Test $test, float $time): void - { - } - - public function addRiskyTest(Test $test, \Throwable $e, float $time): void - { - } - - public function addWarning(Test $test, Warning $e, float $time): void - { - } - - /** - * Get web server hostname. - * - * @return string - * - * @throws \Exception - */ - protected function getHostName() - { - return $this->trait->getHostName(); - } - - /** - * Get web server port. - * - * @return int - * - * @throws \Exception - */ - protected function getPort() - { - return $this->trait->getPort(); - } - - /** - * Get web server port. - * - * @return int - * - * @throws \Exception - */ - protected function getDocRoot() - { - return $this->trait->getDocRoot(); - } - - /** - * Start PHP built-in web server. - * - * @return int PID - */ - protected function startPhpWebServer() - { - return $this->trait->startPhpWebServer(); - } - - /** - * Wait for caching proxy to be started up and reachable. - * - * @param string $ip - * @param int $port - * @param int $timeout Timeout in milliseconds - * - * @throws \RuntimeException If proxy is not reachable within timeout - */ - protected function waitFor($ip, $port, $timeout) - { - $this->trait->waitFor($ip, $port, $timeout); - } - } -} diff --git a/src/Test/WebServerListenerTrait.php b/src/Test/WebServerSubscriber.php similarity index 58% rename from src/Test/WebServerListenerTrait.php rename to src/Test/WebServerSubscriber.php index 895708f22..2e9fbc78a 100644 --- a/src/Test/WebServerListenerTrait.php +++ b/src/Test/WebServerSubscriber.php @@ -11,49 +11,51 @@ namespace FOS\HttpCache\Test; -/** - * This fake trait is used to have the same code and behavior between WebServerListener and its legacy version. - */ -class WebServerListenerTrait +use PHPUnit\Event\TestRunner\ExecutionStarted; +use PHPUnit\Event\TestRunner\ExecutionStartedSubscriber; + +class WebServerSubscriber implements ExecutionStartedSubscriber { /** * PHP web server PID. - * - * @var int */ - protected $pid; + private int $pid; - /** - * Make sure the PHP built-in web server is running for tests with group - * 'webserver'. - */ - public function startTestSuite($suite) + public function notify(ExecutionStarted $event): void { - // Only run on PHP >= 5.4 as PHP below that and HHVM don't have a - // built-in web server - if (defined('HHVM_VERSION')) { - return; - } - - if (null !== $this->pid || !in_array('webserver', $suite->getGroups())) { + if (isset($this->pid)) { + // TODO: can we detect if 'webserver' is in the list of groups of the test suite? return; } $this->pid = $pid = $this->startPhpWebServer(); - register_shutdown_function(function () use ($pid) { + register_shutdown_function(static function () use ($pid): void { exec('kill '.$pid); }); } /** - * Get web server hostname. - * - * @return string + * Start PHP built-in web server. * - * @throws \Exception + * @return int PID */ - public function getHostName() + public function startPhpWebServer(): int + { + $command = sprintf( + 'php -S %s:%d -t %s >/dev/null 2>&1 & echo $!', + '127.0.0.1', + $this->getPort(), + $this->getDocRoot() + ); + exec($command, $output); + + $this->waitFor($this->getHostName(), (int) $this->getPort(), 2000); + + return $output[0]; + } + + public function getHostName(): string { if (!defined('WEB_SERVER_HOSTNAME')) { throw new \Exception('Set WEB_SERVER_HOSTNAME in your phpunit.xml'); @@ -62,14 +64,7 @@ public function getHostName() return WEB_SERVER_HOSTNAME; } - /** - * Get web server port. - * - * @return int - * - * @throws \Exception - */ - public function getPort() + public function getPort(): string { if (!defined('WEB_SERVER_PORT')) { throw new \Exception('Set WEB_SERVER_PORT in your phpunit.xml'); @@ -78,14 +73,7 @@ public function getPort() return WEB_SERVER_PORT; } - /** - * Get web server port. - * - * @return int - * - * @throws \Exception - */ - public function getDocRoot() + public function getDocRoot(): string { if (!defined('WEB_SERVER_DOCROOT')) { throw new \Exception('Set WEB_SERVER_DOCROOT in your phpunit.xml'); @@ -94,39 +82,22 @@ public function getDocRoot() return WEB_SERVER_DOCROOT; } - /** - * Start PHP built-in web server. - * - * @return int PID - */ - public function startPhpWebServer() - { - $command = sprintf( - 'php -S %s:%d -t %s >/dev/null 2>&1 & echo $!', - '127.0.0.1', - $this->getPort(), - $this->getDocRoot() - ); - exec($command, $output); - - $this->waitFor($this->getHostName(), $this->getPort(), 2000); - - return $output[0]; - } - /** * Wait for caching proxy to be started up and reachable. * - * @param string $ip - * @param int $port - * @param int $timeout Timeout in milliseconds + * @param int $timeout Timeout in milliseconds * * @throws \RuntimeException If proxy is not reachable within timeout */ - public function waitFor($ip, $port, $timeout) + public function waitFor(string $ip, int $port, int $timeout): void { + echo "Starting webserver at $ip:$port"; + for ($i = 0; $i < $timeout; ++$i) { + echo '.'; if (@fsockopen($ip, $port)) { + echo " done.\n\n"; + return; } diff --git a/src/Test/WebServerSubscriberExtension.php b/src/Test/WebServerSubscriberExtension.php new file mode 100644 index 000000000..d22d5cfa6 --- /dev/null +++ b/src/Test/WebServerSubscriberExtension.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\HttpCache\Test; + +use PHPUnit\Runner\Extension\Extension; +use PHPUnit\Runner\Extension\Facade; +use PHPUnit\Runner\Extension\ParameterCollection; +use PHPUnit\TextUI\Configuration\Configuration; + +class WebServerSubscriberExtension implements Extension +{ + public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void + { + $facade->registerSubscriber(new WebServerSubscriber()); + } +} diff --git a/tests/Unit/CacheInvalidatorTest.php b/tests/Unit/CacheInvalidatorTest.php index 151343722..4fe02c46a 100644 --- a/tests/Unit/CacheInvalidatorTest.php +++ b/tests/Unit/CacheInvalidatorTest.php @@ -157,7 +157,7 @@ public function testInvalidateRegex(): void $cacheInvalidator->invalidateRegex('/a', 'b', ['example.com']); } - public function provideOperations(): iterable + public static function provideOperations(): iterable { yield from [ ['invalidatePath', '/'], diff --git a/tests/Unit/ProxyClient/HttpDispatcherTest.php b/tests/Unit/ProxyClient/HttpDispatcherTest.php index ce26f96c5..ce44ebcd1 100644 --- a/tests/Unit/ProxyClient/HttpDispatcherTest.php +++ b/tests/Unit/ProxyClient/HttpDispatcherTest.php @@ -97,7 +97,7 @@ private function doTestException(\Exception $exception, string $type, string $me $httpDispatcher->flush(); } - public function exceptionProvider(): array + public static function exceptionProvider(): array { /** @var RequestInterface $request */ $request = \Mockery::mock(RequestInterface::class) diff --git a/tests/Unit/ProxyClient/MultiplexerClientTest.php b/tests/Unit/ProxyClient/MultiplexerClientTest.php index ca42ca92c..dbd9880ad 100644 --- a/tests/Unit/ProxyClient/MultiplexerClientTest.php +++ b/tests/Unit/ProxyClient/MultiplexerClientTest.php @@ -163,11 +163,11 @@ public function testClear(): void $this->assertSame($multiplexer, $multiplexer->clear()); } - public function provideInvalidClient(): array + public static function provideInvalidClient(): array { return [ [['this-is-not-an-object']], - [[$this]], + [[new \stdClass()]], ]; } diff --git a/tests/Unit/ProxyClient/SymfonyTest.php b/tests/Unit/ProxyClient/SymfonyTest.php index 2b6fe0970..5635eda30 100644 --- a/tests/Unit/ProxyClient/SymfonyTest.php +++ b/tests/Unit/ProxyClient/SymfonyTest.php @@ -74,40 +74,28 @@ function (RequestInterface $request) { public function testInvalidateTagsWithALotOfTags(): void { + $tags = [ + 'foobar,foobar1,foobar2,foobar3,foobar4,foobar5,foobar6,foobar7,foobar8,foobar9,foobar10' => true, + 'foobar11,foobar12,foobar13,foobar14,foobar15,foobar16,foobar17,foobar18,foobar19,foobar20,foobar21' => true, + 'foobar22,foobar23,foobar24,foobar25' => true, + ]; + /** @var HttpDispatcher&MockObject $dispatcher */ $dispatcher = $this->createMock(HttpDispatcher::class); $dispatcher ->expects($this->exactly(3)) ->method('invalidate') - ->withConsecutive( - [ - $this->callback(function (RequestInterface $request) { - $this->assertEquals('PURGETAGS', $request->getMethod()); - $this->assertEquals('foobar,foobar1,foobar2,foobar3,foobar4,foobar5,foobar6,foobar7,foobar8,foobar9,foobar10', $request->getHeaderLine('X-Cache-Tags')); - - return true; - }), - false, - ], - [ - $this->callback(function (RequestInterface $request) { - $this->assertEquals('PURGETAGS', $request->getMethod()); - $this->assertEquals('foobar11,foobar12,foobar13,foobar14,foobar15,foobar16,foobar17,foobar18,foobar19,foobar20,foobar21', $request->getHeaderLine('X-Cache-Tags')); - - return true; - }), - false, - ], - [ - $this->callback(function (RequestInterface $request) { - $this->assertEquals('PURGETAGS', $request->getMethod()); - $this->assertEquals('foobar22,foobar23,foobar24,foobar25', $request->getHeaderLine('X-Cache-Tags')); - - return true; - }), - false, - ] - ); + ->with($this->callback(function (RequestInterface $request) use (&$tags): bool { + $this->assertSame('PURGETAGS', $request->getMethod()); + $purgeTags = $request->getHeaderLine('X-Cache-Tags'); + if (!array_key_exists($purgeTags, $tags)) { + $this->fail("Unexpected request to purge $purgeTags"); + } + unset($tags[$purgeTags]); + + return true; + })) + ; $symfony = new Symfony($dispatcher, ['header_length' => 100]); @@ -139,6 +127,8 @@ public function testInvalidateTagsWithALotOfTags(): void 'foobar24', 'foobar25', ]); + + $this->assertCount(0, $tags); } public function testClear(): void diff --git a/tests/Unit/SymfonyCache/UserContextListenerTest.php b/tests/Unit/SymfonyCache/UserContextListenerTest.php index 49bb52607..1bb733397 100644 --- a/tests/Unit/SymfonyCache/UserContextListenerTest.php +++ b/tests/Unit/SymfonyCache/UserContextListenerTest.php @@ -35,7 +35,7 @@ public function setUp(): void /** * UserContextListener default options to simulate the correct headers. */ - public function provideConfigOptions(): array + public static function provideConfigOptions(): array { $userContextListener = new UserContextListener(); $ref = new \ReflectionObject($userContextListener); @@ -119,8 +119,9 @@ public function testUserHashAnonymous(array $arg, array $options): void // Just avoid the response to modify the request object, otherwise it's impossible to test objects equality. /** @var Response&MockObject $hashResponse */ $hashResponse = $this->getMockBuilder(Response::class) - ->setMethods(['prepare']) - ->getMock(); + ->onlyMethods(['prepare']) + ->getMock() + ; $hashResponse->headers->set($options['user_hash_header'], $expectedContextHash); $that = $this; @@ -190,8 +191,9 @@ public function testUserHashUserWithSession(array $arg, array $options): void // Just avoid the response to modify the request object, otherwise it's impossible to test objects equality. /** @var Response&MockObject $hashResponse */ $hashResponse = $this->getMockBuilder(Response::class) - ->setMethods(['prepare']) - ->getMock(); + ->onlyMethods(['prepare']) + ->getMock() + ; $hashResponse->headers->set($options['user_hash_header'], $expectedContextHash); $that = $this; diff --git a/tests/Unit/TagHeaderFormatter/MaxHeaderValueLengthFormatterTest.php b/tests/Unit/TagHeaderFormatter/MaxHeaderValueLengthFormatterTest.php index b366efc9d..bcb970ce7 100644 --- a/tests/Unit/TagHeaderFormatter/MaxHeaderValueLengthFormatterTest.php +++ b/tests/Unit/TagHeaderFormatter/MaxHeaderValueLengthFormatterTest.php @@ -60,7 +60,7 @@ private function getFormatter(int $maxLength): MaxHeaderValueLengthFormatter ); } - public function tooLongProvider(): array + public static function tooLongProvider(): array { return [ [3, ['foo', 'bar', 'baz'], ['foo', 'bar', 'baz']], diff --git a/tests/Unit/Test/PHPUnit/AbstractCacheConstraintTest.php b/tests/Unit/Test/PHPUnit/AbstractCacheConstraintTestCase.php similarity index 92% rename from tests/Unit/Test/PHPUnit/AbstractCacheConstraintTest.php rename to tests/Unit/Test/PHPUnit/AbstractCacheConstraintTestCase.php index b335c95cc..62a34c890 100644 --- a/tests/Unit/Test/PHPUnit/AbstractCacheConstraintTest.php +++ b/tests/Unit/Test/PHPUnit/AbstractCacheConstraintTestCase.php @@ -16,7 +16,7 @@ use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; -abstract class AbstractCacheConstraintTest extends TestCase +abstract class AbstractCacheConstraintTestCase extends TestCase { use MockeryPHPUnitIntegration; diff --git a/tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php b/tests/Unit/Test/PHPUnit/IsCacheHitConstraintTestCase.php similarity index 96% rename from tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php rename to tests/Unit/Test/PHPUnit/IsCacheHitConstraintTestCase.php index d1e73cd28..195df43a0 100644 --- a/tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php +++ b/tests/Unit/Test/PHPUnit/IsCacheHitConstraintTestCase.php @@ -15,7 +15,7 @@ use GuzzleHttp\Psr7\Stream; use PHPUnit\Framework\ExpectationFailedException; -class IsCacheHitConstraintTest extends AbstractCacheConstraintTest +class IsCacheHitConstraintTestCase extends AbstractCacheConstraintTestCase { private IsCacheHitConstraint $constraint; diff --git a/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php b/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTestCase.php similarity index 93% rename from tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php rename to tests/Unit/Test/PHPUnit/IsCacheMissConstraintTestCase.php index bb00c09b9..e86c30514 100644 --- a/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php +++ b/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTestCase.php @@ -14,7 +14,7 @@ use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint; use PHPUnit\Framework\ExpectationFailedException; -class IsCacheMissConstraintTest extends AbstractCacheConstraintTest +class IsCacheMissConstraintTestCase extends AbstractCacheConstraintTestCase { private IsCacheMissConstraint $constraint; diff --git a/tests/Unit/Test/Proxy/VarnishProxyTest.php b/tests/Unit/Test/Proxy/VarnishProxyTest.php index f15ed513d..44178fd41 100644 --- a/tests/Unit/Test/Proxy/VarnishProxyTest.php +++ b/tests/Unit/Test/Proxy/VarnishProxyTest.php @@ -24,7 +24,7 @@ public function testInvalidConfigFileThrowsException(): void new VarnishProxy('nope.vcl'); } - public function allowInlineFlagProvider(): array + public static function allowInlineFlagProvider(): array { return [[true], [false]]; }