minor #41238 [Form] Replace broken ServerParams mock (derrabus)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] Replace broken ServerParams mock

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

`AbstractRequestHandlerTest` creates a mocked version of the `ServerParams` class. Since `getNormalizedIniPostMaxSize()` does not have a return type declaration, PHPUnit will by default create an implementation that always returns `null`. However, the real implementation of that method guarantees to always return a string.

This becomes problematic in PHP 8.1 because the return value is sometimes passed as-is into functions like `strtolower()` which trigger a deprecation warning on `null` now.

This PR proposes to replace the mock with a dummy implementation that returns an empty string by default.

Commits
-------

77c2d69f19 [Form] Replace broken ServerParams mock
This commit is contained in:
Nicolas Grekas 2021-05-16 10:57:09 +02:00
commit d3ebc5fd53
1 changed files with 18 additions and 8 deletions

View File

@ -44,7 +44,21 @@ abstract class AbstractRequestHandlerTest extends TestCase
protected function setUp(): void
{
$this->serverParams = $this->getMockBuilder(ServerParams::class)->setMethods(['getNormalizedIniPostMaxSize', 'getContentLength'])->getMock();
$this->serverParams = new class() extends ServerParams {
public $contentLength;
public $postMaxSize = '';
public function getContentLength(): ?int
{
return $this->contentLength;
}
public function getNormalizedIniPostMaxSize(): string
{
return $this->postMaxSize;
}
};
$this->requestHandler = $this->getRequestHandler();
$this->factory = Forms::createFormFactoryBuilder()->getFormFactory();
$this->request = null;
@ -310,14 +324,10 @@ abstract class AbstractRequestHandlerTest extends TestCase
/**
* @dataProvider getPostMaxSizeFixtures
*/
public function testAddFormErrorIfPostMaxSizeExceeded($contentLength, $iniMax, $shouldFail, array $errorParams = [])
public function testAddFormErrorIfPostMaxSizeExceeded(?int $contentLength, string $iniMax, bool $shouldFail, array $errorParams = [])
{
$this->serverParams->expects($this->once())
->method('getContentLength')
->willReturn($contentLength);
$this->serverParams->expects($this->any())
->method('getNormalizedIniPostMaxSize')
->willReturn($iniMax);
$this->serverParams->contentLength = $contentLength;
$this->serverParams->postMaxSize = $iniMax;
$options = ['post_max_size_message' => 'Max {{ max }}!'];
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, $options);