IdenticalToValidatorTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Validator\Constraints\IdenticalTo;
  12. use Symfony\Component\Validator\Constraints\IdenticalToValidator;
  13. use Symfony\Component\Validator\Validation;
  14. /**
  15. * @author Daniel Holmes <daniel@danielholmes.org>
  16. */
  17. class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
  18. {
  19. protected function getApiVersion()
  20. {
  21. return Validation::API_VERSION_2_5;
  22. }
  23. protected function createValidator()
  24. {
  25. return new IdenticalToValidator();
  26. }
  27. protected function createConstraint(array $options = null)
  28. {
  29. return new IdenticalTo($options);
  30. }
  31. protected function getErrorCode()
  32. {
  33. return IdenticalTo::NOT_IDENTICAL_ERROR;
  34. }
  35. public function provideAllValidComparisons()
  36. {
  37. $this->setDefaultTimezone('UTC');
  38. // Don't call addPhp5Dot5Comparisons() automatically, as it does
  39. // not take care of identical objects
  40. $comparisons = $this->provideValidComparisons();
  41. $this->restoreDefaultTimezone();
  42. return $comparisons;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function provideValidComparisons()
  48. {
  49. $date = new \DateTime('2000-01-01');
  50. $object = new ComparisonTest_Class(2);
  51. $comparisons = array(
  52. array(3, 3),
  53. array('a', 'a'),
  54. array($date, $date),
  55. array($object, $object),
  56. array(null, 1),
  57. );
  58. if (\PHP_VERSION_ID >= 50500) {
  59. $immutableDate = new \DateTimeImmutable('2000-01-01');
  60. $comparisons[] = array($immutableDate, $immutableDate);
  61. }
  62. return $comparisons;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function provideInvalidComparisons()
  68. {
  69. return array(
  70. array(1, '1', 2, '2', 'integer'),
  71. array(2, '2', '2', '"2"', 'string'),
  72. array('22', '"22"', '333', '"333"', 'string'),
  73. array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', 'DateTime'),
  74. array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('1999-01-01'), 'Jan 1, 1999, 12:00 AM', 'DateTime'),
  75. array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
  76. );
  77. }
  78. }