AbstractComparisonValidatorTestCase.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\Intl\Util\IntlTestHelper;
  12. use Symfony\Component\Validator\Constraint;
  13. class ComparisonTest_Class
  14. {
  15. protected $value;
  16. public function __construct($value)
  17. {
  18. $this->value = $value;
  19. }
  20. public function __toString()
  21. {
  22. return (string) $this->value;
  23. }
  24. }
  25. /**
  26. * @author Daniel Holmes <daniel@danielholmes.org>
  27. */
  28. abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintValidatorTest
  29. {
  30. protected static function addPhp5Dot5Comparisons(array $comparisons)
  31. {
  32. if (\PHP_VERSION_ID < 50500) {
  33. return $comparisons;
  34. }
  35. $result = $comparisons;
  36. // Duplicate all tests involving DateTime objects to be tested with
  37. // DateTimeImmutable objects as well
  38. foreach ($comparisons as $comparison) {
  39. $add = false;
  40. foreach ($comparison as $i => $value) {
  41. if ($value instanceof \DateTime) {
  42. $comparison[$i] = new \DateTimeImmutable(
  43. $value->format('Y-m-d H:i:s.u e'),
  44. $value->getTimezone()
  45. );
  46. $add = true;
  47. } elseif ('DateTime' === $value) {
  48. $comparison[$i] = 'DateTimeImmutable';
  49. $add = true;
  50. }
  51. }
  52. if ($add) {
  53. $result[] = $comparison;
  54. }
  55. }
  56. return $result;
  57. }
  58. public function provideInvalidConstraintOptions()
  59. {
  60. return array(
  61. array(null),
  62. array(array()),
  63. );
  64. }
  65. /**
  66. * @dataProvider provideInvalidConstraintOptions
  67. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  68. */
  69. public function testThrowsConstraintExceptionIfNoValueOrProperty($options)
  70. {
  71. $this->createConstraint($options);
  72. }
  73. /**
  74. * @dataProvider provideAllValidComparisons
  75. *
  76. * @param mixed $dirtyValue
  77. * @param mixed $comparisonValue
  78. */
  79. public function testValidComparisonToValue($dirtyValue, $comparisonValue)
  80. {
  81. $constraint = $this->createConstraint(array('value' => $comparisonValue));
  82. $this->validator->validate($dirtyValue, $constraint);
  83. $this->assertNoViolation();
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function provideAllValidComparisons()
  89. {
  90. // The provider runs before setUp(), so we need to manually fix
  91. // the default timezone
  92. $this->setDefaultTimezone('UTC');
  93. $comparisons = self::addPhp5Dot5Comparisons($this->provideValidComparisons());
  94. $this->restoreDefaultTimezone();
  95. return $comparisons;
  96. }
  97. /**
  98. * @return array
  99. */
  100. abstract public function provideValidComparisons();
  101. /**
  102. * @dataProvider provideAllInvalidComparisons
  103. *
  104. * @param mixed $dirtyValue
  105. * @param mixed $dirtyValueAsString
  106. * @param mixed $comparedValue
  107. * @param mixed $comparedValueString
  108. * @param string $comparedValueType
  109. */
  110. public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
  111. {
  112. // Conversion of dates to string differs between ICU versions
  113. // Make sure we have the correct version loaded
  114. if ($dirtyValue instanceof \DateTime || $dirtyValue instanceof \DateTimeInterface) {
  115. IntlTestHelper::requireIntl($this, '57.1');
  116. if (\PHP_VERSION_ID < 50304 && !(\extension_loaded('intl') && method_exists('IntlDateFormatter', 'setTimeZone'))) {
  117. $this->markTestSkipped('Intl supports formatting DateTime objects since 5.3.4');
  118. }
  119. }
  120. $constraint = $this->createConstraint(array('value' => $comparedValue));
  121. $constraint->message = 'Constraint Message';
  122. $this->validator->validate($dirtyValue, $constraint);
  123. $this->buildViolation('Constraint Message')
  124. ->setParameter('{{ value }}', $dirtyValueAsString)
  125. ->setParameter('{{ compared_value }}', $comparedValueString)
  126. ->setParameter('{{ compared_value_type }}', $comparedValueType)
  127. ->setCode($this->getErrorCode())
  128. ->assertRaised();
  129. }
  130. /**
  131. * @return array
  132. */
  133. public function provideAllInvalidComparisons()
  134. {
  135. // The provider runs before setUp(), so we need to manually fix
  136. // the default timezone
  137. $this->setDefaultTimezone('UTC');
  138. $comparisons = self::addPhp5Dot5Comparisons($this->provideInvalidComparisons());
  139. $this->restoreDefaultTimezone();
  140. return $comparisons;
  141. }
  142. /**
  143. * @return array
  144. */
  145. abstract public function provideInvalidComparisons();
  146. /**
  147. * @param array|null $options Options for the constraint
  148. *
  149. * @return Constraint
  150. */
  151. abstract protected function createConstraint(array $options = null);
  152. /**
  153. * @return string|null
  154. */
  155. protected function getErrorCode()
  156. {
  157. }
  158. }