PageRenderTime 113ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/Unit/Validation/ValidatorResolverTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 481 lines | 316 code | 79 blank | 86 comment | 0 complexity | 9245a47b94927bf454e41c24f5ae3ee0 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Tests\Unit\Validation;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. /**
  13. * Testcase for the validator resolver
  14. *
  15. */
  16. class ValidatorResolverTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  17. /**
  18. * @var \TYPO3\FLOW3\Validation\ValidatorResolver
  19. */
  20. protected $validatorResolver;
  21. /**
  22. * @var \TYPO3\FLOW3\Object\ObjectManagerInterface
  23. */
  24. protected $mockObjectManager;
  25. /**
  26. * @var \TYPO3\FLOW3\Reflection\ReflectionService
  27. */
  28. protected $mockReflectionService;
  29. public function setUp() {
  30. $this->mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManagerInterface');
  31. $this->mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService');
  32. $this->validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('dummy'));
  33. $this->validatorResolver->_set('objectManager', $this->mockObjectManager);
  34. $this->validatorResolver->_set('reflectionService', $this->mockReflectionService);
  35. }
  36. /**
  37. * @test
  38. */
  39. public function resolveValidatorObjectNameReturnsFalseIfValidatorCantBeResolved() {
  40. $this->mockObjectManager->expects($this->at(0))->method('isRegistered')->with('Foo')->will($this->returnValue(FALSE));
  41. $this->mockObjectManager->expects($this->at(1))->method('isRegistered')->with('TYPO3\FLOW3\Validation\Validator\FooValidator')->will($this->returnValue(FALSE));
  42. $this->assertSame(FALSE, $this->validatorResolver->_call('resolveValidatorObjectName', 'Foo'));
  43. }
  44. /**
  45. * @test
  46. */
  47. public function resolveValidatorObjectNameReturnsTheGivenArgumentIfAnObjectOfThatNameIsRegisteredAndImplementsValidatorInterface() {
  48. $this->mockObjectManager->expects($this->any())->method('isRegistered')->with('Foo')->will($this->returnValue(TRUE));
  49. $this->mockReflectionService->expects($this->atLeastOnce())->method('isClassImplementationOf')->with('Foo', 'TYPO3\FLOW3\Validation\Validator\ValidatorInterface')->will($this->returnValue(TRUE));
  50. $this->assertSame('Foo', $this->validatorResolver->_call('resolveValidatorObjectName', 'Foo'));
  51. }
  52. /**
  53. * @test
  54. */
  55. public function resolveValidatorObjectNameReturnsFalseIfAnObjectOfTheArgumentNameIsRegisteredButDoesNotImplementValidatorInterface() {
  56. $this->mockObjectManager->expects($this->any())->method('isRegistered')->with('Foo')->will($this->returnValue(TRUE));
  57. $this->mockReflectionService->expects($this->atLeastOnce())->method('isClassImplementationOf')->with('Foo', 'TYPO3\FLOW3\Validation\Validator\ValidatorInterface')->will($this->returnValue(FALSE));
  58. $this->assertFalse($this->validatorResolver->_call('resolveValidatorObjectName', 'Foo'));
  59. }
  60. /**
  61. * @test
  62. */
  63. public function resolveValidatorObjectNameRemovesALeadingBackslashFromThePassedType() {
  64. $this->mockObjectManager->expects($this->any())->method('isRegistered')->with('Foo\\Bar')->will($this->returnValue(TRUE));
  65. $this->mockReflectionService->expects($this->atLeastOnce())->method('isClassImplementationOf')->with('Foo\\Bar', 'TYPO3\FLOW3\Validation\Validator\ValidatorInterface')->will($this->returnValue(TRUE));
  66. $this->assertSame('Foo\\Bar', $this->validatorResolver->_call('resolveValidatorObjectName', '\\Foo\\Bar'));
  67. }
  68. /**
  69. * @test
  70. */
  71. public function resolveValidatorObjectNameCanResolveShorthandValidatornames() {
  72. $this->mockObjectManager->expects($this->at(0))->method('isRegistered')->with('Mypkg:My')->will($this->returnValue(FALSE));
  73. $this->mockObjectManager->expects($this->at(1))->method('isRegistered')->with('Mypkg\Validation\Validator\\MyValidator')->will($this->returnValue(TRUE));
  74. $this->mockReflectionService->expects($this->atLeastOnce())->method('isClassImplementationOf')->with('Mypkg\Validation\Validator\\MyValidator', 'TYPO3\FLOW3\Validation\Validator\ValidatorInterface')->will($this->returnValue(TRUE));
  75. $this->assertSame('Mypkg\Validation\Validator\\MyValidator', $this->validatorResolver->_call('resolveValidatorObjectName', 'Mypkg:My'));
  76. }
  77. /**
  78. * @test
  79. */
  80. public function resolveValidatorObjectNameCanResolveShorthandValidatornamesForHierarchicalPackages() {
  81. $this->mockObjectManager->expects($this->at(0))->method('isRegistered')->with('Mypkg.Foo:My')->will($this->returnValue(FALSE));
  82. $this->mockObjectManager->expects($this->at(1))->method('isRegistered')->with('Mypkg\Foo\Validation\Validator\\MyValidator')->will($this->returnValue(TRUE));
  83. $this->mockReflectionService->expects($this->atLeastOnce())->method('isClassImplementationOf')->with('Mypkg\Foo\Validation\Validator\\MyValidator', 'TYPO3\FLOW3\Validation\Validator\ValidatorInterface')->will($this->returnValue(TRUE));
  84. $this->assertSame('Mypkg\Foo\Validation\Validator\\MyValidator', $this->validatorResolver->_call('resolveValidatorObjectName', 'Mypkg.Foo:My'));
  85. }
  86. /**
  87. * @test
  88. */
  89. public function resolveValidatorObjectNameCanResolveShortNamesOfBuiltInValidators() {
  90. $this->mockObjectManager->expects($this->at(0))->method('isRegistered')->with('Foo')->will($this->returnValue(FALSE));
  91. $this->mockObjectManager->expects($this->at(1))->method('isRegistered')->with('TYPO3\FLOW3\Validation\Validator\FooValidator')->will($this->returnValue(TRUE));
  92. $this->mockReflectionService->expects($this->atLeastOnce())->method('isClassImplementationOf')->with('TYPO3\FLOW3\Validation\Validator\FooValidator', 'TYPO3\FLOW3\Validation\Validator\ValidatorInterface')->will($this->returnValue(TRUE));
  93. $this->assertSame('TYPO3\FLOW3\Validation\Validator\FooValidator', $this->validatorResolver->_call('resolveValidatorObjectName', 'Foo'));
  94. }
  95. /**
  96. * @test
  97. */
  98. public function createValidatorResolvesAndReturnsAValidatorAndPassesTheGivenOptions() {
  99. $className = 'Test' . md5(uniqid(mt_rand(), TRUE));
  100. eval("class $className implements \TYPO3\FLOW3\Validation\Validator\ValidatorInterface {" . '
  101. protected $options = array();
  102. public function __construct(array $options = array()) {
  103. $this->options = $options;
  104. }
  105. public function validate($subject) {}
  106. public function getOptions() { return $this->options; }
  107. }');
  108. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManagerInterface');
  109. $mockObjectManager->expects($this->any())->method('getScope')->with($className)->will($this->returnValue(\TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_PROTOTYPE));
  110. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('resolveValidatorObjectName'));
  111. $validatorResolver->_set('objectManager', $mockObjectManager);
  112. $validatorResolver->expects($this->once())->method('resolveValidatorObjectName')->with($className)->will($this->returnValue($className));
  113. $validator = $validatorResolver->createValidator($className, array('foo' => 'bar'));
  114. $this->assertInstanceOf($className, $validator);
  115. $this->assertEquals(array('foo' => 'bar'), $validator->getOptions());
  116. }
  117. /**
  118. * @test
  119. */
  120. public function createValidatorReturnsNullIfAValidatorCouldNotBeResolved() {
  121. $validatorResolver = $this->getMock('TYPO3\FLOW3\Validation\ValidatorResolver',array('resolveValidatorObjectName'), array(), '', FALSE);
  122. $validatorResolver->expects($this->once())->method('resolveValidatorObjectName')->with('Foo')->will($this->returnValue(FALSE));
  123. $validator = $validatorResolver->createValidator('Foo', array('foo' => 'bar'));
  124. $this->assertNull($validator);
  125. }
  126. /**
  127. * @test
  128. */
  129. public function buildBaseValidatorCachesTheResultOfTheBuildBaseValidatorConjunctionCalls() {
  130. $result1 = $this->validatorResolver->getBaseValidatorConjunction('TYPO3\Virtual\Foo');
  131. $this->assertInstanceOf('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator', $result1, '#1');
  132. $result2 = $this->validatorResolver->getBaseValidatorConjunction('TYPO3\Virtual\Foo');
  133. $this->assertSame($result1, $result2, '#2');
  134. }
  135. /**
  136. * @test
  137. */
  138. public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments() {
  139. $mockController = $this->getAccessibleMock('TYPO3\FLOW3\Mvc\Controller\ActionController', array('fooAction'), array(), '', FALSE);
  140. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  141. $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue(array()));
  142. $this->validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('createValidator'), array(), '', FALSE);
  143. $this->validatorResolver->_set('reflectionService', $mockReflectionService);
  144. $result = $this->validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction');
  145. $this->assertSame(array(), $result);
  146. }
  147. /**
  148. * @test
  149. */
  150. public function buildMethodArgumentsValidatorConjunctionsBuildsAConjunctionFromValidateAnnotationsOfTheSpecifiedMethod() {
  151. $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
  152. $methodParameters = array(
  153. 'arg1' => array(
  154. 'type' => 'string'
  155. ),
  156. 'arg2' => array(
  157. 'type' => 'array'
  158. )
  159. );
  160. $validateAnnotations = array(
  161. new \TYPO3\FLOW3\Annotations\Validate(array(
  162. 'type' => 'Foo',
  163. 'options' => array('bar' => 'baz'),
  164. 'argumentName' => '$arg1'
  165. )),
  166. new \TYPO3\FLOW3\Annotations\Validate(array(
  167. 'type' => 'Bar',
  168. 'argumentName' => '$arg1'
  169. )),
  170. new \TYPO3\FLOW3\Annotations\Validate(array(
  171. 'type' => 'TYPO3\TestPackage\Quux',
  172. 'argumentName' => '$arg2'
  173. )),
  174. );
  175. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  176. $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
  177. $mockReflectionService->expects($this->once())->method('getMethodAnnotations')->with(get_class($mockObject), 'fooAction', 'TYPO3\FLOW3\Annotations\Validate')->will($this->returnValue($validateAnnotations));
  178. $mockStringValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface', array(), array(), '', FALSE);
  179. $mockArrayValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface', array(), array(), '', FALSE);
  180. $mockFooValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface', array(), array(), '', FALSE);
  181. $mockBarValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface', array(), array(), '', FALSE);
  182. $mockQuuxValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface', array(), array(), '', FALSE);
  183. $conjunction1 = $this->getMock('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator', array(), array(), '', FALSE);
  184. $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
  185. $conjunction1->expects($this->at(1))->method('addValidator')->with($mockFooValidator);
  186. $conjunction1->expects($this->at(2))->method('addValidator')->with($mockBarValidator);
  187. $conjunction2 = $this->getMock('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator', array(), array(), '', FALSE);
  188. $conjunction2->expects($this->at(0))->method('addValidator')->with($mockArrayValidator);
  189. $conjunction2->expects($this->at(1))->method('addValidator')->with($mockQuuxValidator);
  190. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('createValidator'), array(), '', FALSE);
  191. $validatorResolver->expects($this->at(0))->method('createValidator')->with('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator')->will($this->returnValue($conjunction1));
  192. $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
  193. $validatorResolver->expects($this->at(2))->method('createValidator')->with('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator')->will($this->returnValue($conjunction2));
  194. $validatorResolver->expects($this->at(3))->method('createValidator')->with('array')->will($this->returnValue($mockArrayValidator));
  195. $validatorResolver->expects($this->at(4))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockFooValidator));
  196. $validatorResolver->expects($this->at(5))->method('createValidator')->with('Bar')->will($this->returnValue($mockBarValidator));
  197. $validatorResolver->expects($this->at(6))->method('createValidator')->with('TYPO3\TestPackage\Quux')->will($this->returnValue($mockQuuxValidator));
  198. $validatorResolver->_set('reflectionService', $mockReflectionService);
  199. $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
  200. $this->assertEquals(array('arg1' => $conjunction1, 'arg2' => $conjunction2), $result);
  201. }
  202. /**
  203. * @test
  204. */
  205. public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyConjunctionIfNoValidatorIsFoundForMethodParameter() {
  206. $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
  207. $methodParameters = array(
  208. 'arg' => array(
  209. 'type' => 'FLOW8\Blog\Domain\Model\Blog'
  210. )
  211. );
  212. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  213. $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
  214. $mockReflectionService->expects($this->once())->method('getMethodAnnotations')->with(get_class($mockObject), 'fooAction', 'TYPO3\FLOW3\Annotations\Validate')->will($this->returnValue(array()));
  215. $conjunction = $this->getMock('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator', array(), array(), '', FALSE);
  216. $conjunction->expects($this->never())->method('addValidator');
  217. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('createValidator'), array(), '', FALSE);
  218. $validatorResolver->expects($this->at(0))->method('createValidator')->with('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator')->will($this->returnValue($conjunction));
  219. $validatorResolver->expects($this->at(1))->method('createValidator')->with('FLOW8\Blog\Domain\Validator\BlogValidator')->will($this->returnValue(NULL));
  220. $validatorResolver->_set('reflectionService', $mockReflectionService);
  221. $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
  222. }
  223. /**
  224. * @test
  225. * @expectedException TYPO3\FLOW3\Validation\Exception\InvalidValidationConfigurationException
  226. */
  227. public function buildMethodArgumentsValidatorConjunctionsThrowsExceptionIfValidationAnnotationForNonExistingArgumentExists() {
  228. $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
  229. $methodParameters = array(
  230. 'arg1' => array(
  231. 'type' => 'string'
  232. )
  233. );
  234. $validateAnnotations = array(
  235. new \TYPO3\FLOW3\Annotations\Validate(array(
  236. 'type' => 'TYPO3\TestPackage\Quux',
  237. 'argumentName' => '$arg2'
  238. )),
  239. );
  240. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  241. $mockReflectionService->expects($this->once())->method('getMethodAnnotations')->with(get_class($mockObject), 'fooAction', 'TYPO3\FLOW3\Annotations\Validate')->will($this->returnValue($validateAnnotations));
  242. $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
  243. $mockStringValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface', array(), array(), '', FALSE);
  244. $mockQuuxValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface', array(), array(), '', FALSE);
  245. $conjunction1 = $this->getMock('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator', array(), array(), '', FALSE);
  246. $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
  247. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('createValidator'), array(), '', FALSE);
  248. $validatorResolver->expects($this->at(0))->method('createValidator')->with('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator')->will($this->returnValue($conjunction1));
  249. $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
  250. $validatorResolver->expects($this->at(2))->method('createValidator')->with('TYPO3\TestPackage\Quux')->will($this->returnValue($mockQuuxValidator));
  251. $validatorResolver->_set('reflectionService', $mockReflectionService);
  252. $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
  253. }
  254. /**
  255. * @test
  256. */
  257. public function buildBaseValidatorConjunctionAddsCustomValidatorToTheReturnedConjunction() {
  258. $modelClassName = 'Page' . md5(uniqid(mt_rand(), TRUE));
  259. $validatorClassName = 'Domain\Validator\Content\\' . $modelClassName . 'Validator';
  260. eval('namespace Domain\Model\Content; class ' . $modelClassName . '{}');
  261. $modelClassName = 'Domain\Model\Content\\' . $modelClassName;
  262. $mockReflectionService = $this->getMock('\TYPO3\FLOW3\Reflection\ReflectionService');
  263. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->will($this->returnValue(array()));
  264. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator'));
  265. $validatorResolver->_set('reflectionService', $mockReflectionService);
  266. $validatorResolver->expects($this->once())->method('createValidator')->with($validatorClassName)->will($this->returnValue(new \TYPO3\FLOW3\Validation\Validator\EmailAddressValidator()));
  267. $validatorResolver->_call('buildBaseValidatorConjunction', $modelClassName, $modelClassName, array('Default'));
  268. $builtValidators = $validatorResolver->_get('baseValidatorConjunctions');
  269. $this->assertFalse($builtValidators[$modelClassName]->validate('foo@example.com')->hasErrors());
  270. $this->assertTrue($builtValidators[$modelClassName]->validate('foo')->hasErrors());
  271. }
  272. /**
  273. * @test
  274. */
  275. public function buildBaseValidatorConjunctionAddsValidatorsOnlyForPropertiesHoldingPrototypes() {
  276. $entityClassName = 'Entity' . md5(uniqid(mt_rand(), TRUE));
  277. eval('class ' . $entityClassName . '{}');
  278. $otherClassName = 'Other' . md5(uniqid(mt_rand(), TRUE));
  279. eval('class ' . $otherClassName . '{}');
  280. $modelClassName = 'Model' . md5(uniqid(mt_rand(), TRUE));
  281. eval('class ' . $modelClassName . '{}');
  282. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManagerInterface', array(), array(), '', FALSE);
  283. $mockObjectManager->expects($this->any())->method('isRegistered')->will($this->returnValue(TRUE));
  284. $mockObjectManager->expects($this->at(1))->method('getScope')->with($entityClassName)->will($this->returnValue(\TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_PROTOTYPE));
  285. $mockObjectManager->expects($this->at(3))->method('getScope')->with($otherClassName)->will($this->returnValue(NULL));
  286. $mockReflectionService = $this->getMock('\TYPO3\FLOW3\Reflection\ReflectionService');
  287. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->will($this->returnValue(array('entityProperty', 'otherProperty')));
  288. $mockReflectionService->expects($this->at(1))->method('getPropertyTagsValues')->with($modelClassName, 'entityProperty')->will($this->returnValue(array('var' => array($entityClassName))));
  289. $mockReflectionService->expects($this->at(2))->method('getPropertyAnnotations')->with($modelClassName, 'entityProperty', 'TYPO3\FLOW3\Annotations\Validate')->will($this->returnValue(array()));
  290. $mockReflectionService->expects($this->at(3))->method('getPropertyTagsValues')->with($modelClassName, 'otherProperty')->will($this->returnValue(array('var' => array($otherClassName))));
  291. $mockReflectionService->expects($this->at(4))->method('getPropertyAnnotations')->with($modelClassName, 'otherProperty', 'TYPO3\FLOW3\Annotations\Validate')->will($this->returnValue(array()));
  292. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator', 'getBaseValidatorConjunction'));
  293. $validatorResolver->_set('objectManager', $mockObjectManager);
  294. $validatorResolver->_set('reflectionService', $mockReflectionService);
  295. $validatorResolver->expects($this->once())->method('getBaseValidatorConjunction')->will($this->returnValue($this->getMock('TYPO3\FLOW3\Validation\Validator\ValidatorInterface')));
  296. $validatorResolver->_call('buildBaseValidatorConjunction', $modelClassName, $modelClassName, array('Default'));
  297. }
  298. /**
  299. * @test
  300. */
  301. public function buildBaseValidatorConjunctionReturnsNullIfNoValidatorBuilt() {
  302. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManagerInterface', array(), array(), '', FALSE);
  303. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('dummy'));
  304. $validatorResolver->_set('objectManager', $mockObjectManager);
  305. $this->assertNull($validatorResolver->_call('buildBaseValidatorConjunction', 'NonExistingClassName', 'NonExistingClassName', array('Default')));
  306. }
  307. /**
  308. * @test
  309. */
  310. public function buildBaseValidatorConjunctionAddsValidatorsDefinedByAnnotationsInTheClassToTheReturnedConjunction() {
  311. $mockObject = $this->getMock('stdClass');
  312. $className = get_class($mockObject);
  313. $propertyTagsValues = array(
  314. 'foo' => array(
  315. 'var' => array('string'),
  316. ),
  317. 'bar' => array(
  318. 'var' => array('integer'),
  319. ),
  320. 'baz' => array(
  321. 'var' => array('array<TYPO3\TestPackage\Quux>')
  322. )
  323. );
  324. $validateAnnotations = array(
  325. 'foo' => array(
  326. new \TYPO3\FLOW3\Annotations\Validate(array(
  327. 'type' => 'Foo',
  328. 'options' => array('bar' => 'baz'),
  329. )),
  330. new \TYPO3\FLOW3\Annotations\Validate(array(
  331. 'type' => 'Bar',
  332. )),
  333. new \TYPO3\FLOW3\Annotations\Validate(array(
  334. 'type' => 'Baz',
  335. )),
  336. ),
  337. 'bar' => array(
  338. new \TYPO3\FLOW3\Annotations\Validate(array(
  339. 'type' => 'TYPO3\TestPackage\Quux',
  340. )),
  341. ),
  342. );
  343. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  344. $mockReflectionService->expects($this->at(0))->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('foo', 'bar', 'baz')));
  345. $mockReflectionService->expects($this->at(1))->method('getPropertyTagsValues')->with($className, 'foo')->will($this->returnValue($propertyTagsValues['foo']));
  346. $mockReflectionService->expects($this->at(2))->method('getPropertyAnnotations')->with(get_class($mockObject), 'foo', 'TYPO3\FLOW3\Annotations\Validate')->will($this->returnValue($validateAnnotations['foo']));
  347. $mockReflectionService->expects($this->at(3))->method('getPropertyTagsValues')->with($className, 'bar')->will($this->returnValue($propertyTagsValues['bar']));
  348. $mockReflectionService->expects($this->at(4))->method('getPropertyAnnotations')->with(get_class($mockObject), 'bar', 'TYPO3\FLOW3\Annotations\Validate')->will($this->returnValue($validateAnnotations['bar']));
  349. $mockReflectionService->expects($this->at(5))->method('getPropertyTagsValues')->with($className, 'baz')->will($this->returnValue($propertyTagsValues['baz']));
  350. $mockReflectionService->expects($this->at(6))->method('getPropertyAnnotations')->with(get_class($mockObject), 'baz', 'TYPO3\FLOW3\Annotations\Validate')->will($this->returnValue(array()));
  351. $mockObjectValidator = $this->getMock('TYPO3\FLOW3\Validation\Validator\GenericObjectValidator', array(), array(), '', FALSE);
  352. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator'));
  353. $validatorResolver->_set('reflectionService', $mockReflectionService);
  354. $validatorResolver->expects($this->at(0))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockObjectValidator));
  355. $validatorResolver->expects($this->at(1))->method('createValidator')->with('Bar')->will($this->returnValue($mockObjectValidator));
  356. $validatorResolver->expects($this->at(2))->method('createValidator')->with('Baz')->will($this->returnValue($mockObjectValidator));
  357. $validatorResolver->expects($this->at(3))->method('createValidator')->with('TYPO3\TestPackage\Quux')->will($this->returnValue($mockObjectValidator));
  358. $validatorResolver->expects($this->at(4))->method('createValidator')->with('TYPO3\FLOW3\Validation\Validator\CollectionValidator', array('elementType' => 'TYPO3\TestPackage\Quux', 'validationGroups' => array('Default')))->will($this->returnValue($mockObjectValidator));
  359. $validatorResolver->_call('buildBaseValidatorConjunction', $className . 'Default', $className, array('Default'));
  360. $builtValidators = $validatorResolver->_get('baseValidatorConjunctions');
  361. $this->assertInstanceOf('TYPO3\FLOW3\Validation\Validator\ConjunctionValidator', $builtValidators[$className . 'Default']);
  362. }
  363. /**
  364. * @test
  365. */
  366. public function resolveValidatorObjectNameCallsGetValidatorType() {
  367. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManagerInterface');
  368. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('getValidatorType'));
  369. $validatorResolver->_set('objectManager', $mockObjectManager);
  370. $validatorResolver->expects($this->once())->method('getValidatorType')->with('someDataType');
  371. $validatorResolver->_call('resolveValidatorObjectName', 'someDataType');
  372. }
  373. /**
  374. * @test
  375. */
  376. public function getValidatorTypeCorrectlyRenamesPhpDataTypes() {
  377. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManagerInterface');
  378. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('dummy'));
  379. $validatorResolver->_set('objectManager', $mockObjectManager);
  380. $this->assertEquals('Integer', $validatorResolver->_call('getValidatorType', 'integer'));
  381. $this->assertEquals('Integer', $validatorResolver->_call('getValidatorType', 'int'));
  382. $this->assertEquals('String', $validatorResolver->_call('getValidatorType', 'string'));
  383. $this->assertEquals('Array', $validatorResolver->_call('getValidatorType', 'array'));
  384. $this->assertEquals('Float', $validatorResolver->_call('getValidatorType', 'float'));
  385. $this->assertEquals('Float', $validatorResolver->_call('getValidatorType', 'double'));
  386. $this->assertEquals('Boolean', $validatorResolver->_call('getValidatorType', 'boolean'));
  387. $this->assertEquals('Boolean', $validatorResolver->_call('getValidatorType', 'bool'));
  388. $this->assertEquals('Number', $validatorResolver->_call('getValidatorType', 'number'));
  389. $this->assertEquals('Number', $validatorResolver->_call('getValidatorType', 'numeric'));
  390. }
  391. /**
  392. * @test
  393. */
  394. public function getValidatorTypeRenamesMixedToRaw() {
  395. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManagerInterface');
  396. $validatorResolver = $this->getAccessibleMock('TYPO3\FLOW3\Validation\ValidatorResolver', array('dummy'));
  397. $validatorResolver->_set('objectManager', $mockObjectManager);
  398. $this->assertEquals('Raw', $validatorResolver->_call('getValidatorType', 'mixed'));
  399. }
  400. }
  401. ?>