Permalink
Browse files

moved component and bridge unit tests to the src/ directory

This is the first step to make each Symfony Component and Bridge self-contained.
  • Loading branch information...
1 parent 2dc01d1 commit ce994a5616b20f6ebf37d66a1a89bdd3af6b4ea2 @fabpot fabpot committed Mar 28, 2012
Showing with 8,012 additions and 4 deletions.
  1. +13 −4 README.md
  2. +131 −0 Tests/ConstraintTest.php
  3. +60 −0 Tests/ConstraintValidatorTest.php
  4. +134 −0 Tests/ConstraintViolationListTest.php
  5. +35 −0 Tests/ConstraintViolationTest.php
  6. +120 −0 Tests/Constraints/AllValidatorTest.php
  7. +78 −0 Tests/Constraints/BlankValidatorTest.php
  8. +157 −0 Tests/Constraints/CallbackValidatorTest.php
  9. +297 −0 Tests/Constraints/ChoiceValidatorTest.php
  10. +20 −0 Tests/Constraints/CollectionValidatorArrayObjectTest.php
  11. +20 −0 Tests/Constraints/CollectionValidatorArrayTest.php
  12. +78 −0 Tests/Constraints/CollectionValidatorCustomArrayObjectTest.php
  13. +420 −0 Tests/Constraints/CollectionValidatorTest.php
  14. +114 −0 Tests/Constraints/CountryValidatorTest.php
  15. +120 −0 Tests/Constraints/DateTimeValidatorTest.php
  16. +116 −0 Tests/Constraints/DateValidatorTest.php
  17. +106 −0 Tests/Constraints/EmailValidatorTest.php
  18. +63 −0 Tests/Constraints/FalseValidatorTest.php
  19. +22 −0 Tests/Constraints/FileValidatorObjectTest.php
  20. +37 −0 Tests/Constraints/FileValidatorPathTest.php
  21. +324 −0 Tests/Constraints/FileValidatorTest.php
  22. BIN Tests/Constraints/Fixtures/test.gif
  23. +226 −0 Tests/Constraints/ImageValidatorTest.php
  24. +478 −0 Tests/Constraints/IpValidatorTest.php
  25. +114 −0 Tests/Constraints/LanguageValidatorTest.php
  26. +115 −0 Tests/Constraints/LocaleValidatorTest.php
  27. +22 −0 Tests/Constraints/LocalizedTestCase.php
  28. +127 −0 Tests/Constraints/MaxLengthValidatorTest.php
  29. +103 −0 Tests/Constraints/MaxValidatorTest.php
  30. +127 −0 Tests/Constraints/MinLengthValidatorTest.php
  31. +97 −0 Tests/Constraints/MinValidatorTest.php
  32. +109 −0 Tests/Constraints/NotBlankValidatorTest.php
  33. +69 −0 Tests/Constraints/NotNullValidatorTest.php
  34. +70 −0 Tests/Constraints/NullValidatorTest.php
  35. +116 −0 Tests/Constraints/RegexValidatorTest.php
  36. +172 −0 Tests/Constraints/SizeLengthValidatorTest.php
  37. +121 −0 Tests/Constraints/SizeValidatorTest.php
  38. +117 −0 Tests/Constraints/TimeValidatorTest.php
  39. +64 −0 Tests/Constraints/TrueValidatorTest.php
  40. +180 −0 Tests/Constraints/TypeValidatorTest.php
  41. +168 −0 Tests/Constraints/UrlValidatorTest.php
  42. +223 −0 Tests/ExecutionContextTest.php
  43. +13 −0 Tests/Fixtures/ClassConstraint.php
  44. +22 −0 Tests/Fixtures/ConstraintA.php
  45. +30 −0 Tests/Fixtures/ConstraintAValidator.php
  46. +15 −0 Tests/Fixtures/ConstraintB.php
  47. +21 −0 Tests/Fixtures/ConstraintC.php
  48. +47 −0 Tests/Fixtures/Entity.php
  49. +7 −0 Tests/Fixtures/EntityInterface.php
  50. +16 −0 Tests/Fixtures/EntityParent.php
  51. +15 −0 Tests/Fixtures/FailingConstraint.php
  52. +16 −0 Tests/Fixtures/FailingConstraintValidator.php
  53. +25 −0 Tests/Fixtures/FakeClassMetadataFactory.php
  54. +30 −0 Tests/Fixtures/FilesLoader.php
  55. +27 −0 Tests/Fixtures/GroupSequenceProviderEntity.php
  56. +7 −0 Tests/Fixtures/InvalidConstraint.php
  57. +5 −0 Tests/Fixtures/InvalidConstraintValidator.php
  58. +13 −0 Tests/Fixtures/PropertyConstraint.php
  59. +7 −0 Tests/Fixtures/Reference.php
  60. +63 −0 Tests/GlobalExecutionContextTest.php
  61. +468 −0 Tests/GraphWalkerTest.php
  62. +78 −0 Tests/Mapping/Cache/ApcCacheTest.php
  63. +120 −0 Tests/Mapping/ClassMetadataFactoryTest.php
  64. +225 −0 Tests/Mapping/ClassMetadataTest.php
  65. +73 −0 Tests/Mapping/ElementMetadataTest.php
  66. +39 −0 Tests/Mapping/GetterMetadataTest.php
  67. +157 −0 Tests/Mapping/Loader/AnnotationLoaderTest.php
  68. +48 −0 Tests/Mapping/Loader/FilesLoaderTest.php
  69. +85 −0 Tests/Mapping/Loader/LoaderChainTest.php
  70. +80 −0 Tests/Mapping/Loader/StaticMethodLoaderTest.php
  71. +82 −0 Tests/Mapping/Loader/XmlFileLoaderTest.php
  72. +107 −0 Tests/Mapping/Loader/YamlFileLoaderTest.php
  73. +87 −0 Tests/Mapping/Loader/constraint-mapping.xml
  74. +44 −0 Tests/Mapping/Loader/constraint-mapping.yml
  75. 0 Tests/Mapping/Loader/empty-mapping.yml
  76. +1 −0 Tests/Mapping/Loader/nonvalid-mapping.yml
  77. +83 −0 Tests/Mapping/MemberMetadataTest.php
  78. +36 −0 Tests/Mapping/PropertyMetadataTest.php
  79. +61 −0 Tests/ValidatorContextTest.php
  80. +193 −0 Tests/ValidatorFactoryTest.php
  81. +210 −0 Tests/ValidatorTest.php
  82. +44 −0 Tests/bootstrap.php
  83. +29 −0 phpunit.xml.dist
