FLOW3-Composer /Tests/Unit/Validation/Validator/GenericObjectValidatorTest.php

Language PHP Lines 194
MD5 Hash 5cb5ce218807c4339df894c8635f663f
Repository https://github.com/christianjul/FLOW3-Composer.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace TYPO3\FLOW3\Tests\Unit\Validation\Validator;

/*                                                                        *
 * This script belongs to the FLOW3 framework.                            *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License, either version 3   *
 * of the License, or (at your option) any later version.                 *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

require_once('AbstractValidatorTestcase.php');

/**
 * Testcase for the Generic Object Validator
 *
 */
class GenericObjectValidatorTest extends \TYPO3\FLOW3\Tests\Unit\Validation\Validator\AbstractValidatorTestcase {

	protected $validatorClassName = 'TYPO3\FLOW3\Validation\Validator\GenericObjectValidator';

	/**
	 * @test
	 */
	public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString() {
		$this->assertFalse($this->validator->validate('')->hasErrors());
	}

	/**
	 * @test
	 */
	public function validatorShouldReturnErrorsIfTheValueIsNoObjectAndNotNull() {
		$this->assertTrue($this->validator->validate('foo')->hasErrors());
	}

	/**
	 * @test
	 */
	public function validatorShouldReturnNoErrorsIfTheValueIsNull() {
		$this->assertFalse($this->validator->validate(NULL)->hasErrors());
	}

	/**
	 * @return array
	 */
	public function dataProviderForValidator() {
		$error1 = new \TYPO3\FLOW3\Error\Error('error1', 1);
		$error2 = new \TYPO3\FLOW3\Error\Error('error2', 2);

		$emptyResult1 = new \TYPO3\FLOW3\Error\Result();
		$emptyResult2 = new \TYPO3\FLOW3\Error\Result();

		$resultWithError1 = new \TYPO3\FLOW3\Error\Result();
		$resultWithError1->addError($error1);

		$resultWithError2 = new \TYPO3\FLOW3\Error\Result();
		$resultWithError2->addError($error2);

		$classNameForObjectWithPrivateProperties = 'B' . md5(uniqid(mt_rand(), TRUE));
		eval('class ' . $classNameForObjectWithPrivateProperties . '{ protected $foo = \'foovalue\'; protected $bar = \'barvalue\'; }');
		$objectWithPrivateProperties = new $classNameForObjectWithPrivateProperties();

		return array(
			// If no errors happened, this is shown
			array($objectWithPrivateProperties, $emptyResult1, $emptyResult2, array()),

			// If errors on two properties happened, they are merged together.
			array($objectWithPrivateProperties, $resultWithError1, $resultWithError2, array('foo' => array($error1), 'bar' => array($error2)))
		);
	}

	/**
	 * @test
	 * @dataProvider dataProviderForValidator
	 */
	public function validateChecksAllPropertiesForWhichAPropertyValidatorExists($mockObject, $validationResultForFoo, $validationResultForBar, $errors) {

		$validatorForFoo = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface');
		$validatorForFoo->expects($this->once())->method('validate')->with('foovalue')->will($this->returnValue($validationResultForFoo));

		$validatorForBar = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface');
		$validatorForBar->expects($this->once())->method('validate')->with('barvalue')->will($this->returnValue($validationResultForBar));

		$this->validator->addPropertyValidator('foo', $validatorForFoo);
		$this->validator->addPropertyValidator('bar', $validatorForBar);
		$this->assertEquals($errors, $this->validator->validate($mockObject)->getFlattenedErrors());
	}

