Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
[BUGFIX] Check for interfaces in class check
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Mar 29, 2017
1 parent 4dc2762 commit 47f8441
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 161 deletions.
2 changes: 1 addition & 1 deletion Classes/ConfigurationObjectInstance.php
Expand Up @@ -133,7 +133,7 @@ public function hasValidationResult()
public function refreshValidationResult()
{
$validator = Core::get()->getValidatorResolver()
->getBaseValidatorConjunctionWithMixedTypesCheck(get_class($this->object));
->getBaseValidatorConjunction(get_class($this->object));

$this->validationResult = $validator->validate($this->object);
$this->validationResult->merge($this->mapperResult);
Expand Down
2 changes: 1 addition & 1 deletion Classes/Core/Core.php
Expand Up @@ -107,7 +107,7 @@ public static function get()
public function classExists($className)
{
if (false === isset($this->existingClassList[$className])) {
$this->existingClassList[$className] = class_exists($className);
$this->existingClassList[$className] = class_exists($className) || interface_exists($className);
}

return $this->existingClassList[$className];
Expand Down
Expand Up @@ -30,7 +30,7 @@ public function isValid($value)
{
foreach ($value as $index => $collectionElement) {
$collectionElementValidator = Core::get()->getValidatorResolver()
->getBaseValidatorConjunctionWithMixedTypesCheck(get_class($collectionElement));
->getBaseValidatorConjunction(get_class($collectionElement));

$this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));

Expand Down
40 changes: 0 additions & 40 deletions Classes/Validation/Validator/Internal/MixedTypeObjectValidator.php

This file was deleted.

49 changes: 3 additions & 46 deletions Classes/Validation/ValidatorResolver.php
Expand Up @@ -15,12 +15,9 @@

use Romm\ConfigurationObject\Core\Core;
use Romm\ConfigurationObject\Reflection\ReflectionService;
use Romm\ConfigurationObject\Service\Items\MixedTypes\MixedTypesInterface;
use Romm\ConfigurationObject\Validation\Validator\Internal\MixedTypeCollectionValidator;
use Romm\ConfigurationObject\Validation\Validator\Internal\MixedTypeObjectValidator;
use TYPO3\CMS\Extbase\Reflection\ReflectionService as ExtbaseReflectionService;
use TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator;
use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;

/**
* Customized validator resolver, which it mostly used to support the mixed
Expand All @@ -43,49 +40,9 @@ class ValidatorResolver extends \TYPO3\CMS\Extbase\Validation\ValidatorResolver
*/
public function createValidator($validatorType, array $validatorOptions = [])
{
if (CollectionValidator::class === $validatorType) {
$result = parent::createValidator(MixedTypeCollectionValidator::class);
} else {
$result = parent::createValidator($validatorType, $validatorOptions);
}

return $result;
}

/**
* If the given class implements the interface `MixedTypesInterface`, a
* custom conjunction validator is used instead of the default one (from the
* parent class).
*
* @param string $targetClassName
* @return ConjunctionValidator
*/
public function getBaseValidatorConjunctionWithMixedTypesCheck($targetClassName)
{
if (false === array_key_exists($targetClassName, $this->baseValidatorConjunctionsWithChecks)) {
$this->baseValidatorConjunctionsWithChecks[$targetClassName] = $this->buildBaseValidatorConjunctionWithMixedTypesCheck($targetClassName);
}

return $this->baseValidatorConjunctionsWithChecks[$targetClassName];
}

/**
* @param string $targetClassName
* @return ConjunctionValidator
*/
protected function buildBaseValidatorConjunctionWithMixedTypesCheck($targetClassName)
{
$interfaces = class_implements($targetClassName);

if (true === isset($interfaces[MixedTypesInterface::class])) {
$conjunctionValidator = new ConjunctionValidator();
$newValidator = new MixedTypeObjectValidator();
$conjunctionValidator->addValidator($newValidator);
} else {
$conjunctionValidator = $this->getBaseValidatorConjunction($targetClassName);
}

return $conjunctionValidator;
return (CollectionValidator::class === $validatorType)
? parent::createValidator(MixedTypeCollectionValidator::class)
: parent::createValidator($validatorType, $validatorOptions);
}

/**
Expand Down
72 changes: 0 additions & 72 deletions Tests/Unit/Validation/ValidatorResolverTest.php
Expand Up @@ -2,11 +2,8 @@
namespace Romm\ConfigurationObject\Tests\Unit\Validation;

use Romm\ConfigurationObject\Core\Core;
use Romm\ConfigurationObject\Tests\Fixture\Model\DummyConfigurationObjectWithMixedTypes;
use Romm\ConfigurationObject\Tests\Unit\AbstractUnitTest;
use Romm\ConfigurationObject\Validation\Validator\Internal\MixedTypeCollectionValidator;
use Romm\ConfigurationObject\Validation\Validator\Internal\MixedTypeObjectValidator;
use Romm\ConfigurationObject\Validation\ValidatorResolver;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator;
use TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator;
Expand Down Expand Up @@ -65,73 +62,4 @@ public function createValidatorChecksCollectionType()
unset($validatorResolver);
unset($extbaseValidatorResolver);
}

/**
* Will test that the validator resolver checks the mixed types, and uses a
* local storage to improve performances.
*
* @test
*/
public function getBaseValidatorConjunctionCheckMixedTypes()
{
/** @var ValidatorResolver|\PHPUnit_Framework_MockObject_MockObject $validatorResolver */
$validatorResolver = $this->getMockBuilder(ValidatorResolver::class)
->setMethods(['getBaseValidatorConjunction'])
->getMock();
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '7.6.0', '<')) {
$reflectedProperty = new \ReflectionProperty($validatorResolver, 'objectManager');
$reflectedProperty->setAccessible(true);
$reflectedProperty->setValue($validatorResolver, Core::get()->getObjectManager());

$reflectedProperty = new \ReflectionProperty($validatorResolver, 'reflectionService');
$reflectedProperty->setAccessible(true);
$reflectedProperty->setValue($validatorResolver, Core::get()->getReflectionService());
} else {
$validatorResolver->injectObjectManager(Core::get()->getObjectManager());
$validatorResolver->injectReflectionService(Core::get()->getReflectionService());
}

$validatorResolver->expects($this->never())
->method('getBaseValidatorConjunction');

$validator = $validatorResolver->getBaseValidatorConjunctionWithMixedTypesCheck(DummyConfigurationObjectWithMixedTypes::class);

$validator->getValidators()->rewind();
$this->assertEquals(1, $validator->count());
$this->assertEquals(
MixedTypeObjectValidator::class,
get_class($validator->getValidators()->current())
);

/*
* Here we check that getting a base validator conjunction on a class
* which does not implement the interface `MixedTypesInterface` should
* call the parent function `getBaseValidatorConjunction()`, and only
* once thanks to the local storage.
*/
/** @var ValidatorResolver|\PHPUnit_Framework_MockObject_MockObject $validatorResolver */
$validatorResolver = $this->getMockBuilder(ValidatorResolver::class)
->setMethods(['getBaseValidatorConjunction'])
->getMock();

if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '7.6.0', '<')) {
$reflectedProperty = new \ReflectionProperty($validatorResolver, 'objectManager');
$reflectedProperty->setAccessible(true);
$reflectedProperty->setValue($validatorResolver, Core::get()->getObjectManager());

$reflectedProperty = new \ReflectionProperty($validatorResolver, 'reflectionService');
$reflectedProperty->setAccessible(true);
$reflectedProperty->setValue($validatorResolver, Core::get()->getReflectionService());
} else {
$validatorResolver->injectObjectManager(Core::get()->getObjectManager());
$validatorResolver->injectReflectionService(Core::get()->getReflectionService());
}

$validatorResolver->expects($this->once())
->method('getBaseValidatorConjunction');

for ($i = 0; $i < 5; $i++) {
$validatorResolver->getBaseValidatorConjunctionWithMixedTypesCheck(\stdClass::class);
}
}
}

0 comments on commit 47f8441

Please sign in to comment.