function DocParserTest::testAnnotationTarget

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php \Drupal\Tests\Component\Annotation\Doctrine\DocParserTest::testAnnotationTarget()
  2. 8.9.x core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php \Drupal\Tests\Component\Annotation\Doctrine\DocParserTest::testAnnotationTarget()
  3. 10 core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php \Drupal\Tests\Component\Annotation\Doctrine\DocParserTest::testAnnotationTarget()

File

core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php, line 321

Class

DocParserTest
This class is a near-copy of Doctrine\Tests\Common\Annotations\DocParserTest, which is part of the Doctrine project: <http://www.doctrine-project.org&gt;. It was copied from version 1.2.7.

Namespace

Drupal\Tests\Component\Annotation\Doctrine

Code

public function testAnnotationTarget() : void {
    $parser = new DocParser();
    $parser->setImports(array(
        '__NAMESPACE__' => 'Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures',
    ));
    $class = new \ReflectionClass('Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\ClassWithValidAnnotationTarget');
    $context = 'class ' . $class->getName();
    $docComment = $class->getDocComment();
    $parser->setTarget(Target::TARGET_CLASS);
    $this->assertNotNull($parser->parse($docComment, $context));
    $property = $class->getProperty('foo');
    $docComment = $property->getDocComment();
    $context = 'property ' . $class->getName() . "::\$" . $property->getName();
    $parser->setTarget(Target::TARGET_PROPERTY);
    $this->assertNotNull($parser->parse($docComment, $context));
    $method = $class->getMethod('someFunction');
    $docComment = $property->getDocComment();
    $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
    $parser->setTarget(Target::TARGET_METHOD);
    $this->assertNotNull($parser->parse($docComment, $context));
    try {
        $class = new \ReflectionClass('Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\ClassWithInvalidAnnotationTargetAtClass');
        $context = 'class ' . $class->getName();
        $docComment = $class->getDocComment();
        $parser->setTarget(Target::TARGET_CLASS);
        $parser->parse($docComment, $context);
        $this->fail();
    } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
        $this->assertNotNull($exc->getMessage());
    }
    try {
        $class = new \ReflectionClass('Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\ClassWithInvalidAnnotationTargetAtMethod');
        $method = $class->getMethod('functionName');
        $docComment = $method->getDocComment();
        $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
        $parser->setTarget(Target::TARGET_METHOD);
        $parser->parse($docComment, $context);
        $this->fail();
    } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
        $this->assertNotNull($exc->getMessage());
    }
    try {
        $class = new \ReflectionClass('Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\ClassWithInvalidAnnotationTargetAtProperty');
        $property = $class->getProperty('foo');
        $docComment = $property->getDocComment();
        $context = 'property ' . $class->getName() . "::\$" . $property->getName();
        $parser->setTarget(Target::TARGET_PROPERTY);
        $parser->parse($docComment, $context);
        $this->fail();
    } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
        $this->assertNotNull($exc->getMessage());
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.