Skip to content

Commit d3ebc5f

Browse files
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 ------- 77c2d69 [Form] Replace broken ServerParams mock
2 parents dffdc71 + 77c2d69 commit d3ebc5f

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,21 @@ abstract class AbstractRequestHandlerTest extends TestCase
4444

4545
protected function setUp(): void
4646
{
47-
$this->serverParams = $this->getMockBuilder(ServerParams::class)->setMethods(['getNormalizedIniPostMaxSize', 'getContentLength'])->getMock();
47+
$this->serverParams = new class() extends ServerParams {
48+
public $contentLength;
49+
public $postMaxSize = '';
50+
51+
public function getContentLength(): ?int
52+
{
53+
return $this->contentLength;
54+
}
55+
56+
public function getNormalizedIniPostMaxSize(): string
57+
{
58+
return $this->postMaxSize;
59+
}
60+
};
61+
4862
$this->requestHandler = $this->getRequestHandler();
4963
$this->factory = Forms::createFormFactoryBuilder()->getFormFactory();
5064
$this->request = null;
@@ -310,14 +324,10 @@ public function testSubmitFileWithNamelessForm($method)
310324
/**
311325
* @dataProvider getPostMaxSizeFixtures
312326
*/
313-
public function testAddFormErrorIfPostMaxSizeExceeded($contentLength, $iniMax, $shouldFail, array $errorParams = [])
327+
public function testAddFormErrorIfPostMaxSizeExceeded(?int $contentLength, string $iniMax, bool $shouldFail, array $errorParams = [])
314328
{
315-
$this->serverParams->expects($this->once())
316-
->method('getContentLength')
317-
->willReturn($contentLength);
318-
$this->serverParams->expects($this->any())
319-
->method('getNormalizedIniPostMaxSize')
320-
->willReturn($iniMax);
329+
$this->serverParams->contentLength = $contentLength;
330+
$this->serverParams->postMaxSize = $iniMax;
321331

322332
$options = ['post_max_size_message' => 'Max {{ max }}!'];
323333
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, $options);

0 commit comments

Comments
 (0)