PageRenderTime 68ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Propel/Tests/Generator/Behavior/Validate/I18nConcreteInheritanceHandleValidateBehaviorTest.php

http://github.com/propelorm/Propel2
PHP | 200 lines | 125 code | 39 blank | 36 comment | 3 complexity | 7914551c3c337ea21cb44770eeaa7610 MD5 | raw file
  1. <?php
  2. /**
  3. * MIT License. This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. */
  7. namespace Propel\Tests\Generator\Behavior\Validate;
  8. use Propel\Tests\Helpers\Bookstore\BookstoreTestBase;
  9. use ReflectionMethod;
  10. use Symfony\Component\Validator\Constraints\NotNull;
  11. use Symfony\Component\Validator\Constraints\Regex;
  12. use Symfony\Component\Validator\Constraints\Type;
  13. use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
  14. use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
  15. /**
  16. * Tests for interaction between I18n behavior, ConcreteInheritance behavior
  17. * and Validate behavior.
  18. *
  19. * @author Cristiano Cinotti
  20. *
  21. * @group database
  22. */
  23. class I18nConcreteInheritanceHandleValidateBehaviorTest extends BookstoreTestBase
  24. {
  25. protected $metadataFactory;
  26. /**
  27. * @return void
  28. */
  29. public function assertPreConditions(): void
  30. {
  31. $this->metadataFactory = new LazyLoadingMetadataFactory(new StaticMethodLoader());
  32. }
  33. /**
  34. * @return void
  35. */
  36. public function testI18nBehaviorHandlesValidateBehavior()
  37. {
  38. $class = 'Propel\Tests\Bookstore\Behavior\ValidateTriggerBook';
  39. $this->checkClassHasValidateBehavior($class);
  40. $classMetadata = $this->metadataFactory->getMetadataFor($class);
  41. $this->assertCount(1, $classMetadata->getConstrainedProperties());
  42. $this->assertTrue(in_array('isbn', $classMetadata->getConstrainedProperties(), true));
  43. $metadatas = $classMetadata->getPropertyMetadata('isbn');
  44. $this->assertCount(1, $metadatas);
  45. $constraints = $metadatas[0]->getConstraints();
  46. $this->assertCount(1, $constraints);
  47. $this->assertInstanceOf('Symfony\Component\Validator\Constraints\Regex', $constraints[0]);
  48. $i18nClass = 'Propel\Tests\Bookstore\Behavior\ValidateTriggerBookI18n';
  49. $this->checkClassHasValidateBehavior($i18nClass);
  50. $i18nClassMetadata = $this->metadataFactory->getMetadataFor($i18nClass);
  51. $this->assertCount(1, $i18nClassMetadata->getConstrainedProperties());
  52. $this->assertTrue(in_array('title', $i18nClassMetadata->getConstrainedProperties(), true));
  53. $i18nMetadatas = $i18nClassMetadata->getPropertyMetadata('title');
  54. $this->assertCount(1, $i18nMetadatas);
  55. $i18nConstraints = $i18nMetadatas[0]->getConstraints();
  56. $this->assertCount(1, $i18nConstraints);
  57. $this->assertInstanceOf('Symfony\Component\Validator\Constraints\NotNull', $i18nConstraints[0]);
  58. }
  59. /**
  60. * @return void
  61. */
  62. public function testConcreteInheritanceBehaviorHandlesValidateBehavior()
  63. {
  64. $fiction = 'Propel\Tests\Bookstore\Behavior\ValidateTriggerFiction';
  65. $this->checkClassHasValidateBehavior($fiction);
  66. $fictionMetadata = $this->metadataFactory->getMetadataFor($fiction);
  67. $this->assertCount(1, $fictionMetadata->getConstrainedProperties());
  68. $this->assertTrue(in_array('isbn', $fictionMetadata->getConstrainedProperties(), true));
  69. $fictionMetadatas = $fictionMetadata->getPropertyMetadata('isbn');
  70. // 1st is for ValidateTriggerFiction (base)
  71. // 2nd is for ValidateTriggerBook (base)
  72. // I'm not sure if this is needed. We should not care about validator internals
  73. $this->assertCount(2, $fictionMetadatas);
  74. $expectedValidatorGroups = [
  75. 'ValidateTriggerFiction',
  76. 'ValidateTriggerBook',
  77. ];
  78. // iterate over metadatas and constarints.
  79. // If constraint match with expected constraint -> remove it form expectations list
  80. // We are looking for our regex validations
  81. foreach ($fictionMetadatas as $fictionmetadata) {
  82. /** @var \Symfony\Component\Validator\Mapping\PropertyMetadata $constraint */
  83. foreach ($fictionmetadata->getConstraints() as $constraint) {
  84. if ($constraint instanceof Regex) {
  85. $expectedValidatorGroups = array_diff($expectedValidatorGroups, $constraint->groups);
  86. }
  87. }
  88. }
  89. $this->assertEmpty($expectedValidatorGroups);
  90. $comic = 'Propel\Tests\Bookstore\Behavior\ValidateTriggerComic';
  91. $this->checkClassHasValidateBehavior($comic);
  92. $comicMetadata = $this->metadataFactory->getMetadataFor($comic);
  93. $this->assertCount(2, $comicMetadata->getConstrainedProperties());
  94. $this->assertTrue(in_array('isbn', $comicMetadata->getConstrainedProperties(), true));
  95. $this->assertTrue(in_array('bar', $comicMetadata->getConstrainedProperties(), true));
  96. $comicMetadatas['isbn'] = $comicMetadata->getPropertyMetadata('isbn');
  97. $comicMetadatas['bar'] = $comicMetadata->getPropertyMetadata('bar');
  98. $expectedComicValidators = [
  99. 'ValidateTriggerComic',
  100. 'ValidateTriggerComic',
  101. 'ValidateTriggerBook',
  102. ];
  103. foreach ($comicMetadatas['isbn'] as $metadata) {
  104. /** @var \Symfony\Component\Validator\Mapping\PropertyMetadata $metadata */
  105. foreach ($metadata->getConstraints() as $constraint) {
  106. if ($constraint instanceof Regex) {
  107. $expectedComicValidators = array_diff($expectedComicValidators, $constraint->groups);
  108. }
  109. }
  110. }
  111. $this->assertEmpty($expectedComicValidators);
  112. $comicMetadataBar = $comicMetadatas['bar'];
  113. $expectedComicBarValidatorTypes = [
  114. 0 => 'Symfony\Component\Validator\Constraints\NotNull',
  115. 1 => 'Symfony\Component\Validator\Constraints\Type',
  116. ];
  117. foreach ($comicMetadataBar as $metadata) {
  118. $constraints = $metadata->getConstraints();
  119. foreach ($constraints as $constraint) {
  120. if ($constraint instanceof NotNull) {
  121. unset($expectedComicBarValidatorTypes[0]);
  122. } elseif ($constraint instanceof Type) {
  123. unset($expectedComicBarValidatorTypes[1]);
  124. }
  125. }
  126. }
  127. $this->assertEmpty($expectedComicBarValidatorTypes);
  128. }
  129. /**
  130. * @return void
  131. */
  132. public function testConcreteInheritanceAndI18nBehaviorHandlesValidateBehavior()
  133. {
  134. $classes = ['ValidateTriggerFictionI18n', 'ValidateTriggerComicI18n'];
  135. foreach ($classes as $class) {
  136. $this->checkClassHasValidateBehavior('Propel\Tests\Bookstore\Behavior\\' . $class);
  137. $classMetadata = $this->metadataFactory->getMetadataFor('Propel\Tests\Bookstore\Behavior\\' . $class);
  138. $this->assertCount(1, $classMetadata->getConstrainedProperties());
  139. $this->assertTrue(in_array('title', $classMetadata->getConstrainedProperties(), true));
  140. $metadatas = $classMetadata->getPropertyMetadata('title');
  141. $this->assertCount(1, $metadatas);
  142. $constraints = $metadatas[0]->getConstraints();
  143. $this->assertCount(1, $constraints);
  144. $this->assertInstanceOf('Symfony\Component\Validator\Constraints\NotNull', $constraints[0]);
  145. }
  146. }
  147. /**
  148. * @return void
  149. */
  150. protected function checkClassHasValidateBehavior($class)
  151. {
  152. $this->assertTrue(method_exists($class, 'validate'), "Class $class has no validate() method");
  153. $this->assertTrue(method_exists($class, 'getValidationFailures'), "Class $class has no getValidationFailures() method");
  154. $this->assertTrue(method_exists($class, 'loadValidatorMetadata'), "Class $class has no loadValidatorMetadata() method");
  155. $this->assertClassHasAttribute('alreadyInValidation', $class, "Class $class has no 'alreadyInValidation' property");
  156. $this->assertClassHasAttribute('validationFailures', $class, "Class $class has no 'validationFailures' property");
  157. $method = new ReflectionMethod($class, 'loadValidatorMetadata');
  158. $this->assertTrue($method->isStatic(), "Method loadValidatorMetadata() of class $class isn't static");
  159. }
  160. }