You are here

class ClassMetadataTest in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/validator/Tests/Mapping/ClassMetadataTest.php \Symfony\Component\Validator\Tests\Mapping\ClassMetadataTest
  2. 8 vendor/symfony/serializer/Tests/Mapping/ClassMetadataTest.php \Symfony\Component\Serializer\Tests\Mapping\ClassMetadataTest
Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Tests/Mapping/ClassMetadataTest.php \Symfony\Component\Validator\Tests\Mapping\ClassMetadataTest

Hierarchy

  • class \Symfony\Component\Validator\Tests\Mapping\ClassMetadataTest extends \Symfony\Component\Validator\Tests\Mapping\PHPUnit_Framework_TestCase

Expanded class hierarchy of ClassMetadataTest

File

vendor/symfony/validator/Tests/Mapping/ClassMetadataTest.php, line 21

Namespace

Symfony\Component\Validator\Tests\Mapping
View source
class ClassMetadataTest extends \PHPUnit_Framework_TestCase {
  const CLASSNAME = 'Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity';
  const PARENTCLASS = 'Symfony\\Component\\Validator\\Tests\\Fixtures\\EntityParent';
  const PROVIDERCLASS = 'Symfony\\Component\\Validator\\Tests\\Fixtures\\GroupSequenceProviderEntity';
  protected $metadata;
  protected function setUp() {
    $this->metadata = new ClassMetadata(self::CLASSNAME);
  }
  protected function tearDown() {
    $this->metadata = null;
  }
  public function testAddConstraintDoesNotAcceptValid() {
    $this
      ->setExpectedException('Symfony\\Component\\Validator\\Exception\\ConstraintDefinitionException');
    $this->metadata
      ->addConstraint(new Valid());
  }
  public function testAddConstraintRequiresClassConstraints() {
    $this
      ->setExpectedException('Symfony\\Component\\Validator\\Exception\\ConstraintDefinitionException');
    $this->metadata
      ->addConstraint(new PropertyConstraint());
  }
  public function testAddPropertyConstraints() {
    $this->metadata
      ->addPropertyConstraint('firstName', new ConstraintA());
    $this->metadata
      ->addPropertyConstraint('lastName', new ConstraintB());
    $this
      ->assertEquals(array(
      'firstName',
      'lastName',
    ), $this->metadata
      ->getConstrainedProperties());
  }
  public function testAddMultiplePropertyConstraints() {
    $this->metadata
      ->addPropertyConstraints('lastName', array(
      new ConstraintA(),
      new ConstraintB(),
    ));
    $constraints = array(
      new ConstraintA(array(
        'groups' => array(
          'Default',
          'Entity',
        ),
      )),
      new ConstraintB(array(
        'groups' => array(
          'Default',
          'Entity',
        ),
      )),
    );
    $properties = $this->metadata
      ->getPropertyMetadata('lastName');
    $this
      ->assertCount(1, $properties);
    $this
      ->assertEquals('lastName', $properties[0]
      ->getName());
    $this
      ->assertEquals($constraints, $properties[0]
      ->getConstraints());
  }
  public function testAddGetterConstraints() {
    $this->metadata
      ->addGetterConstraint('lastName', new ConstraintA());
    $this->metadata
      ->addGetterConstraint('lastName', new ConstraintB());
    $constraints = array(
      new ConstraintA(array(
        'groups' => array(
          'Default',
          'Entity',
        ),
      )),
      new ConstraintB(array(
        'groups' => array(
          'Default',
          'Entity',
        ),
      )),
    );
    $properties = $this->metadata
      ->getPropertyMetadata('lastName');
    $this
      ->assertCount(1, $properties);
    $this
      ->assertEquals('getLastName', $properties[0]
      ->getName());
    $this
      ->assertEquals($constraints, $properties[0]
      ->getConstraints());
  }
  public function testAddMultipleGetterConstraints() {
    $this->metadata
      ->addGetterConstraints('lastName', array(
      new ConstraintA(),
      new ConstraintB(),
    ));
    $constraints = array(
      new ConstraintA(array(
        'groups' => array(
          'Default',
          'Entity',
        ),
      )),
      new ConstraintB(array(
        'groups' => array(
          'Default',
          'Entity',
        ),
      )),
    );
    $properties = $this->metadata
      ->getPropertyMetadata('lastName');
    $this
      ->assertCount(1, $properties);
    $this
      ->assertEquals('getLastName', $properties[0]
      ->getName());
    $this
      ->assertEquals($constraints, $properties[0]
      ->getConstraints());
  }
  public function testMergeConstraintsMergesClassConstraints() {
    $parent = new ClassMetadata(self::PARENTCLASS);
    $parent
      ->addConstraint(new ConstraintA());
    $this->metadata
      ->mergeConstraints($parent);
    $this->metadata
      ->addConstraint(new ConstraintA());
    $constraints = array(
      new ConstraintA(array(
        'groups' => array(
          'Default',
          'EntityParent',
          'Entity',
        ),
      )),
      new ConstraintA(array(
        'groups' => array(
          'Default',
          'Entity',
        ),
      )),
    );
    $this
      ->assertEquals($constraints, $this->metadata
      ->getConstraints());
  }
  public function testMergeConstraintsMergesMemberConstraints() {
    $parent = new ClassMetadata(self::PARENTCLASS);
    $parent
      ->addPropertyConstraint('firstName', new ConstraintA());
    $this->metadata
      ->mergeConstraints($parent);
    $this->metadata
      ->addPropertyConstraint('firstName', new ConstraintA());
    $constraints = array(
      new ConstraintA(array(
        'groups' => array(
          'Default',
          'EntityParent',
          'Entity',
        ),
      )),
      new ConstraintA(array(
        'groups' => array(
          'Default',
          'Entity',
        ),
      )),
    );
    $members = $this->metadata
      ->getPropertyMetadata('firstName');
    $this
      ->assertCount(1, $members);
    $this
      ->assertEquals(self::PARENTCLASS, $members[0]
      ->getClassName());
    $this
      ->assertEquals($constraints, $members[0]
      ->getConstraints());
  }
  public function testMemberMetadatas() {
    $this->metadata
      ->addPropertyConstraint('firstName', new ConstraintA());
    $this
      ->assertTrue($this->metadata
      ->hasPropertyMetadata('firstName'));
    $this
      ->assertFalse($this->metadata
      ->hasPropertyMetadata('non_existent_field'));
  }
  public function testMergeConstraintsKeepsPrivateMembersSeparate() {
    $parent = new ClassMetadata(self::PARENTCLASS);
    $parent
      ->addPropertyConstraint('internal', new ConstraintA());
    $this->metadata
      ->mergeConstraints($parent);
    $this->metadata
      ->addPropertyConstraint('internal', new ConstraintA());
    $parentConstraints = array(
      new ConstraintA(array(
        'groups' => array(
          'Default',
          'EntityParent',
          'Entity',
        ),
      )),
    );
    $constraints = array(
      new ConstraintA(array(
        'groups' => array(
          'Default',
          'Entity',
        ),
      )),
    );
    $members = $this->metadata
      ->getPropertyMetadata('internal');
    $this
      ->assertCount(2, $members);
    $this
      ->assertEquals(self::PARENTCLASS, $members[0]
      ->getClassName());
    $this
      ->assertEquals($parentConstraints, $members[0]
      ->getConstraints());
    $this
      ->assertEquals(self::CLASSNAME, $members[1]
      ->getClassName());
    $this
      ->assertEquals($constraints, $members[1]
      ->getConstraints());
  }
  public function testGetReflectionClass() {
    $reflClass = new \ReflectionClass(self::CLASSNAME);
    $this
      ->assertEquals($reflClass, $this->metadata
      ->getReflectionClass());
  }
  public function testSerialize() {
    $this->metadata
      ->addConstraint(new ConstraintA(array(
      'property1' => 'A',
    )));
    $this->metadata
      ->addConstraint(new ConstraintB(array(
      'groups' => 'TestGroup',
    )));
    $this->metadata
      ->addPropertyConstraint('firstName', new ConstraintA());
    $this->metadata
      ->addGetterConstraint('lastName', new ConstraintB());
    $metadata = unserialize(serialize($this->metadata));
    $this
      ->assertEquals($this->metadata, $metadata);
  }
  public function testGroupSequencesWorkIfContainingDefaultGroup() {
    $this->metadata
      ->setGroupSequence(array(
      'Foo',
      $this->metadata
        ->getDefaultGroup(),
    ));
  }
  public function testGroupSequencesFailIfNotContainingDefaultGroup() {
    $this
      ->setExpectedException('Symfony\\Component\\Validator\\Exception\\GroupDefinitionException');
    $this->metadata
      ->setGroupSequence(array(
      'Foo',
      'Bar',
    ));
  }
  public function testGroupSequencesFailIfContainingDefault() {
    $this
      ->setExpectedException('Symfony\\Component\\Validator\\Exception\\GroupDefinitionException');
    $this->metadata
      ->setGroupSequence(array(
      'Foo',
      $this->metadata
        ->getDefaultGroup(),
      Constraint::DEFAULT_GROUP,
    ));
  }

