Raised DBAL version from 1.1.5 to 1.1.6
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Validation / Validator / ConjunctionValidator_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Testcase for the Conjunction Validators
30 *
31 * @package Extbase
32 * @subpackage extbase
33 * @version $Id: ConjunctionValidator_testcase.php 1729 2009-11-25 21:37:20Z stucki $
34 */
35 class Tx_Extbase_Validation_Validator_ConjunctionValidator_testcase extends Tx_Extbase_BaseTestCase {
36
37 /**
38 * @test
39 */
40 public function addingValidatorsToAJunctionValidatorWorks() {
41 $proxyClassName = $this->buildAccessibleProxy('Tx_Extbase_Validation_Validator_ConjunctionValidator');
42 $conjunctionValidator = new $proxyClassName;
43
44 $mockValidator = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
45 $conjunctionValidator->addValidator($mockValidator);
46 $this->assertTrue($conjunctionValidator->_get('validators')->contains($mockValidator));
47 }
48
49 /**
50 * @test
51 */
52 public function allValidatorsInTheConjunctionAreCalledEvenIfOneReturnsFalse() {
53 $validatorConjunction = new Tx_Extbase_Validation_Validator_ConjunctionValidator();
54 $validatorObject = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
55 $validatorObject->expects($this->once())->method('isValid')->will($this->returnValue(TRUE));
56
57 $secondValidatorObject = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
58 $secondValidatorObject->expects($this->once())->method('isValid')->will($this->returnValue(FALSE));
59 $secondValidatorObject->expects($this->any())->method('getErrors')->will($this->returnValue(array()));
60
61 $thirdValidatorObject = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
62 $thirdValidatorObject->expects($this->once())->method('isValid')->will($this->returnValue(TRUE));
63
64 $validatorConjunction->addValidator($validatorObject);
65 $validatorConjunction->addValidator($secondValidatorObject);
66 $validatorConjunction->addValidator($thirdValidatorObject);
67
68 $validatorConjunction->isValid('some subject');
69 }
70
71 /**
72 * @test
73 */
74 public function validatorConjunctionReturnsTrueIfAllJunctionedValidatorsReturnTrue() {
75 $validatorConjunction = new Tx_Extbase_Validation_Validator_ConjunctionValidator();
76 $validatorObject = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
77 $validatorObject->expects($this->any())->method('isValid')->will($this->returnValue(TRUE));
78
79 $secondValidatorObject = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
80 $secondValidatorObject->expects($this->any())->method('isValid')->will($this->returnValue(TRUE));
81
82 $validatorConjunction->addValidator($validatorObject);
83 $validatorConjunction->addValidator($secondValidatorObject);
84
85 $this->assertTrue($validatorConjunction->isValid('some subject'));
86 }
87
88 /**
89 * @test
90 */
91 public function validatorConjunctionReturnsFalseIfOneValidatorReturnsFalse() {
92 $validatorConjunction = new Tx_Extbase_Validation_Validator_ConjunctionValidator();
93 $validatorObject = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
94 $validatorObject->expects($this->any())->method('isValid')->will($this->returnValue(FALSE));
95 $validatorObject->expects($this->any())->method('getErrors')->will($this->returnValue(array()));
96
97 $validatorConjunction->addValidator($validatorObject);
98
99 $this->assertFalse($validatorConjunction->isValid('some subject'));
100 }
101
102 /**
103 * @test
104 */
105 public function removingAValidatorOfTheValidatorConjunctionWorks() {
106 $validatorConjunction = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_Validator_ConjunctionValidator'), array('dummy'), array(), '', TRUE);
107
108 $validator1 = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
109 $validator2 = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
110
111 $validatorConjunction->addValidator($validator1);
112 $validatorConjunction->addValidator($validator2);
113
114 $validatorConjunction->removeValidator($validator1);
115
116 $this->assertFalse($validatorConjunction->_get('validators')->contains($validator1));
117 $this->assertTrue($validatorConjunction->_get('validators')->contains($validator2));
118 }
119
120 /**
121 * @test
122 * @expectedException Tx_Extbase_Validation_Exception_NoSuchValidator
123 */
124 public function removingANotExistingValidatorIndexThrowsException() {
125 $validatorConjunction = new Tx_Extbase_Validation_Validator_ConjunctionValidator();
126 $validator = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
127 $validatorConjunction->removeValidator($validator);
128 }
129
130 /**
131 * @test
132 */
133 public function countReturnesTheNumberOfValidatorsContainedInTheConjunction() {
134 $validatorConjunction = new Tx_Extbase_Validation_Validator_ConjunctionValidator();
135
136 $validator1 = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
137 $validator2 = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface');
138
139 $this->assertSame(0, count($validatorConjunction));
140
141 $validatorConjunction->addValidator($validator1);
142 $validatorConjunction->addValidator($validator2);
143
144 $this->assertSame(2, count($validatorConjunction));
145 }
146 }
147
148 ?>