PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Unit/Validation/Validator/CollectionValidatorTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 111 lines | 60 code | 21 blank | 30 comment | 0 complexity | 8802953e7ba9534c631df92bcc603727 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Tests\Unit\Validation\Validator;
  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. require_once('AbstractValidatorTestcase.php');
  13. /**
  14. * Testcase for the collection validator
  15. *
  16. */
  17. class CollectionValidatorTest extends \TYPO3\FLOW3\Tests\Unit\Validation\Validator\AbstractValidatorTestcase {
  18. protected $validatorClassName = 'TYPO3\FLOW3\Validation\Validator\CollectionValidator';
  19. protected $mockValidatorResolver;
  20. public function setUp() {
  21. parent::setUp();
  22. $this->mockValidatorResolver = $this->getMock('TYPO3\FLOW3\Validation\ValidatorResolver', array(), array(), '', FALSE);
  23. $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
  24. }
  25. /**
  26. * @test
  27. */
  28. public function collectionValidatorReturnsNoErrorsForANullValue() {
  29. $this->assertFalse($this->validator->validate(NULL)->hasErrors());
  30. }
  31. /**
  32. * @test
  33. */
  34. public function collectionValidatorFailsForAValueNotBeingACollection() {
  35. $this->assertTrue($this->validator->validate(new \StdClass())->hasErrors());
  36. }
  37. /**
  38. * @test
  39. */
  40. public function collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator() {
  41. $this->validator->_set('options', array('elementValidator' => 'EmailAddress'));
  42. $this->mockValidatorResolver->expects($this->exactly(4))->method('createValidator')->with('EmailAddress')->will($this->returnValue(new \TYPO3\FLOW3\Validation\Validator\EmailAddressValidator()));
  43. $arrayOfEmailAddresses = array(
  44. 'andreas.foerthner@netlogix.de',
  45. 'not a valid address',
  46. 'robert@typo3.org',
  47. 'also not valid'
  48. );
  49. $result = $this->validator->validate($arrayOfEmailAddresses);
  50. $this->assertTrue($result->hasErrors());
  51. $this->assertEquals(2, count($result->getFlattenedErrors()));
  52. }
  53. /**
  54. * @test
  55. */
  56. public function collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping() {
  57. $classNameA = 'A' . md5(uniqid(mt_rand(), TRUE));
  58. eval('class ' . $classNameA . '{ public $b = array(); public $integer = 5; }');
  59. $classNameB = 'B' . md5(uniqid(mt_rand(), TRUE));
  60. eval('class ' . $classNameB . '{ public $a; public $c; public $integer = "Not an integer"; }');
  61. $A = new $classNameA();
  62. $B = new $classNameB();
  63. $A->b = array($B);
  64. $B->a = $A;
  65. $B->c = array($A);
  66. $this->mockValidatorResolver->expects($this->any())->method('createValidator')->with('Integer')->will($this->returnValue(new \TYPO3\FLOW3\Validation\Validator\IntegerValidator()));
  67. $this->mockValidatorResolver->expects($this->any())->method('buildBaseValidatorConjunction')->will($this->returnValue(new \TYPO3\FLOW3\Validation\Validator\GenericObjectValidator()));
  68. // Create validators
  69. $aValidator = new \TYPO3\FLOW3\Validation\Validator\GenericObjectValidator(array());
  70. $this->validator->_set('options', array('elementValidator' => 'Integer'));
  71. $integerValidator = new \TYPO3\FLOW3\Validation\Validator\IntegerValidator(array());
  72. // Add validators to properties
  73. $aValidator->addPropertyValidator('b', $this->validator);
  74. $aValidator->addPropertyValidator('integer', $integerValidator);
  75. $result = $aValidator->validate($A)->getFlattenedErrors();
  76. $this->assertEquals('A valid integer number is expected.', $result['b.0'][0]->getMessage());
  77. }
  78. /**
  79. * @test
  80. */
  81. public function collectionValidatorIsValidEarlyReturnsOnUnitializedDoctrinePersistenceCollections() {
  82. $entityManager = $this->getMock('Doctrine\ORM\EntityManager', array(), array(), '', FALSE);
  83. $collection = new \Doctrine\Common\Collections\ArrayCollection(array());
  84. $persistentCollection = new \Doctrine\ORM\PersistentCollection($entityManager, '', $collection);
  85. \TYPO3\FLOW3\Reflection\ObjectAccess::setProperty($persistentCollection, 'initialized', FALSE, TRUE);
  86. $this->mockValidatorResolver->expects($this->never())->method('createValidator');
  87. $this->validator->validate($persistentCollection);
  88. }
  89. }
  90. ?>