	/**
	 * @test
	 */
	public function validateCanHandleRecursiveTargetsWithoutEndlessLooping() {
		$classNameA = 'B' . md5(uniqid(mt_rand(), TRUE));
		eval('class ' . $classNameA . '{ public $b; }');
		$classNameB = 'B' . md5(uniqid(mt_rand(), TRUE));
		eval('class ' . $classNameB . '{ public $a; }');
		$A = new $classNameA();
		$B = new $classNameB();
		$A->b = $B;
		$B->a = $A;

		$aValidator = new \TYPO3\FLOW3\Validation\Validator\GenericObjectValidator(array());
		$bValidator = new \TYPO3\FLOW3\Validation\Validator\GenericObjectValidator(array());
		$aValidator->addPropertyValidator('b', $bValidator);
		$bValidator->addPropertyValidator('a', $aValidator);

		$this->assertFalse($aValidator->validate($A)->hasErrors());
	}

	/**
	 * @test
	 */
	public function validateDetectsFailuresInRecursiveTargetsI() {
		$classNameA = 'A' . md5(uniqid(mt_rand(), TRUE));
		eval('class ' . $classNameA . '{ public $b; }');
		$classNameB = 'B' . md5(uniqid(mt_rand(), TRUE));
		eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
		$A = new $classNameA();
		$B = new $classNameB();
		$A->b = $B;
		$B->a = $A;

		$aValidator = $this->getValidator();
		$bValidator = $this->getValidator();

		$aValidator->addPropertyValidator('b', $bValidator);
		$bValidator->addPropertyValidator('a', $aValidator);

		$error = new \TYPO3\FLOW3\Error\Error('error1', 123);
		$result = new \TYPO3\FLOW3\Error\Result();
		$result->addError($error);
		$mockUuidValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface');
		$mockUuidValidator->expects($this->any())->method('validate')->with(0xF)->will($this->returnValue($result));
		$bValidator->addPropertyValidator('uuid', $mockUuidValidator);

		$this->assertSame(array('b.uuid' => array($error)), $aValidator->validate($A)->getFlattenedErrors());
	}

	/**
	 * @test
	 */
	public function validateDetectsFailuresInRecursiveTargetsII() {
		$classNameA = 'A' . md5(uniqid(mt_rand(), TRUE));
		eval('class ' . $classNameA . '{ public $b; public $uuid = 0xF; }');
		$classNameB = 'B' . md5(uniqid(mt_rand(), TRUE));
		eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
		$A = new $classNameA();
		$B = new $classNameB();
		$A->b = $B;
		$B->a = $A;

		$aValidator = $this->getValidator();
		$bValidator = $this->getValidator();

		$aValidator->addPropertyValidator('b', $bValidator);
		$bValidator->addPropertyValidator('a', $aValidator);

		$error1 = new \TYPO3\FLOW3\Error\Error('error1', 123);
		$result1 = new \TYPO3\FLOW3\Error\Result();
		$result1->addError($error1);
		$mockUuidValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface');
		$mockUuidValidator->expects($this->any())->method('validate')->with(0xF)->will($this->returnValue($result1));
		$aValidator->addPropertyValidator('uuid', $mockUuidValidator);
		$bValidator->addPropertyValidator('uuid', $mockUuidValidator);

		$this->assertSame(array('b.uuid' => array($error1), 'uuid' => array($error1)), $aValidator->validate($A)->getFlattenedErrors());
	}

	/**
	 * @test
	 */
	public function objectsAreValidatedOnlyOnce() {
		$className = 'A' . md5(uniqid(mt_rand(), TRUE));
		eval('class ' . $className . '{ public $integer = 1; }');
		$object = new $className();

		$integerValidator = $this->getAccessibleMock('TYPO3\FLOW3\Validation\Validator\IntegerValidator');
		$matcher = $this->any();
		$integerValidator->expects($matcher)->method('validate')->with(1)->will($this->returnValue(new \TYPO3\FLOW3\Error\Result()));

		$validator = $this->getValidator();
		$validator->addPropertyValidator('integer', $integerValidator);

			// Call the validation twice
		$validator->validate($object);
		$validator->validate($object);

		$this->assertEquals(1, $matcher->getInvocationCount());
	}
}

?>
Back to Top