2 declare(strict_types = 1);
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator::class;
45 protected function getValidator(array $options = [], array $mockedMethods = [
'translateErrorMessage'])
47 return $this->getAccessibleMock($this->validatorClassName, $mockedMethods, [$options],
'',
true);
50 protected function setUp()
52 $this->mockValidatorResolver = $this->getAccessibleMock(
54 [
'createValidator',
'buildBaseValidatorConjunction',
'getBaseValidatorConjunction']
57 $this->validator->_set(
'validatorResolver', $this->mockValidatorResolver);
65 $this->assertFalse($this->validator->validate(
null)->hasErrors());
73 $this->assertTrue($this->validator->validate(
new \stdClass())->hasErrors());
83 $this->validator->_set(
'options', [
'elementValidator' =>
'EmailAddress']);
84 $this->mockValidatorResolver->expects($this->exactly(4))
85 ->method(
'createValidator')
86 ->with(
'EmailAddress')
87 ->will($this->returnValue(
88 $this->getMockBuilder(\
TYPO3\CMS\
Extbase\Validation\Validator\EmailAddressValidator::class)
89 ->setMethods([
'translateErrorMessage'])
92 $this->validator->_set(
'validatorResolver', $this->mockValidatorResolver);
93 $arrayOfEmailAddresses = [
95 'not a valid address',
100 $result = $this->validator->validate($arrayOfEmailAddresses);
102 $this->assertTrue($result->hasErrors());
103 $this->assertCount(2, $result->getFlattenedErrors());
121 public $integer =
'Not an integer';
129 $aValidator = $this->getMockBuilder(\
TYPO3\CMS\
Extbase\Validation\Validator\GenericObjectValidator::class)
130 ->setMethods([
'translateErrorMessage'])
131 ->setConstructorArgs([[]])
133 $this->validator->_set(
'options', [
'elementValidator' =>
'Integer']);
134 $integerValidator = $this->getMockBuilder(\
TYPO3\CMS\
Extbase\Validation\Validator\IntegerValidator::class)
135 ->setMethods([
'translateErrorMessage'])
136 ->setConstructorArgs([[]])
139 $this->mockValidatorResolver->expects($this->any())
140 ->method(
'createValidator')
142 ->will($this->returnValue($integerValidator));
143 $this->mockValidatorResolver->expects($this->any())
144 ->method(
'buildBaseValidatorConjunction')
145 ->will($this->returnValue($aValidator));
148 $aValidator->addPropertyValidator(
'b', $this->validator);
149 $aValidator->addPropertyValidator(
'integer', $integerValidator);
151 $result = $aValidator->validate($A)->getFlattenedErrors();
152 $this->assertEquals(1221560494, $result[
'b.0'][0]->getCode());
162 $parentObject = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity(
'Foo');
163 $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
164 $lazyObjectStorage = new \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage(
167 [
'someNotEmptyValue']
172 $validator->expects($this->never())->method(
'isValid');
173 $this->mockValidatorResolver->expects($this->never())->method(
'createValidator');
184 $entity = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity(
'Foo');
185 $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
186 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
187 $objectStorage->attach($entity);
188 $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator([]);
190 $this->mockValidatorResolver->expects($this->never())->method(
'createValidator');
191 $this->mockValidatorResolver->expects($this->once())
192 ->method(
'getBaseValidatorConjunction')
194 ->will($this->returnValue($aValidator));
196 $this->validator->_set(
'options', [
'elementType' => $elementType]);
198 $this->validator->validate($objectStorage);