From d1fbcfef7e8df2ec5e3e3d08766bdb78fe1ec8ea Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Fri, 11 Aug 2023 14:26:20 +0200 Subject: [PATCH] [TASK] Migrate `getMockForAbstractClass()` calls in `EXT:form` `getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1: https://github.com/sebastianbergmann/phpunit/issues/5241 Hence, we should replace its usages to follow best practices and avoid deprecation warnings later with PHPUnit 11. We do this by creating dedicated fixture subclasses of the affected abstract classes. Resolves: #101661 Related: #101601 Releases: main, 12.4 Change-Id: I3b9ba4511620aebf125ff207428adc5483d47154 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80944 Reviewed-by: Benjamin Franzke Tested-by: Benjamin Franzke Tested-by: core-ci --- .../FormElements/AbstractFormElementTest.php | 174 +++++------------- .../FormElements/AbstractSectionTest.php | 89 ++------- .../Fixtures/TestingFormElement.php | 31 ++++ .../FormElements/Fixtures/TestingSection.php | 37 ++++ .../Renderable/AbstractRenderableTest.php | 44 ++--- .../Renderable/Fixtures/TestingRenderable.php | 27 +++ 6 files changed, 170 insertions(+), 232 deletions(-) create mode 100644 Tests/Unit/Domain/FormElements/Fixtures/TestingFormElement.php create mode 100644 Tests/Unit/Domain/FormElements/Fixtures/TestingSection.php create mode 100644 Tests/Unit/Domain/Renderable/Fixtures/TestingRenderable.php diff --git a/Tests/Unit/Domain/FormElements/AbstractFormElementTest.php b/Tests/Unit/Domain/FormElements/AbstractFormElementTest.php index 4c6b255e..8527340a 100644 --- a/Tests/Unit/Domain/FormElements/AbstractFormElementTest.php +++ b/Tests/Unit/Domain/FormElements/AbstractFormElementTest.php @@ -20,8 +20,8 @@ use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException; use TYPO3\CMS\Form\Domain\Model\FormDefinition; -use TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement; use TYPO3\CMS\Form\Domain\Model\FormElements\GenericFormElement; +use TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\Fixtures\TestingFormElement; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; final class AbstractFormElementTest extends UnitTestCase @@ -31,7 +31,7 @@ final class AbstractFormElementTest extends UnitTestCase */ public function newInstanceHasNoProperties(): void { - $subject = $this->getMockForAbstractClass(AbstractFormElement::class, ['an_id', 'a_type']); + $subject = new TestingFormElement(); self::assertNotNull($subject); self::assertCount(0, $subject->getProperties()); } @@ -41,7 +41,7 @@ public function newInstanceHasNoProperties(): void */ public function setSimpleProperties(): void { - $subject = $this->getMockForAbstractClass(AbstractFormElement::class, ['an_id', 'a_type']); + $subject = new TestingFormElement(); $subject->setProperty('foo', 'bar'); $subject->setProperty('buz', 'qax'); @@ -59,7 +59,7 @@ public function setSimpleProperties(): void */ public function overrideProperties(): void { - $subject = $this->getMockForAbstractClass(AbstractFormElement::class, ['an_id', 'a_type']); + $subject = new TestingFormElement(); $subject->setProperty('foo', 'bar'); $subject->setProperty('foo', 'buz'); @@ -75,7 +75,7 @@ public function overrideProperties(): void */ public function setArrayProperties(): void { - $subject = $this->getMockForAbstractClass(AbstractFormElement::class, ['an_id', 'a_type']); + $subject = new TestingFormElement(); $subject->setProperty('foo', ['bar' => 'baz', 'bla' => 'blubb']); $properties = $subject->getProperties(); @@ -95,7 +95,7 @@ public function setArrayProperties(): void */ public function setPropertyUnsetIfValueIsNull(): void { - $subject = $this->getMockForAbstractClass(AbstractFormElement::class, ['an_id', 'a_type']); + $subject = new TestingFormElement(); $expected = ['foo-1' => ['bar-1' => 'foo-2']]; $subject->setProperty('foo-1', ['bar-1' => 'foo-2']); @@ -110,7 +110,7 @@ public function setPropertyUnsetIfValueIsNull(): void */ public function setPropertyUnsetIfValueIsArrayWithSomeNullVales(): void { - $subject = $this->getMockForAbstractClass(AbstractFormElement::class, ['an_id', 'a_type']); + $subject = new TestingFormElement(); $expected = [ 'foo-1' => [ @@ -144,16 +144,9 @@ public function constructThrowsExceptionWhenIdentifierIsEmpty(): void */ public function constructMustNotThrowExceptionWhenIdentifierIsNonEmptyString(): void { - $mock = $this->getMockForAbstractClass( - AbstractFormElement::class, - ['is_in', 'a_type'], - '', - true, - true, - true, - [] - ); - self::assertInstanceOf(AbstractFormElement::class, $mock); + $formElement = new TestingFormElement(); + + self::assertInstanceOf(TestingFormElement::class, $formElement); } /** @@ -161,39 +154,22 @@ public function constructMustNotThrowExceptionWhenIdentifierIsNonEmptyString(): */ public function initializeFormElementExpectedCallInitializeFormObjectHooks(): void { - $abstractFormElementMock = $this->getMockForAbstractClass( - AbstractFormElement::class, - [], - '', - false, - false, - true, - [] - ); - $secondMock = $this->getMockForAbstractClass( - AbstractFormElement::class, - [], - '', - false, - false, - true, - [ - 'initializeFormElement', - ] - ); + $formElement = new TestingFormElement(); + $secondFormElementMock = $this->createMock(TestingFormElement::class); - $secondMock-> + $secondFormElementMock-> expects(self::once()) ->method('initializeFormElement') - ->with($abstractFormElementMock); + ->with($formElement); - GeneralUtility::addInstance(\get_class($secondMock), $secondMock); + $secondFormElementMockClassName = \get_class($secondFormElementMock); + GeneralUtility::addInstance($secondFormElementMockClassName, $secondFormElementMock); $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'] = [ - \get_class($secondMock), + $secondFormElementMockClassName, ]; - $abstractFormElementMock->initializeFormElement(); + $formElement->initializeFormElement(); } /** @@ -201,51 +177,25 @@ public function initializeFormElementExpectedCallInitializeFormObjectHooks(): vo */ public function getUniqueIdentifierExpectedUnique(): void { - $abstractFormElementMock1 = $this->getMockForAbstractClass( - AbstractFormElement::class, - [], - '', - false, - false, - true, - [ - 'getRootForm', - ] - ); - - $abstractFormElementMock2 = $this->getMockForAbstractClass( - AbstractFormElement::class, - [], - '', - false, - false, - true, - [ - 'getRootForm', - ] - ); - $formDefinition1 = $this->createMock(FormDefinition::class); $formDefinition1 ->method('getIdentifier') ->willReturn('c'); - $abstractFormElementMock1 - ->method('getRootForm') - ->willReturn($formDefinition1); + $formElement1 = new TestingFormElement(); + $formElement1->setParentRenderable($formDefinition1); $formDefinition2 = $this->createMock(FormDefinition::class); $formDefinition2 ->method('getIdentifier') ->willReturn('d'); - $abstractFormElementMock2 - ->method('getRootForm') - ->willReturn($formDefinition2); + $formElement2 = new TestingFormElement(); + $formElement2->setParentRenderable($formDefinition2); self::assertNotSame( - $abstractFormElementMock1->getUniqueIdentifier(), - $abstractFormElementMock2->getUniqueIdentifier() + $formElement1->getUniqueIdentifier(), + $formElement2->getUniqueIdentifier() ); } @@ -255,25 +205,15 @@ public function getUniqueIdentifierExpectedUnique(): void public function setDefaultValueSetStringValueIfKeyDoesNotExists(): void { $formDefinitionMock = $this->getAccessibleMock(FormDefinition::class, null, [], '', false); - $abstractFormElementMock = $this->getMockForAbstractClass( - AbstractFormElement::class, - ['is_in', 'a_type'], - '', - true, - true, - true, - ['getRootForm'] - ); - $abstractFormElementMock - ->method('getRootForm') - ->willReturn($formDefinitionMock); + $formElement = new TestingFormElement(); + $formElement->setParentRenderable($formDefinitionMock); $input = 'foo'; $expected = 'foo'; - $abstractFormElementMock->setDefaultValue($input); + $formElement->setDefaultValue($input); - self::assertSame($expected, $abstractFormElementMock->getDefaultValue()); + self::assertSame($expected, $formElement->getDefaultValue()); } /** @@ -282,25 +222,15 @@ public function setDefaultValueSetStringValueIfKeyDoesNotExists(): void public function setDefaultValueSetArrayValueIfKeyDoesNotExists(): void { $formDefinitionMock = $this->getAccessibleMock(FormDefinition::class, null, [], '', false); - $abstractFormElementMock = $this->getMockForAbstractClass( - AbstractFormElement::class, - ['is_in', 'a_type'], - '', - true, - true, - true, - ['getRootForm'] - ); - $abstractFormElementMock - ->method('getRootForm') - ->willReturn($formDefinitionMock); + $formElement = new TestingFormElement(); + $formElement->setParentRenderable($formDefinitionMock); $input = ['foo' => 'bar']; $expected = ['foo' => 'bar']; - $abstractFormElementMock->setDefaultValue($input); + $formElement->setDefaultValue($input); - self::assertSame($expected, $abstractFormElementMock->getDefaultValue()); + self::assertSame($expected, $formElement->getDefaultValue()); } /** @@ -309,18 +239,8 @@ public function setDefaultValueSetArrayValueIfKeyDoesNotExists(): void public function setDefaultValueUnsetIfValueIsArrayWithSomeNullVales(): void { $formDefinitionMock = $this->getAccessibleMock(FormDefinition::class, null, [], '', false); - $abstractFormElementMock = $this->getMockForAbstractClass( - AbstractFormElement::class, - ['is_in', 'a_type'], - '', - true, - true, - true, - ['getRootForm'] - ); - $abstractFormElementMock - ->method('getRootForm') - ->willReturn($formDefinitionMock); + $formElement = new TestingFormElement(); + $formElement->setParentRenderable($formDefinitionMock); $input1 = [ 'foo-1' => [ @@ -347,10 +267,10 @@ public function setDefaultValueUnsetIfValueIsArrayWithSomeNullVales(): void ], ]; - $abstractFormElementMock->setDefaultValue($input1); - $abstractFormElementMock->setDefaultValue($input2); + $formElement->setDefaultValue($input1); + $formElement->setDefaultValue($input2); - self::assertSame($expected, $abstractFormElementMock->getDefaultValue()); + self::assertSame($expected, $formElement->getDefaultValue()); } /** @@ -359,18 +279,8 @@ public function setDefaultValueUnsetIfValueIsArrayWithSomeNullVales(): void public function setDefaultValueAddValueIfValueIsArray(): void { $formDefinitionMock = $this->getAccessibleMock(FormDefinition::class, null, [], '', false); - $abstractFormElementMock = $this->getMockForAbstractClass( - AbstractFormElement::class, - ['is_in', 'a_type'], - '', - true, - true, - true, - ['getRootForm'] - ); - $abstractFormElementMock - ->method('getRootForm') - ->willReturn($formDefinitionMock); + $formElement = new TestingFormElement(); + $formElement->setParentRenderable($formDefinitionMock); $input1 = [ 'foo-1' => [ @@ -397,9 +307,9 @@ public function setDefaultValueAddValueIfValueIsArray(): void ], ]; - $abstractFormElementMock->setDefaultValue($input1); - $abstractFormElementMock->setDefaultValue($input2); + $formElement->setDefaultValue($input1); + $formElement->setDefaultValue($input2); - self::assertSame($expected, $abstractFormElementMock->getDefaultValue()); + self::assertSame($expected, $formElement->getDefaultValue()); } } diff --git a/Tests/Unit/Domain/FormElements/AbstractSectionTest.php b/Tests/Unit/Domain/FormElements/AbstractSectionTest.php index 3c9fd6dc..6d4618c6 100644 --- a/Tests/Unit/Domain/FormElements/AbstractSectionTest.php +++ b/Tests/Unit/Domain/FormElements/AbstractSectionTest.php @@ -22,10 +22,11 @@ use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException; use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException; use TYPO3\CMS\Form\Domain\Model\FormDefinition; -use TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement; use TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection; use TYPO3\CMS\Form\Domain\Model\FormElements\Section; use TYPO3\CMS\Form\Domain\Model\FormElements\UnknownFormElement; +use TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\Fixtures\TestingFormElement; +use TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\Fixtures\TestingSection; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; final class AbstractSectionTest extends UnitTestCase @@ -107,25 +108,10 @@ public function createElementReturnsUnknownElementsIfTypeDefinitionIsNotFoundAnd $rootForm ->method('getTypeDefinitions') ->willReturn([]); - - $mockAbstractSection = $this->getMockForAbstractClass( - AbstractSection::class, - [], - '', - false, - false, - true, - [ - 'getRootForm', - ] - ); - - $mockAbstractSection - ->method('getRootForm') - ->willReturn($rootForm); + $section = new TestingSection($rootForm); GeneralUtility::addInstance(UnknownFormElement::class, new UnknownFormElement('foo', 'bar')); - $result = $mockAbstractSection->createElement('foo', 'bar'); + $result = $section->createElement('foo', 'bar'); self::assertInstanceOf(UnknownFormElement::class, $result); self::assertSame('foo', $result->getIdentifier()); @@ -147,27 +133,12 @@ public function createElementThrowsExceptionIfTypeDefinitionIsNotSet(): void $rootForm ->method('getTypeDefinitions') ->willReturn(['foobar' => []]); - - $mockAbstractSection = $this->getMockForAbstractClass( - AbstractSection::class, - [], - '', - false, - false, - true, - [ - 'getRootForm', - ] - ); - - $mockAbstractSection - ->method('getRootForm') - ->willReturn($rootForm); + $section = new TestingSection($rootForm); $this->expectException(TypeDefinitionNotFoundException::class); $this->expectExceptionCode(1325689855); - $mockAbstractSection->createElement('id', 'foobar'); + $section->createElement('id', 'foobar'); } /** @@ -176,18 +147,6 @@ public function createElementThrowsExceptionIfTypeDefinitionIsNotSet(): void public function createElementThrowsExceptionIfTypeDefinitionNotInstanceOfFormElementInterface(): void { $this->resetSingletonInstances = true; - $mockAbstractSection = $this->getMockForAbstractClass( - AbstractSection::class, - [], - '', - false, - false, - true, - [ - 'getRootForm', - ] - ); - $rootForm = $this->getMockBuilder(FormDefinition::class) ->onlyMethods(['getRenderingOptions', 'getTypeDefinitions']) ->disableOriginalConstructor() @@ -204,16 +163,13 @@ public function createElementThrowsExceptionIfTypeDefinitionNotInstanceOfFormEle ], ] ); - - $mockAbstractSection - ->method('getRootForm') - ->willReturn($rootForm); + $section = new TestingSection($rootForm); GeneralUtility::addInstance(self::class, $this); $this->expectException(TypeDefinitionNotValidException::class); $this->expectExceptionCode(1327318156); - $mockAbstractSection->createElement('id', 'foobar'); + $section->createElement('id', 'foobar'); } /** @@ -221,15 +177,7 @@ public function createElementThrowsExceptionIfTypeDefinitionNotInstanceOfFormEle */ public function createElementExpectedToAddAndInitializeElement(): void { - $implementationMock = $this->getMockForAbstractClass( - AbstractFormElement::class, - [], - '', - false, - false, - true, - ['setOptions', 'initializeFormElement'] - ); + $implementationMock = $this->createPartialMock(TestingFormElement::class, ['setOptions', 'initializeFormElement']); $typeDefinition = [ 'foo' => 'bar', @@ -249,18 +197,6 @@ public function createElementExpectedToAddAndInitializeElement(): void ->method('setOptions') ->with($typeDefinitionWithoutImplementationClassName); - $mockAbstractSection = $this->getMockForAbstractClass( - AbstractSection::class, - [], - '', - false, - false, - true, - [ - 'getRootForm', - ] - ); - $rootForm = $this->getMockBuilder(FormDefinition::class) ->onlyMethods(['getRenderingOptions', 'getTypeDefinitions']) ->disableOriginalConstructor() @@ -271,13 +207,10 @@ public function createElementExpectedToAddAndInitializeElement(): void $rootForm ->method('getTypeDefinitions') ->willReturn(['foobar' => $typeDefinition]); - - $mockAbstractSection - ->method('getRootForm') - ->willReturn($rootForm); + $section = new TestingSection($rootForm); GeneralUtility::addInstance(get_class($implementationMock), $implementationMock); - $mockAbstractSection->createElement('id', 'foobar'); + $section->createElement('id', 'foobar'); } } diff --git a/Tests/Unit/Domain/FormElements/Fixtures/TestingFormElement.php b/Tests/Unit/Domain/FormElements/Fixtures/TestingFormElement.php new file mode 100644 index 00000000..44eafb19 --- /dev/null +++ b/Tests/Unit/Domain/FormElements/Fixtures/TestingFormElement.php @@ -0,0 +1,31 @@ +rootForm; + } +} diff --git a/Tests/Unit/Domain/Renderable/AbstractRenderableTest.php b/Tests/Unit/Domain/Renderable/AbstractRenderableTest.php index 70c148c2..b2081bd3 100644 --- a/Tests/Unit/Domain/Renderable/AbstractRenderableTest.php +++ b/Tests/Unit/Domain/Renderable/AbstractRenderableTest.php @@ -17,7 +17,7 @@ namespace TYPO3\CMS\Form\Tests\Unit\Domain\Renderable; -use TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable; +use TYPO3\CMS\Form\Tests\Unit\Domain\Renderable\Fixtures\TestingRenderable; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; final class AbstractRenderableTest extends UnitTestCase @@ -27,12 +27,12 @@ final class AbstractRenderableTest extends UnitTestCase */ public function setRenderingOptionSetStringValueIfKeyDoesNotExists(): void { - $abstractRenderableMock = $this->getMockForAbstractClass(AbstractRenderable::class); + $renderable = new TestingRenderable(); $expected = ['foo' => 'bar']; - $abstractRenderableMock->setRenderingOption('foo', 'bar'); + $renderable->setRenderingOption('foo', 'bar'); - self::assertSame($expected, $abstractRenderableMock->getRenderingOptions()); + self::assertSame($expected, $renderable->getRenderingOptions()); } /** @@ -40,12 +40,12 @@ public function setRenderingOptionSetStringValueIfKeyDoesNotExists(): void */ public function setRenderingOptionSetArrayValueIfKeyDoesNotExists(): void { - $abstractRenderableMock = $this->getMockForAbstractClass(AbstractRenderable::class); + $renderable = new TestingRenderable(); $expected = ['foo-1' => ['bar' => 'foo-2']]; - $abstractRenderableMock->setRenderingOption('foo-1', ['bar' => 'foo-2']); + $renderable->setRenderingOption('foo-1', ['bar' => 'foo-2']); - self::assertSame($expected, $abstractRenderableMock->getRenderingOptions()); + self::assertSame($expected, $renderable->getRenderingOptions()); } /** @@ -53,14 +53,14 @@ public function setRenderingOptionSetArrayValueIfKeyDoesNotExists(): void */ public function setRenderingOptionUnsetIfValueIsNull(): void { - $abstractRenderableMock = $this->getMockForAbstractClass(AbstractRenderable::class); + $renderable = new TestingRenderable(); $expected = ['foo-1' => ['bar-1' => 'foo-2']]; - $abstractRenderableMock->setRenderingOption('foo-1', ['bar-1' => 'foo-2']); - $abstractRenderableMock->setRenderingOption('foo-2', ['bar-2' => 'foo-3']); - $abstractRenderableMock->setRenderingOption('foo-2', null); + $renderable->setRenderingOption('foo-1', ['bar-1' => 'foo-2']); + $renderable->setRenderingOption('foo-2', ['bar-2' => 'foo-3']); + $renderable->setRenderingOption('foo-2', null); - self::assertSame($expected, $abstractRenderableMock->getRenderingOptions()); + self::assertSame($expected, $renderable->getRenderingOptions()); } /** @@ -68,7 +68,7 @@ public function setRenderingOptionUnsetIfValueIsNull(): void */ public function setRenderingOptionUnsetIfValueIsArrayWithSomeNullVales(): void { - $abstractRenderableMock = $this->getMockForAbstractClass(AbstractRenderable::class); + $renderable = new TestingRenderable(); $expected = [ 'foo-1' => [ @@ -78,11 +78,11 @@ public function setRenderingOptionUnsetIfValueIsArrayWithSomeNullVales(): void 'bar-2' => 'foo-3', ], ]; - $abstractRenderableMock->setRenderingOption('foo-1', ['bar-1' => 'foo-2']); - $abstractRenderableMock->setRenderingOption('foo-2', ['bar-2' => 'foo-3', 'bar-3' => 'foo-4']); - $abstractRenderableMock->setRenderingOption('foo-2', ['bar-3' => null]); + $renderable->setRenderingOption('foo-1', ['bar-1' => 'foo-2']); + $renderable->setRenderingOption('foo-2', ['bar-2' => 'foo-3', 'bar-3' => 'foo-4']); + $renderable->setRenderingOption('foo-2', ['bar-3' => null]); - self::assertSame($expected, $abstractRenderableMock->getRenderingOptions()); + self::assertSame($expected, $renderable->getRenderingOptions()); } /** @@ -90,7 +90,7 @@ public function setRenderingOptionUnsetIfValueIsArrayWithSomeNullVales(): void */ public function setRenderingOptionAddValueIfValueIsArray(): void { - $abstractRenderableMock = $this->getMockForAbstractClass(AbstractRenderable::class); + $renderable = new TestingRenderable(); $expected = [ 'foo-1' => [ @@ -101,10 +101,10 @@ public function setRenderingOptionAddValueIfValueIsArray(): void 'bar-3' => 'foo-4', ], ]; - $abstractRenderableMock->setRenderingOption('foo-1', ['bar-1' => 'foo-2']); - $abstractRenderableMock->setRenderingOption('foo-2', ['bar-2' => 'foo-3']); - $abstractRenderableMock->setRenderingOption('foo-2', ['bar-3' => 'foo-4']); + $renderable->setRenderingOption('foo-1', ['bar-1' => 'foo-2']); + $renderable->setRenderingOption('foo-2', ['bar-2' => 'foo-3']); + $renderable->setRenderingOption('foo-2', ['bar-3' => 'foo-4']); - self::assertSame($expected, $abstractRenderableMock->getRenderingOptions()); + self::assertSame($expected, $renderable->getRenderingOptions()); } } diff --git a/Tests/Unit/Domain/Renderable/Fixtures/TestingRenderable.php b/Tests/Unit/Domain/Renderable/Fixtures/TestingRenderable.php new file mode 100644 index 00000000..4b30942a --- /dev/null +++ b/Tests/Unit/Domain/Renderable/Fixtures/TestingRenderable.php @@ -0,0 +1,27 @@ +