From 4863f3960e1bfd8cddb3adb9449b40dd47347254 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sun, 15 Feb 2026 20:48:56 +0100 Subject: [PATCH 1/6] Create stub instead of mock in the getWillReturn method --- src/Symfony/AbstractControllerTestCase.php | 2 +- src/Symfony/Helper/FormAssertion.php | 23 ++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Symfony/AbstractControllerTestCase.php b/src/Symfony/AbstractControllerTestCase.php index 249f928..a9fc82b 100644 --- a/src/Symfony/AbstractControllerTestCase.php +++ b/src/Symfony/AbstractControllerTestCase.php @@ -86,7 +86,7 @@ public function expectCreateForm(string $type, mixed $data = null, array $option $this->container->set('form.factory', $factory); - return new FormAssertion($form, $this); + return new FormAssertion($form); } public function expectAddFlash(string $type, mixed $message): void diff --git a/src/Symfony/Helper/FormAssertion.php b/src/Symfony/Helper/FormAssertion.php index 7290873..a564249 100644 --- a/src/Symfony/Helper/FormAssertion.php +++ b/src/Symfony/Helper/FormAssertion.php @@ -4,7 +4,7 @@ namespace DR\PHPUnitExtensions\Symfony\Helper; -use PHPUnit\Framework\MockObject\MockBuilder; +use PHPUnit\Framework\MockObject\Generator\Generator as MockGenerator; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use RuntimeException; @@ -21,10 +21,9 @@ class FormAssertion { /** * @internal Instance should not be made directly, use AbstractControllerTestCase::expectCreateForm - * - * @see AbstractControllerTestCase::expectCreateForm + * @see AbstractControllerTestCase::expectCreateForm */ - public function __construct(public readonly FormInterface&MockObject $form, private readonly TestCase $testCase) + public function __construct(public readonly FormInterface&MockObject $form) { } @@ -60,10 +59,18 @@ function (string $key) use ($keyValueData) { // @codeCoverageIgnoreEnd } - $mock = (new MockBuilder($this->testCase, FormInterface::class))->getMock(); - $mock->method('getData')->willReturn($keyValueData[$key]); - - return $mock; + $stub = (new MockGenerator()) + ->testDouble( + FormInterface::class, + true, + callOriginalConstructor: false, + callOriginalClone: false, + cloneArguments: false, + allowMockingUnknownTypes: false, + ); + $stub->method('getData')->willReturn($keyValueData[$key]); + + return $stub; } ); From 3f9986df5d495cbda4ada947c42f9d85687d4483 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sun, 15 Feb 2026 21:02:34 +0100 Subject: [PATCH 2/6] Create stub instead of mock in the getWillReturn method --- src/Symfony/AbstractConstraintValidatorTestCase.php | 1 + src/Symfony/Helper/FormAssertion.php | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Symfony/AbstractConstraintValidatorTestCase.php b/src/Symfony/AbstractConstraintValidatorTestCase.php index 667291e..a472217 100644 --- a/src/Symfony/AbstractConstraintValidatorTestCase.php +++ b/src/Symfony/AbstractConstraintValidatorTestCase.php @@ -53,6 +53,7 @@ abstract protected function getConstraint(): Constraint; protected function setUp(): void { parent::setUp(); + static::createStub($originalClassName) $this->violationBuilder = $this->createMock(ConstraintViolationBuilder::class); $this->executionContext = $this->createMock(ExecutionContextInterface::class); $this->executionContext->method('getRoot')->willReturn($this->initRootForm()); diff --git a/src/Symfony/Helper/FormAssertion.php b/src/Symfony/Helper/FormAssertion.php index a564249..adc30e9 100644 --- a/src/Symfony/Helper/FormAssertion.php +++ b/src/Symfony/Helper/FormAssertion.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\MockObject\Generator\Generator as MockGenerator; use PHPUnit\Framework\MockObject\MockObject; -use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\Version; use RuntimeException; use Symfony\Component\Form\FormConfigInterface; use Symfony\Component\Form\FormError; @@ -59,8 +59,9 @@ function (string $key) use ($keyValueData) { // @codeCoverageIgnoreEnd } - $stub = (new MockGenerator()) - ->testDouble( + $generator = new MockGenerator(); + if (Version::majorVersionNumber() === 10) { + $stub = $generator->testDouble( FormInterface::class, true, callOriginalConstructor: false, @@ -68,6 +69,9 @@ function (string $key) use ($keyValueData) { cloneArguments: false, allowMockingUnknownTypes: false, ); + } else { + $stub = $generator->testDouble(FormInterface::class, true, callOriginalConstructor: false, callOriginalClone: false); + } $stub->method('getData')->willReturn($keyValueData[$key]); return $stub; From 9634b7bc1c01cdc803aa0317028bbd4ad22e727a Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sun, 15 Feb 2026 21:04:20 +0100 Subject: [PATCH 3/6] Create stub instead of mock in the getWillReturn method --- src/Symfony/AbstractConstraintValidatorTestCase.php | 1 - src/Symfony/Helper/FormAssertion.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Symfony/AbstractConstraintValidatorTestCase.php b/src/Symfony/AbstractConstraintValidatorTestCase.php index a472217..667291e 100644 --- a/src/Symfony/AbstractConstraintValidatorTestCase.php +++ b/src/Symfony/AbstractConstraintValidatorTestCase.php @@ -53,7 +53,6 @@ abstract protected function getConstraint(): Constraint; protected function setUp(): void { parent::setUp(); - static::createStub($originalClassName) $this->violationBuilder = $this->createMock(ConstraintViolationBuilder::class); $this->executionContext = $this->createMock(ExecutionContextInterface::class); $this->executionContext->method('getRoot')->willReturn($this->initRootForm()); diff --git a/src/Symfony/Helper/FormAssertion.php b/src/Symfony/Helper/FormAssertion.php index adc30e9..8fc7a97 100644 --- a/src/Symfony/Helper/FormAssertion.php +++ b/src/Symfony/Helper/FormAssertion.php @@ -70,7 +70,7 @@ function (string $key) use ($keyValueData) { allowMockingUnknownTypes: false, ); } else { - $stub = $generator->testDouble(FormInterface::class, true, callOriginalConstructor: false, callOriginalClone: false); + $stub = $generator->testDouble(FormInterface::class, false, callOriginalConstructor: false, callOriginalClone: false); } $stub->method('getData')->willReturn($keyValueData[$key]); From 29e4d4be19dc573ad68636d819292bb1222bedee Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sun, 15 Feb 2026 21:07:18 +0100 Subject: [PATCH 4/6] Create stub instead of mock in the getWillReturn method --- src/Symfony/Helper/FormAssertion.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Symfony/Helper/FormAssertion.php b/src/Symfony/Helper/FormAssertion.php index 8fc7a97..f2ab9ee 100644 --- a/src/Symfony/Helper/FormAssertion.php +++ b/src/Symfony/Helper/FormAssertion.php @@ -69,6 +69,16 @@ function (string $key) use ($keyValueData) { cloneArguments: false, allowMockingUnknownTypes: false, ); + } elseif (Version::majorVersionNumber() === 11) { + $stub = $generator->testDouble( + FormInterface::class, + true, + false, + callOriginalConstructor: false, + callOriginalClone: false, + cloneArguments: false, + allowMockingUnknownTypes: false, + ); } else { $stub = $generator->testDouble(FormInterface::class, false, callOriginalConstructor: false, callOriginalClone: false); } From 2e48f64cf0cb4a262c2aab1b4eb39c496b05ecd0 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sun, 15 Feb 2026 21:17:00 +0100 Subject: [PATCH 5/6] Create stub instead of mock in the getWillReturn method --- phpstan-baseline.neon | 30 ++++++++++++++++++++++++++++ src/Symfony/Helper/FormAssertion.php | 2 ++ 2 files changed, 32 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e8df237..49fa3aa 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -17,3 +17,33 @@ parameters: identifier: missingType.generics count: 1 path: src/Symfony/Helper/FormAssertion.php + + - + message: '#^Method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) invoked with named argument \$callOriginalClone, but it''s not allowed because of @no\-named\-arguments\.$#' + identifier: argument.named + count: 3 + path: src/Symfony/Helper/FormAssertion.php + + - + message: '#^Method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) invoked with named argument \$callOriginalConstructor, but it''s not allowed because of @no\-named\-arguments\.$#' + identifier: argument.named + count: 3 + path: src/Symfony/Helper/FormAssertion.php + + - + message: '#^Parameter \#3 \$methods of method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) expects list\\|null, false given\.$#' + identifier: argument.type + count: 1 + path: src/Symfony/Helper/FormAssertion.php + + - + message: '#^Unknown parameter \$allowMockingUnknownTypes in call to method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\)\.$#' + identifier: argument.unknown + count: 2 + path: src/Symfony/Helper/FormAssertion.php + + - + message: '#^Unknown parameter \$cloneArguments in call to method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\)\.$#' + identifier: argument.unknown + count: 2 + path: src/Symfony/Helper/FormAssertion.php diff --git a/src/Symfony/Helper/FormAssertion.php b/src/Symfony/Helper/FormAssertion.php index f2ab9ee..2d975a9 100644 --- a/src/Symfony/Helper/FormAssertion.php +++ b/src/Symfony/Helper/FormAssertion.php @@ -59,6 +59,7 @@ function (string $key) use ($keyValueData) { // @codeCoverageIgnoreEnd } + // @codeCoverageIgnoreStart $generator = new MockGenerator(); if (Version::majorVersionNumber() === 10) { $stub = $generator->testDouble( @@ -82,6 +83,7 @@ function (string $key) use ($keyValueData) { } else { $stub = $generator->testDouble(FormInterface::class, false, callOriginalConstructor: false, callOriginalClone: false); } + // @codeCoverageIgnoreEnd $stub->method('getData')->willReturn($keyValueData[$key]); return $stub; From 7562f7ef974833fd25641ce0c96e77d527505199 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sun, 15 Feb 2026 21:22:29 +0100 Subject: [PATCH 6/6] Create stub instead of mock in the getWillReturn method --- phpstan-baseline.neon | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 49fa3aa..ba038c5 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -19,31 +19,31 @@ parameters: path: src/Symfony/Helper/FormAssertion.php - - message: '#^Method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) invoked with named argument \$callOriginalClone, but it''s not allowed because of @no\-named\-arguments\.$#' + message: '#^Method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) invoked with named argument \$allowMockingUnknownTypes, but it''s not allowed because of @no\-named\-arguments\.$#' identifier: argument.named - count: 3 + count: 2 path: src/Symfony/Helper/FormAssertion.php - - message: '#^Method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) invoked with named argument \$callOriginalConstructor, but it''s not allowed because of @no\-named\-arguments\.$#' + message: '#^Method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) invoked with named argument \$callOriginalClone, but it''s not allowed because of @no\-named\-arguments\.$#' identifier: argument.named count: 3 path: src/Symfony/Helper/FormAssertion.php - - message: '#^Parameter \#3 \$methods of method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) expects list\\|null, false given\.$#' - identifier: argument.type - count: 1 + message: '#^Method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) invoked with named argument \$callOriginalConstructor, but it''s not allowed because of @no\-named\-arguments\.$#' + identifier: argument.named + count: 3 path: src/Symfony/Helper/FormAssertion.php - - message: '#^Unknown parameter \$allowMockingUnknownTypes in call to method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\)\.$#' - identifier: argument.unknown + message: '#^Method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) invoked with named argument \$cloneArguments, but it''s not allowed because of @no\-named\-arguments\.$#' + identifier: argument.named count: 2 path: src/Symfony/Helper/FormAssertion.php - - message: '#^Unknown parameter \$cloneArguments in call to method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\)\.$#' - identifier: argument.unknown - count: 2 + message: '#^Parameter \#3 \$methods of method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) expects array\|null, false given\.$#' + identifier: argument.type + count: 1 path: src/Symfony/Helper/FormAssertion.php