Skip to content

Commit

Permalink
Merge branch '5.4' into 6.4
Browse files Browse the repository at this point in the history
* 5.4:
  [PropertyInfo] Update DoctrineExtractor for new DBAL 4 BIGINT type
  Update security.nl.xlf
  [Validator] IBAN Check digits should always between 2 and 98
  [Security] Populate translations for trans-unit 20
  add missing plural translation messages
  filter out empty HTTP header parts
  [String] Fix folded in compat mode
  Remove calls to `getMockForAbstractClass()`
  [ErrorHandler] Do not call xdebug_get_function_stack() with xdebug >= 3.0 when not in develop mode
  [Serializer] Fix type for missing property
  add test for JSON response with null as content
  [Filesystem] Fix dumpFile `stat failed` error hitting custom handler
  Remove calls to `TestCase::iniSet()` and calls to deprecated methods of `MockBuilder`
  [PhpUnitBridge] Fix `DeprecationErrorHandler` with PhpUnit 10
  • Loading branch information
fabpot committed May 17, 2024
2 parents a2cf112 + 67d0b0a commit b4dfaf4
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 49 deletions.
30 changes: 17 additions & 13 deletions Tests/EventListener/ErrorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,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
9 changes: 8 additions & 1 deletion Tests/EventListener/SessionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionFactory;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorageFactory;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorageFactory;
Expand Down Expand Up @@ -335,7 +336,13 @@ public function testSessionCookieSetWhenOtherNativeVariablesSet()

public function testOnlyTriggeredOnMainRequest()
{
$listener = $this->getMockForAbstractClass(AbstractSessionListener::class);
$listener = new class() extends AbstractSessionListener {
protected function getSession(): ?SessionInterface
{
return null;
}
};

$event = $this->createMock(RequestEvent::class);
$event->expects($this->once())->method('isMainRequest')->willReturn(false);
$event->expects($this->never())->method('getRequest');
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
2 changes: 1 addition & 1 deletion Tests/Fragment/RoutableFragmentRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function getGenerateFragmentUriDataWithNonScalar()

private function callGenerateFragmentUriMethod(ControllerReference $reference, Request $request, $absolute = false)
{
$renderer = $this->getMockForAbstractClass(RoutableFragmentRenderer::class);
$renderer = $this->createStub(RoutableFragmentRenderer::class);
$r = new \ReflectionObject($renderer);
$m = $r->getMethod('generateFragmentUri');

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 @@ -151,10 +152,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
22 changes: 9 additions & 13 deletions Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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 @@ -154,7 +155,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 @@ -410,7 +411,7 @@ public function testLocateResourceOnDirectories()
$kernel
->expects($this->exactly(2))
->method('getBundle')
->willReturn($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))
->willReturn($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, 'Bundle1Bundle'))
;

$this->assertEquals(
Expand All @@ -427,8 +428,8 @@ public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWith
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Trying to register two bundles with the same name "DuplicateName"');
$fooBundle = $this->getBundle(__DIR__.'/Fixtures/FooBundle', null, 'FooBundle', 'DuplicateName');
$barBundle = $this->getBundle(__DIR__.'/Fixtures/BarBundle', null, 'BarBundle', 'DuplicateName');
$fooBundle = $this->getBundle(__DIR__.'/Fixtures/FooBundle', 'FooBundle', 'DuplicateName');
$barBundle = $this->getBundle(__DIR__.'/Fixtures/BarBundle', 'BarBundle', 'DuplicateName');

$kernel = $this->getKernel([], [$fooBundle, $barBundle]);
$kernel->boot();
Expand Down Expand Up @@ -675,19 +676,18 @@ public function getContainerClass(): string
/**
* Returns a mock for the BundleInterface.
*/
protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null): BundleInterface
protected function getBundle($dir = null, $className = null, $bundleName = null): BundleInterface
{
$bundle = $this
->getMockBuilder(BundleInterface::class)
->onlyMethods(['getPath', 'getName'])
->disableOriginalConstructor()
;

if ($className) {
$bundle->setMockClassName($className);
}

$bundle = $bundle->getMockForAbstractClass();
$bundle = $bundle->getMock();

$bundle
->expects($this->any())
Expand All @@ -710,20 +710,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')
Expand Down
21 changes: 13 additions & 8 deletions Tests/UriSignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,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 b4dfaf4

Please sign in to comment.