Skip to content

Commit

Permalink
minor #54785 Remove calls to TestCase::iniSet() and calls to deprec…
Browse files Browse the repository at this point in the history
…ated methods of `MockBuilder` (alexandre-daubois)

This PR was merged into the 5.4 branch.

Discussion
----------

Remove calls to `TestCase::iniSet()` and calls to deprecated methods of `MockBuilder`

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Last round (before new deprecations 🙂). All deprecations [listed here](https://github.com/sebastianbergmann/phpunit/blob/main/DEPRECATIONS.md) should be gone.

Commits
-------

4d5065ddd9 Remove calls to `TestCase::iniSet()` and calls to deprecated methods of `MockBuilder`
  • Loading branch information
xabbuh committed May 15, 2024
2 parents 77810d5 + 880afee commit 67d0b0a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 41 deletions.
30 changes: 17 additions & 13 deletions Tests/EventListener/ErrorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/Fixtures/KernelForTestWithLoadClassCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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
{
}
}
22 changes: 22 additions & 0 deletions Tests/Fixtures/MockableUploadFileWithClientSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;
}
}
8 changes: 4 additions & 4 deletions Tests/Fragment/FragmentHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) {
Expand All @@ -97,7 +97,7 @@ protected function getHandler($returnValue, $arguments = [])
$e = $renderer
->expects($this->any())
->method('render')
->will($returnValue)
->willReturn($returnValue)
;

if ($arguments) {
Expand Down
17 changes: 11 additions & 6 deletions Tests/Fragment/InlineFragmentRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -81,15 +81,15 @@ 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());
}

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);
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/HttpKernelBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 */
Expand Down
11 changes: 4 additions & 7 deletions Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -662,20 +663,16 @@ protected function getBundle($dir = null, $className = null, $bundleName = null)
* @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')
Expand Down
21 changes: 13 additions & 8 deletions Tests/UriSignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ public function testCheck()

public function testCheckWithDifferentArgSeparator()
{
$this->iniSet('arg_separator.output', '&amp;');
$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', '&amp;');

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()
Expand Down

0 comments on commit 67d0b0a

Please sign in to comment.