Skip to content

Commit

Permalink
[TASK] Avoid ObjectManager in remaining ext:form places
Browse files Browse the repository at this point in the history
Finish ObjectManager removal / fallback works in
ext:form. The validators are no issue since they
have been postulated of not being autowire DI
aware in v11.0 with #92238 already, so we can
follow this.

Resolves: #94384
Related: #90803
Related: #92238
Releases: master
Change-Id: I1115d2eaa595fa5685f6549843b4f388d5d07c98
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69525
Tested-by: core-ci <typo3@b13.com>
Tested-by: Jochen <rothjochen@gmail.com>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Jochen <rothjochen@gmail.com>
Reviewed-by: Benni Mack <benni@typo3.org>
  • Loading branch information
lolli42 authored and bmack committed Jun 21, 2021
1 parent 755c355 commit 1a3fdde
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
namespace TYPO3\CMS\Form\Domain\Model\FormElements;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException;
use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException;
Expand Down Expand Up @@ -120,8 +119,7 @@ public function createElement(string $identifier, string $typeName): FormElement
throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $typeName), 1382364019);
}

$element = GeneralUtility::makeInstance(ObjectManager::class)
->get(UnknownFormElement::class, $identifier, $typeName);
$element = GeneralUtility::makeInstance(UnknownFormElement::class, $identifier, $typeName);
$this->addElement($element);
return $element;
}
Expand All @@ -131,8 +129,7 @@ public function createElement(string $identifier, string $typeName): FormElement
}

