Skip to content

Commit

Permalink
[TASK] Migrate getMockForAbstractClass() calls in EXT:form
Browse files Browse the repository at this point in the history
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#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 <ben@bnf.dev>
Tested-by: Benjamin Franzke <ben@bnf.dev>
Tested-by: core-ci <typo3@b13.com>
  • Loading branch information
oliverklee authored and bnf committed Sep 11, 2023
1 parent 852e913 commit a7570a3
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 232 deletions.
Expand Up @@ -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
Expand All @@ -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());
}
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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();
Expand All @@ -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']);
Expand All @@ -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' => [
Expand Down Expand Up @@ -144,108 +144,58 @@ 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);
}

/**
* @test
*/
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();
}

/**
* @test
*/
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()
);
}

Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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' => [
Expand All @@ -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());
}

/**
Expand All @@ -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' => [
Expand All @@ -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());
}
}

0 comments on commit a7570a3

Please sign in to comment.