View
@@ -36,14 +36,23 @@ Silex integration:
https://github.com/fabpot/Silex/blob/master/src/Silex/Provider/ValidatorServiceProvider.php
-Unit tests:
-
-https://github.com/symfony/symfony/tree/master/tests/Symfony/Tests/Component/Validator
-
Documentation:
http://symfony.com/doc/2.0/book/validation.html
JSR-303 Specification:
http://jcp.org/en/jsr/detail?id=303
+
+You can run the unit tests with the following command:
+
+ phpunit -c src/Symfony/Component/Validator/
+
+If you also want to run the unit tests that depend on other Symfony
+Components, declare the following environment variables before running
+PHPUnit:
+
+ export SYMFONY_YAML=../path/to/Yaml
+ export SYMFONY_LOCALE=../path/to/Locale
+ export SYMFONY_HTTP_FOUNDATION=../path/to/HttpFoundation
+ export DOCTRINE_COMMON=../path/to/doctrine-common
View
@@ -0,0 +1,131 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Validator\Tests;
+
+use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
+use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
+use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
+use Symfony\Component\Validator\Tests\Fixtures\ConstraintC;
+
+class ConstraintTest extends \PHPUnit_Framework_TestCase
+{
+ public function testSetProperties()
+ {
+ $constraint = new ConstraintA(array(
+ 'property1' => 'foo',
+ 'property2' => 'bar',
+ ));
+
+ $this->assertEquals('foo', $constraint->property1);
+ $this->assertEquals('bar', $constraint->property2);
+ }
+
+ public function testSetNotExistingPropertyThrowsException()
+ {
+ $this->setExpectedException('Symfony\Component\Validator\Exception\InvalidOptionsException');
+
+ new ConstraintA(array(
+ 'foo' => 'bar',
+ ));
+ }
+
+ public function testMagicPropertiesAreNotAllowed()
+ {
+ $constraint = new ConstraintA();
+
+ $this->setExpectedException('Symfony\Component\Validator\Exception\InvalidOptionsException');
+
+ $constraint->foo = 'bar';
+ }
+
+ public function testInvalidAndRequiredOptionsPassed()
+ {
+ $this->setExpectedException('Symfony\Component\Validator\Exception\InvalidOptionsException');
+
+ new ConstraintC(array(
+ 'option1' => 'default',
+ 'foo' => 'bar'
+ ));
+ }
+
+ public function testSetDefaultProperty()
+ {
+ $constraint = new ConstraintA('foo');
+
+ $this->assertEquals('foo', $constraint->property2);
+ }
+
+ public function testSetDefaultPropertyDoctrineStyle()
+ {
+ $constraint = new ConstraintA(array('value' => 'foo'));
+
+ $this->assertEquals('foo', $constraint->property2);
+ }
+
+ public function testSetUndefinedDefaultProperty()
+ {
+ $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
+
+ new ConstraintB('foo');
+ }
+
+ public function testRequiredOptionsMustBeDefined()
+ {
+ $this->setExpectedException('Symfony\Component\Validator\Exception\MissingOptionsException');
+
+ new ConstraintC();
+ }
+
+ public function testRequiredOptionsPassed()
+ {
+ new ConstraintC(array('option1' => 'default'));
+ }
+
+ public function testGroupsAreConvertedToArray()
+ {
+ $constraint = new ConstraintA(array('groups' => 'Foo'));
+
+ $this->assertEquals(array('Foo'), $constraint->groups);
+ }
+
+ public function testAddDefaultGroupAddsGroup()
+ {
+ $constraint = new ConstraintA(array('groups' => 'Default'));
+ $constraint->addImplicitGroupName('Foo');
+ $this->assertEquals(array('Default', 'Foo'), $constraint->groups);
+ }
+
+ public function testAllowsSettingZeroRequiredPropertyValue()
+ {
+ $constraint = new ConstraintA(0);
+ $this->assertEquals(0, $constraint->property2);
+ }
+
+ public function testCanCreateConstraintWithNoDefaultOptionAndEmptyArray()
+ {
+ new ConstraintB(array());
+ }
+
+ public function testGetTargetsCanBeString()
+ {
+ $constraint = new ClassConstraint;
+
+ $this->assertEquals('class', $constraint->getTargets());
+ }
+
+ public function testGetTargetsCanBeArray()
+ {
+ $constraint = new ConstraintA;
+
+ $this->assertEquals(array('property', 'class'), $constraint->getTargets());
+ }
+}
@@ -0,0 +1,60 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Validator\Tests;
+
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+
+class ConstraintValidatorTest_Validator extends ConstraintValidator
+{
+ private $message;
+ private $params;
+
+ public function __construct($message, array $params = array())
+ {
+ $this->message = $message;
+ $this->params = $params;
+ }
+
+ public function isValid($value, Constraint $constraint)
+ {
+ $this->setMessage($this->message, $this->params);
+ }
+}
+
+class ConstraintValidatorTest extends \PHPUnit_Framework_TestCase
+{
+ public function testSetMessage()
+ {
+ $context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
+ $constraint = $this->getMock('Symfony\Component\Validator\Constraint', array(), array(), '', false);
+ $validator = new ConstraintValidatorTest_Validator('error message', array('foo' => 'bar'));
+ $validator->initialize($context);
+
+ $context->expects($this->once())
+ ->method('addViolation')
+ ->with('error message', array('foo' => 'bar'));
+
+ $validator->isValid('bam', $constraint);
+ }
+
+ /**
+ * @expectedException Symfony\Component\Validator\Exception\ValidatorException
+ */
+ public function testSetMessageFailsIfNoContextSet()
+ {
+ $constraint = $this->getMock('Symfony\Component\Validator\Constraint', array(), array(), '', false);
+ $validator = new ConstraintValidatorTest_Validator('error message', array('foo' => 'bar'));
+
+ $validator->isValid('bam', $constraint);
+ }
+}
@@ -0,0 +1,134 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Validator\Tests;
+
+use Symfony\Component\Validator\ConstraintViolation;
+use Symfony\Component\Validator\ConstraintViolationList;
+
+class ConstraintViolationListTest extends \PHPUnit_Framework_TestCase
+{
+ protected $list;
+
+ protected function setUp()
+ {
+ $this->list = new ConstraintViolationList();
+ }
+
+ protected function tearDown()
+ {
+ $this->list = null;
+ }
+
+ public function testInit()
+ {
+ $this->assertCount(0, $this->list);
+ }
+
+ public function testInitWithViolations()
+ {
+ $violation = $this->getViolation('Error');
+ $this->list = new ConstraintViolationList(array($violation));
+
+ $this->assertCount(1, $this->list);
+ $this->assertSame($violation, $this->list[0]);
+ }
+
+ public function testAdd()
+ {
+ $violation = $this->getViolation('Error');
+ $this->list->add($violation);
+
+ $this->assertCount(1, $this->list);
+ $this->assertSame($violation, $this->list[0]);
+ }
+
+ public function testAddAll()
+ {
+ $violations = array(
+ 10 => $this->getViolation('Error 1'),
+ 20 => $this->getViolation('Error 2'),
+ 30 => $this->getViolation('Error 3'),
+ );
+ $otherList = new ConstraintViolationList($violations);
+ $this->list->addAll($otherList);
+
+ $this->assertCount(3, $this->list);
+
+ $this->assertSame($violations[10], $this->list[0]);
+ $this->assertSame($violations[20], $this->list[1]);
+ $this->assertSame($violations[30], $this->list[2]);
+ }
+
+ public function testIterator()
+ {
+ $violations = array(
+ 10 => $this->getViolation('Error 1'),
+ 20 => $this->getViolation('Error 2'),
+ 30 => $this->getViolation('Error 3'),
+ );
+
+ $this->list = new ConstraintViolationList($violations);
+
+ // indices are reset upon adding -> array_values()
+ $this->assertSame(array_values($violations), iterator_to_array($this->list));
+ }
+
+ public function testArrayAccess()
+ {
+ $violation = $this->getViolation('Error');
+ $this->list[] = $violation;
+
+ $this->assertSame($violation, $this->list[0]);
+ $this->assertTrue(isset($this->list[0]));
+
+ unset($this->list[0]);
+
+ $this->assertFalse(isset($this->list[0]));
+
+ $this->list[10] = $violation;
+
+ $this->assertSame($violation, $this->list[10]);
+ $this->assertTrue(isset($this->list[10]));
+ }
+
+ public function testToString()
+ {
+ $this->list = new ConstraintViolationList(array(
+ $this->getViolation('Error 1', 'Root'),
+ $this->getViolation('Error 2', 'Root', 'foo.bar'),
+ $this->getViolation('Error 3', 'Root', '[baz]'),
+ $this->getViolation('Error 4', '', 'foo.bar'),
+ $this->getViolation('Error 5', '', '[baz]'),
+ ));
+
+ $expected = <<<EOF
+Root:
+ Error 1
+Root.foo.bar:
+ Error 2
+Root[baz]:
+ Error 3
+foo.bar:
+ Error 4
+[baz]:
+ Error 5
+
+EOF;
+
+ $this->assertEquals($expected, (string) $this->list);
+ }
+
+ protected function getViolation($message, $root = null, $propertyPath = null)
+ {
+ return new ConstraintViolation($message, array(), $root, $propertyPath, null);
+ }
+}
@@ -0,0 +1,35 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Validator\Tests;
+
+use Symfony\Component\Validator\ConstraintViolation;
+
+class ConstraintViolationTest extends \PHPUnit_Framework_TestCase
+{
+ public function testToStringHandlesArrays()
+ {
+ $violation = new ConstraintViolation(
+ '{{ value }}',
+ array('{{ value }}' => array(1, 2, 3)),
+ 'Root',
+ 'property.path',
+ null
+ );
+
+ $expected = <<<EOF
+Root.property.path:
+ Array
+EOF;
+
+ $this->assertSame($expected, (string) $violation);
+ }
+}
Oops, something went wrong.

0 comments on commit ce994a5

Please sign in to comment.