From 880afee475318b2eceb20016d5a3ffac7196e1cf Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Mon, 29 Apr 2024 16:31:15 +0200 Subject: [PATCH] Remove calls to `TestCase::iniSet()` and calls to deprecated methods of `MockBuilder` --- Tests/EventListener/ErrorListenerTest.php | 30 +++++++++++-------- .../KernelForTestWithLoadClassCache.php | 19 ++++++++++++ .../MockableUploadFileWithClientSize.php | 22 ++++++++++++++ Tests/Fragment/FragmentHandlerTest.php | 8 ++--- Tests/Fragment/InlineFragmentRendererTest.php | 17 +++++++---- Tests/HttpKernelBrowserTest.php | 6 ++-- Tests/KernelTest.php | 11 +++---- Tests/UriSignerTest.php | 21 ++++++++----- 8 files changed, 93 insertions(+), 41 deletions(-) create mode 100644 Tests/Fixtures/KernelForTestWithLoadClassCache.php create mode 100644 Tests/Fixtures/MockableUploadFileWithClientSize.php diff --git a/Tests/EventListener/ErrorListenerTest.php b/Tests/EventListener/ErrorListenerTest.php index 623b50cd0c..e0505f7f41 100644 --- a/Tests/EventListener/ErrorListenerTest.php +++ b/Tests/EventListener/ErrorListenerTest.php @@ -54,21 +54,25 @@ public function testConstruct() */ public function testHandleWithoutLogger($event, $event2) { - $this->iniSet('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul'); - - $l = new ErrorListener('foo'); - $l->logKernelException($event); - $l->onKernelException($event); - - $this->assertEquals(new Response('foo'), $event->getResponse()); + $initialErrorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul'); try { - $l->logKernelException($event2); - $l->onKernelException($event2); - $this->fail('RuntimeException expected'); - } catch (\RuntimeException $e) { - $this->assertSame('bar', $e->getMessage()); - $this->assertSame('foo', $e->getPrevious()->getMessage()); + $l = new ErrorListener('foo'); + $l->logKernelException($event); + $l->onKernelException($event); + + $this->assertEquals(new Response('foo'), $event->getResponse()); + + try { + $l->logKernelException($event2); + $l->onKernelException($event2); + $this->fail('RuntimeException expected'); + } catch (\RuntimeException $e) { + $this->assertSame('bar', $e->getMessage()); + $this->assertSame('foo', $e->getPrevious()->getMessage()); + } + } finally { + ini_set('error_log', $initialErrorLog); } } diff --git a/Tests/Fixtures/KernelForTestWithLoadClassCache.php b/Tests/Fixtures/KernelForTestWithLoadClassCache.php new file mode 100644 index 0000000000..080953fe02 --- /dev/null +++ b/Tests/Fixtures/KernelForTestWithLoadClassCache.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\Fixtures; + +class KernelForTestWithLoadClassCache extends KernelForTest +{ + public function doLoadClassCache(): void + { + } +} diff --git a/Tests/Fixtures/MockableUploadFileWithClientSize.php b/Tests/Fixtures/MockableUploadFileWithClientSize.php new file mode 100644 index 0000000000..406f07a283 --- /dev/null +++ b/Tests/Fixtures/MockableUploadFileWithClientSize.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\Fixtures; + +use Symfony\Component\HttpFoundation\File\UploadedFile; + +class MockableUploadFileWithClientSize extends UploadedFile +{ + public function getClientSize(): int + { + return 0; + } +} diff --git a/Tests/Fragment/FragmentHandlerTest.php b/Tests/Fragment/FragmentHandlerTest.php index 1d0eb90bf6..8e083e14a2 100644 --- a/Tests/Fragment/FragmentHandlerTest.php +++ b/Tests/Fragment/FragmentHandlerTest.php @@ -43,14 +43,14 @@ public function testRenderWhenRendererDoesNotExist() public function testRenderWithUnknownRenderer() { $this->expectException(\InvalidArgumentException::class); - $handler = $this->getHandler($this->returnValue(new Response('foo'))); + $handler = $this->getHandler(new Response('foo')); $handler->render('/', 'bar'); } public function testDeliverWithUnsuccessfulResponse() { - $handler = $this->getHandler($this->returnValue(new Response('foo', 404))); + $handler = $this->getHandler(new Response('foo', 404)); try { $handler->render('/', 'foo'); $this->fail('->render() throws a \RuntimeException exception if response is not successful'); @@ -70,7 +70,7 @@ public function testRender() { $expectedRequest = Request::create('/'); $handler = $this->getHandler( - $this->returnValue(new Response('foo')), + new Response('foo'), [ '/', $this->callback(function (Request $request) use ($expectedRequest) { @@ -97,7 +97,7 @@ protected function getHandler($returnValue, $arguments = []) $e = $renderer ->expects($this->any()) ->method('render') - ->will($returnValue) + ->willReturn($returnValue) ; if ($arguments) { diff --git a/Tests/Fragment/InlineFragmentRendererTest.php b/Tests/Fragment/InlineFragmentRendererTest.php index fb22a1a094..168957c1c0 100644 --- a/Tests/Fragment/InlineFragmentRendererTest.php +++ b/Tests/Fragment/InlineFragmentRendererTest.php @@ -33,14 +33,14 @@ class InlineFragmentRendererTest extends TestCase { public function testRender() { - $strategy = new InlineFragmentRenderer($this->getKernel($this->returnValue(new Response('foo')))); + $strategy = new InlineFragmentRenderer($this->getKernel(new Response('foo'))); $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent()); } public function testRenderWithControllerReference() { - $strategy = new InlineFragmentRenderer($this->getKernel($this->returnValue(new Response('foo')))); + $strategy = new InlineFragmentRenderer($this->getKernel(new Response('foo'))); $this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', [], []), Request::create('/'))->getContent()); } @@ -81,7 +81,7 @@ public function testRenderExceptionNoIgnoreErrors() $dispatcher = $this->createMock(EventDispatcherInterface::class); $dispatcher->expects($this->never())->method('dispatch'); - $strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))), $dispatcher); + $strategy = new InlineFragmentRenderer($this->getKernel(new \RuntimeException('foo')), $dispatcher); $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent()); } @@ -89,7 +89,7 @@ public function testRenderExceptionNoIgnoreErrors() public function testRenderExceptionIgnoreErrors() { $exception = new \RuntimeException('foo'); - $kernel = $this->getKernel($this->throwException($exception)); + $kernel = $this->getKernel($exception); $request = Request::create('/'); $expectedEvent = new ExceptionEvent($kernel, $request, $kernel::SUB_REQUEST, $exception); $dispatcher = $this->createMock(EventDispatcherInterface::class); @@ -120,12 +120,17 @@ public function testRenderExceptionIgnoreErrorsWithAlt() private function getKernel($returnValue) { $kernel = $this->createMock(HttpKernelInterface::class); - $kernel + $mocker = $kernel ->expects($this->any()) ->method('handle') - ->will($returnValue) ; + if ($returnValue instanceof \Exception) { + $mocker->willThrowException($returnValue); + } else { + $mocker->willReturn(...(\is_array($returnValue) ? $returnValue : [$returnValue])); + } + return $kernel; } diff --git a/Tests/HttpKernelBrowserTest.php b/Tests/HttpKernelBrowserTest.php index 55963a16c3..9092c3bf46 100644 --- a/Tests/HttpKernelBrowserTest.php +++ b/Tests/HttpKernelBrowserTest.php @@ -18,6 +18,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpKernel\HttpKernelBrowser; +use Symfony\Component\HttpKernel\Tests\Fixtures\MockableUploadFileWithClientSize; use Symfony\Component\HttpKernel\Tests\Fixtures\TestClient; /** @@ -153,10 +154,9 @@ public function testUploadedFileWhenSizeExceedsUploadMaxFileSize() $client = new HttpKernelBrowser($kernel); $file = $this - ->getMockBuilder(UploadedFile::class) + ->getMockBuilder(MockableUploadFileWithClientSize::class) ->setConstructorArgs([$source, 'original', 'mime/original', \UPLOAD_ERR_OK, true]) - ->onlyMethods(['getSize']) - ->addMethods(['getClientSize']) + ->onlyMethods(['getSize', 'getClientSize']) ->getMock() ; /* should be modified when the getClientSize will be removed */ diff --git a/Tests/KernelTest.php b/Tests/KernelTest.php index 74cd34cde3..0092b786fc 100644 --- a/Tests/KernelTest.php +++ b/Tests/KernelTest.php @@ -30,6 +30,7 @@ use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest; +use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTestWithLoadClassCache; use Symfony\Component\HttpKernel\Tests\Fixtures\KernelWithoutBundles; use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService; @@ -148,7 +149,7 @@ public function testBootSetsTheBootedFlagToTrue() public function testClassCacheIsNotLoadedByDefault() { - $kernel = $this->getKernel(['initializeBundles'], [], false, ['doLoadClassCache']); + $kernel = $this->getKernel(['initializeBundles', 'doLoadClassCache'], [], false, KernelForTestWithLoadClassCache::class); $kernel->expects($this->never()) ->method('doLoadClassCache'); @@ -663,20 +664,16 @@ protected function getBundle($dir = null, $parent = null, $className = null, $bu * @param array $methods Additional methods to mock (besides the abstract ones) * @param array $bundles Bundles to register */ - protected function getKernel(array $methods = [], array $bundles = [], bool $debug = false, array $methodsToAdd = []): Kernel + protected function getKernel(array $methods = [], array $bundles = [], bool $debug = false, string $kernelClass = KernelForTest::class): Kernel { $methods[] = 'registerBundles'; $kernelMockBuilder = $this - ->getMockBuilder(KernelForTest::class) + ->getMockBuilder($kernelClass) ->onlyMethods($methods) ->setConstructorArgs(['test', $debug]) ; - if (0 !== \count($methodsToAdd)) { - $kernelMockBuilder->addMethods($methodsToAdd); - } - $kernel = $kernelMockBuilder->getMock(); $kernel->expects($this->any()) ->method('registerBundles') diff --git a/Tests/UriSignerTest.php b/Tests/UriSignerTest.php index 4801776cce..8359918815 100644 --- a/Tests/UriSignerTest.php +++ b/Tests/UriSignerTest.php @@ -43,14 +43,19 @@ public function testCheck() public function testCheckWithDifferentArgSeparator() { - $this->iniSet('arg_separator.output', '&'); - $signer = new UriSigner('foobar'); - - $this->assertSame( - 'http://example.com/foo?_hash=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D&baz=bay&foo=bar', - $signer->sign('http://example.com/foo?foo=bar&baz=bay') - ); - $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay'))); + $initialSeparatorOutput = ini_set('arg_separator.output', '&'); + + try { + $signer = new UriSigner('foobar'); + + $this->assertSame( + 'http://example.com/foo?_hash=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D&baz=bay&foo=bar', + $signer->sign('http://example.com/foo?foo=bar&baz=bay') + ); + $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay'))); + } finally { + ini_set('arg_separator.output', $initialSeparatorOutput); + } } public function testCheckWithRequest()