You are here

public function DocParserTest::testAnnotationTarget in Drupal 8

Same name and namespace in other branches
  1. 9 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 322

Class

DocParserTest
@coversDefaultClass \Drupal\Component\Annotation\Doctrine\DocParser

Namespace

Drupal\Tests\Component\Annotation\Doctrine

Code

public function testAnnotationTarget() {
  $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());
  }
}