$implementationClassName = $typeDefinition['implementationClassName'];
$element = GeneralUtility::makeInstance(ObjectManager::class)
->get($implementationClassName, $identifier, $typeName);
$element = GeneralUtility::makeInstance($implementationClassName, $identifier, $typeName);
if (!$element instanceof FormElementInterface) {
throw new TypeDefinitionNotValidException(sprintf('The "implementationClassName" for element "%s" ("%s") does not implement the FormElementInterface.', $identifier, $implementationClassName), 1327318156);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
use TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException;
use TYPO3\CMS\Form\Domain\Model\Exception\ValidatorPresetNotFoundException;
Expand Down Expand Up @@ -215,8 +214,7 @@ public function createValidator(string $validatorIdentifier, array $options = []
ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);

/** @var ValidatorInterface $validator */
$validator = GeneralUtility::makeInstance(ObjectManager::class)
->get($implementationClassName, $defaultOptions);
$validator = GeneralUtility::makeInstance($implementationClassName, $defaultOptions);
$this->addValidator($validator);
return $validator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
Expand Down Expand Up @@ -62,8 +61,7 @@ public function afterBuildingFinished(RenderableInterface $renderable)
// in a dedicated sub-folder (e.g. ".../form_<40-chars-hash>/actual.file").

/** @var UploadedFileReferenceConverter $typeConverter */
$typeConverter = GeneralUtility::makeInstance(ObjectManager::class)
->get(UploadedFileReferenceConverter::class);
$typeConverter = GeneralUtility::makeInstance(UploadedFileReferenceConverter::class);
/** @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMappingConfiguration */
$propertyMappingConfiguration = $renderable->getRootForm()
->getProcessingRule($renderable->getIdentifier())
Expand All @@ -76,8 +74,7 @@ public function afterBuildingFinished(RenderableInterface $renderable)
$allowedMimeTypes = array_filter($renderable->getProperties()['allowedMimeTypes']);
}
if (!empty($allowedMimeTypes)) {
$mimeTypeValidator = GeneralUtility::makeInstance(ObjectManager::class)
->get(MimeTypeValidator::class, ['allowedMimeTypes' => $allowedMimeTypes]);
$mimeTypeValidator = GeneralUtility::makeInstance(MimeTypeValidator::class, ['allowedMimeTypes' => $allowedMimeTypes]);
$validators = [$mimeTypeValidator];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ protected function createFileReferenceFromFalFileReferenceObject(
int $resourcePointer = null
): PseudoFileReference {
if ($resourcePointer === null) {
$fileReference = $this->objectManager->get(PseudoFileReference::class);
$fileReference = GeneralUtility::makeInstance(PseudoFileReference::class);
} else {
$fileReference = $this->persistenceManager->getObjectByIdentifier($resourcePointer, PseudoFileReference::class, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\Exception;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Form\Domain\Finishers\FinisherContext;
use TYPO3\CMS\Form\Domain\Finishers\RedirectFinisher;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
Expand Down Expand Up @@ -76,11 +74,6 @@ public function pageUidOptionForFinisherAcceptsVariousPageRepresentations($pageU
GeneralUtility::setSingletonInstance(TranslationService::class, $translationServiceProphecy->reveal());
$translationServiceProphecy->translateFinisherOption(Argument::cetera())->willReturnArgument(3);

$objectManagerProphecy = $this->prophesize(ObjectManager::class);
$objectManagerProphecy->get(UriBuilder::class)->willReturn(new UriBuilder());
GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
$redirectFinisherMock->injectObjectManager($objectManagerProphecy->reveal());

try {
$redirectFinisherMock->execute($finisherContextProphecy->reveal());
self::fail('RedirectFinisher did not throw expected exception.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
namespace TYPO3\CMS\Form\Tests\Unit\Domain\FormElements;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException;
use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException;
Expand Down Expand Up @@ -132,10 +131,7 @@ public function createElementReturnsUnknownElementsIfTypeDefinitionIsNotFoundAnd
->method('getRootForm')
->willReturn($rootForm);

$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->get(UnknownFormElement::class, 'foo', 'bar')->willReturn(new UnknownFormElement('foo', 'bar'));

GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
GeneralUtility::addInstance(UnknownFormElement::class, new UnknownFormElement('foo', 'bar'));
$result = $mockAbstractSection->createElement('foo', 'bar');

self::assertInstanceOf(UnknownFormElement::class, $result);
Expand Down Expand Up @@ -224,9 +220,7 @@ public function createElementThrowsExceptionIfTypeDefinitionNotInstanceOfFormEle
->method('getRootForm')
->willReturn($rootForm);

$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->get(self::class, 'id', 'foobar')->willReturn($this);
GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
GeneralUtility::addInstance(self::class, $this);

$this->expectException(TypeDefinitionNotValidException::class);
$this->expectExceptionCode(1327318156);
Expand Down Expand Up @@ -295,10 +289,7 @@ public function createElementExpectedToAddAndInitializeElement(): void
->method('getRootForm')
->willReturn($rootForm);

$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->get(get_class($implementationMock), 'id', 'foobar')->willReturn($implementationMock);

GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
GeneralUtility::addInstance(get_class($implementationMock), $implementationMock);

$mockAbstractSection->createElement('id', 'foobar');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration as ExtbasePropertyMappingConfiguration;
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
Expand Down Expand Up @@ -113,23 +112,6 @@ public function setUp(): void
*/
public function afterBuildingFinishedAddsFileReferenceConverter(): void
{
// Mime Type Validator
/** @var \PHPUnit\Framework\MockObject\MockObject|MimeTypeValidator $mimeTypeValidator */
$mimeTypeValidator = $this->createMock(MimeTypeValidator::class);

// Resource Factory
/** @var \PHPUnit\Framework\MockObject\MockObject|ResourceFactory $resourceFactory */
$resourceFactory = $this->createMock(ResourceFactory::class);
/** @var \PHPUnit\Framework\MockObject\MockObject|UploadedFileReferenceConverter $typeConverter */
$typeConverter = $this->createMock(UploadedFileReferenceConverter::class);

// Object Manager (in order to return mocked Resource Factory)
$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->get(UploadedFileReferenceConverter::class)->willReturn($typeConverter);
$objectManager->get(MimeTypeValidator::class)->willReturn($mimeTypeValidator);
$objectManager->get(ResourceFactory::class)->willReturn($resourceFactory);
GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());

// No validators
$this->processingRule
->expects(self::any())
Expand Down Expand Up @@ -158,21 +140,6 @@ public function afterBuildingFinishedAddsMimeTypeConverter(): void
{
$mimeTypes = ['allowedMimeTypes' => ['text/plain', 'application/x-www-form-urlencoded']];

// Create a MimeTypeValidator Mock
/** @var \PHPUnit\Framework\MockObject\MockObject|MimeTypeValidator $mimeTypeValidator */
$mimeTypeValidator = $this->getMockBuilder(MimeTypeValidator::class)
->setMethods(['__construct'])
->disableOriginalConstructor()
->getMock();
/** @var \PHPUnit\Framework\MockObject\MockObject|UploadedFileReferenceConverter $typeConverter */
$typeConverter = $this->createMock(UploadedFileReferenceConverter::class);

// Object Manager to return the MimeTypeValidator
$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->get(UploadedFileReferenceConverter::class)->willReturn($typeConverter);
$objectManager->get(MimeTypeValidator::class, $mimeTypes)->willReturn($mimeTypeValidator);
GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());

// Don't add any validators for now
$this->processingRule
->expects(self::any())
Expand Down Expand Up @@ -204,33 +171,10 @@ public function afterBuildingFinishedAddsMimeTypeConverter(): void
*/
public function afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists(): void
{
// Mime Type Validator
/** @var \PHPUnit\Framework\MockObject\MockObject|MimeTypeValidator $mimeTypeValidator */
$mimeTypeValidator = $this->createMock(MimeTypeValidator::class);

// Resource Factory
/** @var \PHPUnit\Framework\MockObject\MockObject|ResourceFactory $resourceFactory */
$resourceFactory = $this->createMock(ResourceFactory::class);
GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory);
/** @var \PHPUnit\Framework\MockObject\MockObject|UploadedFileReferenceConverter $typeConverter */
$typeConverter = $this->createMock(UploadedFileReferenceConverter::class);

// Object Manager (in order to return mocked Resource Factory)
/** @var \PHPUnit\Framework\MockObject\MockObject|ObjectManager $objectManager */
$objectManager = $this->getMockBuilder(ObjectManager::class)
->setMethods(['get'])
->disableOriginalConstructor()
->getMock();

$objectManager
->expects(self::any())
->method('get')
->willReturnMap([
[UploadedFileReferenceConverter::class, $typeConverter],
[MimeTypeValidator::class, $mimeTypeValidator]
]);

GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);

// Don't add any validators for now
$this->processingRule
Expand Down Expand Up @@ -263,33 +207,10 @@ public function afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountI
*/
public function afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically(): void
{
// Mime Type Validator
/** @var \PHPUnit\Framework\MockObject\MockObject|MimeTypeValidator $mimeTypeValidator */
$mimeTypeValidator = $this->createMock(MimeTypeValidator::class);

// Resource Factory
/** @var \PHPUnit\Framework\MockObject\MockObject|ResourceFactory $resourceFactory */
$resourceFactory = $this->createMock(ResourceFactory::class);
GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory);
/** @var \PHPUnit\Framework\MockObject\MockObject|UploadedFileReferenceConverter $typeConverter */
$typeConverter = $this->createMock(UploadedFileReferenceConverter::class);

// Object Manager (in order to return mocked Resource Factory)
/** @var \PHPUnit\Framework\MockObject\MockObject|ObjectManager $objectManager */
$objectManager = $this->getMockBuilder(ObjectManager::class)
->setMethods(['get'])
->disableOriginalConstructor()
->getMock();

$objectManager
->expects(self::any())
->method('get')
->willReturnMap([
[UploadedFileReferenceConverter::class, $typeConverter],
[MimeTypeValidator::class, $mimeTypeValidator]
]);

GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);

// Don't add any validators for now
$this->processingRule
Expand Down Expand Up @@ -327,30 +248,6 @@ public function afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSa
*/
public function afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath(): void
{
// Mime Type Validator
/** @var \PHPUnit\Framework\MockObject\MockObject|MimeTypeValidator $mimeTypeValidator */
$mimeTypeValidator = $this->createMock(MimeTypeValidator::class);

/** @var \PHPUnit\Framework\MockObject\MockObject|UploadedFileReferenceConverter $typeConverter */
$typeConverter = $this->createMock(UploadedFileReferenceConverter::class);

// Object Manager (in order to return mocked Resource Factory)
/** @var \PHPUnit\Framework\MockObject\MockObject|ObjectManager $objectManager */
$objectManager = $this->getMockBuilder(ObjectManager::class)
->setMethods(['get'])
->disableOriginalConstructor()
->getMock();

$objectManager
->expects(self::any())
->method('get')
->willReturnMap([
[UploadedFileReferenceConverter::class, $typeConverter],
[MimeTypeValidator::class, $mimeTypeValidator]
]);

GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);

// Don't add any validators for now
$this->processingRule
->expects(self::any())
Expand Down Expand Up @@ -384,37 +281,9 @@ public function afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToF
*/
public function afterBuildingFinishedCopiesValidators(): void
{
// Mime Type Validator
/** @var \PHPUnit\Framework\MockObject\MockObject|MimeTypeValidator $mimeTypeValidator */
$mimeTypeValidator = $this->createMock(MimeTypeValidator::class);

// Some other Validator
$otherValidator = $this->getMockForAbstractClass(AbstractValidator::class);

// Resource Factory
/** @var \PHPUnit\Framework\MockObject\MockObject|ResourceFactory $resourceFactory */
$resourceFactory = $this->createMock(ResourceFactory::class);
/** @var \PHPUnit\Framework\MockObject\MockObject|UploadedFileReferenceConverter $typeConverter */
$typeConverter = $this->createMock(UploadedFileReferenceConverter::class);

// Object Manager (in order to return mocked Resource Factory)
/** @var \PHPUnit\Framework\MockObject\MockObject|ObjectManager $objectManager */
$objectManager = $this->getMockBuilder(ObjectManager::class)
->setMethods(['get'])
->disableOriginalConstructor()
->getMock();

$objectManager
->expects(self::any())
->method('get')
->willReturnMap([
[UploadedFileReferenceConverter::class, $typeConverter],
[MimeTypeValidator::class, $mimeTypeValidator],
[ResourceFactory::class, $resourceFactory]
]);

GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);

// Don't add any validators for now
$validators = new \SplObjectStorage();
$validators->attach($otherValidator);
Expand Down Expand Up @@ -443,37 +312,9 @@ public function afterBuildingFinishedCopiesValidators(): void
*/
public function afterBuildingFinishedDoesNotCopyNotEmptyValidator(): void
{
// Mime Type Validator
/** @var \PHPUnit\Framework\MockObject\MockObject|MimeTypeValidator $mimeTypeValidator */
$mimeTypeValidator = $this->createMock(MimeTypeValidator::class);

// Not Empty Validator
$notEmptyValidator = $this->getMockForAbstractClass(NotEmptyValidator::class);

// Resource Factory
/** @var \PHPUnit\Framework\MockObject\MockObject|ResourceFactory $resourceFactory */
$resourceFactory = $this->createMock(ResourceFactory::class);
/** @var \PHPUnit\Framework\MockObject\MockObject|UploadedFileReferenceConverter $typeConverter */
$typeConverter = $this->createMock(UploadedFileReferenceConverter::class);

// Object Manager (in order to return mocked Resource Factory)
/** @var \PHPUnit\Framework\MockObject\MockObject|ObjectManager $objectManager */
$objectManager = $this->getMockBuilder(ObjectManager::class)
->setMethods(['get'])
->disableOriginalConstructor()
->getMock();

$objectManager
->expects(self::any())
->method('get')
->willReturnMap([
[UploadedFileReferenceConverter::class, $typeConverter],
[MimeTypeValidator::class, $mimeTypeValidator],
[ResourceFactory::class, $resourceFactory]
]);

GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);

// Don't add any validators for now
$validators = new \SplObjectStorage();
$validators->attach($notEmptyValidator);
Expand Down

0 comments on commit 1a3fdde

Please sign in to comment.