‪TYPO3CMS  10.4
ConjunctionValidatorTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
28 class ‪ConjunctionValidatorTest extends UnitTestCase
29 {
34  {
35  $conjunctionValidator = new ‪ConjunctionValidator();
36  $mockValidator = $this->createMock(ValidatorInterface::class);
37  $conjunctionValidator->addValidator($mockValidator);
38  self::assertTrue($conjunctionValidator->getValidators()->contains($mockValidator));
39  }
40 
45  {
46  $validatorConjunction = new ‪ConjunctionValidator();
47  $validatorObject = $this->getMockBuilder(ValidatorInterface::class)
48  ->setMethods(['validate', 'getOptions'])
49  ->getMock();
50  $validatorObject->expects(self::once())->method('validate')->willReturn(new ‪Result());
51  ‪$errors = new ‪Result();
52  ‪$errors->addError(new ‪Error('Error', 123));
53  $secondValidatorObject = $this->getMockBuilder(ValidatorInterface::class)
54  ->setMethods(['validate', 'getOptions'])
55  ->getMock();
56  $secondValidatorObject->expects(self::once())->method('validate')->willReturn(‪$errors);
57  $thirdValidatorObject = $this->getMockBuilder(ValidatorInterface::class)
58  ->setMethods(['validate', 'getOptions'])
59  ->getMock();
60  $thirdValidatorObject->expects(self::once())->method('validate')->willReturn(new ‪Result());
61  $validatorConjunction->addValidator($validatorObject);
62  $validatorConjunction->addValidator($secondValidatorObject);
63  $validatorConjunction->addValidator($thirdValidatorObject);
64  $validatorConjunction->validate('some subject');
65  }
66 
71  {
72  $validatorConjunction = new ‪ConjunctionValidator([]);
73  $validatorObject = $this->getMockBuilder(ValidatorInterface::class)
74  ->setMethods(['validate', 'getOptions'])
75  ->getMock();
76  $validatorObject->expects(self::any())->method('validate')->willReturn(new ‪Result());
77  $secondValidatorObject = $this->getMockBuilder(ValidatorInterface::class)
78  ->setMethods(['validate', 'getOptions'])
79  ->getMock();
80  $secondValidatorObject->expects(self::any())->method('validate')->willReturn(new ‪Result());
81  $validatorConjunction->addValidator($validatorObject);
82  $validatorConjunction->addValidator($secondValidatorObject);
83  self::assertFalse($validatorConjunction->validate('some subject')->hasErrors());
84  }
85 
90  {
91  $validatorConjunction = new ‪ConjunctionValidator([]);
92  $validatorObject = $this->getMockBuilder(ValidatorInterface::class)
93  ->setMethods(['validate', 'getOptions'])
94  ->getMock();
95  ‪$errors = new ‪Result();
96  ‪$errors->addError(new ‪Error('Error', 123));
97  $validatorObject->expects(self::any())->method('validate')->willReturn(‪$errors);
98  $validatorConjunction->addValidator($validatorObject);
99  self::assertTrue($validatorConjunction->validate('some subject')->hasErrors());
100  }
101 
106  {
107  $validatorConjunction = new ‪ConjunctionValidator();
108  $validator1 = $this->getMockBuilder(ValidatorInterface::class)
109  ->setMethods(['validate', 'getOptions'])
110  ->getMock();
111  $validator2 = $this->getMockBuilder(ValidatorInterface::class)
112  ->setMethods(['validate', 'getOptions'])
113  ->getMock();
114  $validatorConjunction->addValidator($validator1);
115  $validatorConjunction->addValidator($validator2);
116  $validatorConjunction->removeValidator($validator1);
117  self::assertFalse($validatorConjunction->getValidators()->contains($validator1));
118  self::assertTrue($validatorConjunction->getValidators()->contains($validator2));
119  }
120 
125  {
126  $this->expectException(NoSuchValidatorException::class);
127  $this->expectExceptionCode(1207020177);
128  $validatorConjunction = new ‪ConjunctionValidator();
129  ‪$validator = $this->getMockBuilder(ValidatorInterface::class)
130  ->setMethods(['validate', 'getOptions'])
131  ->getMock();
132  $validatorConjunction->removeValidator(‪$validator);
133  }
134 
139  {
140  $validatorConjunction = new ‪ConjunctionValidator();
141  $validator1 = $this->getMockBuilder(ValidatorInterface::class)
142  ->setMethods(['validate', 'getOptions'])
143  ->getMock();
144  $validator2 = $this->getMockBuilder(ValidatorInterface::class)
145  ->setMethods(['validate', 'getOptions'])
146  ->getMock();
147  self::assertSame(0, count($validatorConjunction));
148  $validatorConjunction->addValidator($validator1);
149  $validatorConjunction->addValidator($validator2);
150  self::assertSame(2, count($validatorConjunction));
151  }
152 }
‪TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator
Definition: ConjunctionValidator.php:24
‪TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException
Definition: NoSuchValidatorException.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\ConjunctionValidatorTest\addingValidatorsToAJunctionValidatorWorks
‪addingValidatorsToAJunctionValidatorWorks()
Definition: ConjunctionValidatorTest.php:33
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\ConjunctionValidatorTest\validatorConjunctionReturnsNoErrorsIfAllJunctionedValidatorsReturnNoErrors
‪validatorConjunctionReturnsNoErrorsIfAllJunctionedValidatorsReturnNoErrors()
Definition: ConjunctionValidatorTest.php:70
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\ConjunctionValidatorTest\allValidatorsInTheConjunctionAreCalledEvenIfOneReturnsError
‪allValidatorsInTheConjunctionAreCalledEvenIfOneReturnsError()
Definition: ConjunctionValidatorTest.php:44
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\ConjunctionValidatorTest\removingAValidatorOfTheValidatorConjunctionWorks
‪removingAValidatorOfTheValidatorConjunctionWorks()
Definition: ConjunctionValidatorTest.php:105
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\ConjunctionValidatorTest\validatorConjunctionReturnsErrorsIfOneValidatorReturnsErrors
‪validatorConjunctionReturnsErrorsIfOneValidatorReturnsErrors()
Definition: ConjunctionValidatorTest.php:89
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\ConjunctionValidatorTest\removingANotExistingValidatorIndexThrowsException
‪removingANotExistingValidatorIndexThrowsException()
Definition: ConjunctionValidatorTest.php:124
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\ConjunctionValidatorTest
Definition: ConjunctionValidatorTest.php:29
‪$errors
‪$errors
Definition: annotationChecker.php:121
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator
Definition: AbstractCompositeValidatorTest.php:16
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\ConjunctionValidatorTest\countReturnsTheNumberOfValidatorsContainedInTheConjunction
‪countReturnsTheNumberOfValidatorsContainedInTheConjunction()
Definition: ConjunctionValidatorTest.php:138