  /**
   * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
   */
  public function testGroupSequenceFailsIfGroupSequenceProviderIsSet() {
    $metadata = new ClassMetadata(self::PROVIDERCLASS);
    $metadata
      ->setGroupSequenceProvider(true);
    $metadata
      ->setGroupSequence(array(
      'GroupSequenceProviderEntity',
      'Foo',
    ));
  }

  /**
   * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
   */
  public function testGroupSequenceProviderFailsIfGroupSequenceIsSet() {
    $metadata = new ClassMetadata(self::PROVIDERCLASS);
    $metadata
      ->setGroupSequence(array(
      'GroupSequenceProviderEntity',
      'Foo',
    ));
    $metadata
      ->setGroupSequenceProvider(true);
  }

  /**
   * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
   */
  public function testGroupSequenceProviderFailsIfDomainClassIsInvalid() {
    $metadata = new ClassMetadata('stdClass');
    $metadata
      ->setGroupSequenceProvider(true);
  }
  public function testGroupSequenceProvider() {
    $metadata = new ClassMetadata(self::PROVIDERCLASS);
    $metadata
      ->setGroupSequenceProvider(true);
    $this
      ->assertTrue($metadata
      ->isGroupSequenceProvider());
  }

  /**
   * https://github.com/symfony/symfony/issues/11604.
   */
  public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadata() {
    $this
      ->assertCount(0, $this->metadata
      ->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClassMetadataTest::$metadata protected property
ClassMetadataTest::CLASSNAME constant
ClassMetadataTest::PARENTCLASS constant
ClassMetadataTest::PROVIDERCLASS constant
ClassMetadataTest::setUp protected function
ClassMetadataTest::tearDown protected function
ClassMetadataTest::testAddConstraintDoesNotAcceptValid public function
ClassMetadataTest::testAddConstraintRequiresClassConstraints public function
ClassMetadataTest::testAddGetterConstraints public function
ClassMetadataTest::testAddMultipleGetterConstraints public function
ClassMetadataTest::testAddMultiplePropertyConstraints public function
ClassMetadataTest::testAddPropertyConstraints public function
ClassMetadataTest::testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadata public function https://github.com/symfony/symfony/issues/11604.
ClassMetadataTest::testGetReflectionClass public function
ClassMetadataTest::testGroupSequenceFailsIfGroupSequenceProviderIsSet public function @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
ClassMetadataTest::testGroupSequenceProvider public function
ClassMetadataTest::testGroupSequenceProviderFailsIfDomainClassIsInvalid public function @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
ClassMetadataTest::testGroupSequenceProviderFailsIfGroupSequenceIsSet public function @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
ClassMetadataTest::testGroupSequencesFailIfContainingDefault public function
ClassMetadataTest::testGroupSequencesFailIfNotContainingDefaultGroup public function
ClassMetadataTest::testGroupSequencesWorkIfContainingDefaultGroup public function
ClassMetadataTest::testMemberMetadatas public function
ClassMetadataTest::testMergeConstraintsKeepsPrivateMembersSeparate public function
ClassMetadataTest::testMergeConstraintsMergesClassConstraints public function
ClassMetadataTest::testMergeConstraintsMergesMemberConstraints public function
ClassMetadataTest::testSerialize public function