public function DocParserTest::testAnnotationTarget

File

drupal/core/vendor/doctrine/common/tests/Doctrine/Tests/Common/Annotations/DocParserTest.php, line 284

Class

DocParserTest

Namespace

Doctrine\Tests\Common\Annotations

Code

public function testAnnotationTarget() {
  $parser = new DocParser();
  $parser
    ->setImports(array(
    '__NAMESPACE__' => 'Doctrine\\Tests\\Common\\Annotations\\Fixtures',
  ));
  $class = new \ReflectionClass('Doctrine\\Tests\\Common\\Annotations\\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('Doctrine\\Tests\\Common\\Annotations\\Fixtures\\ClassWithInvalidAnnotationTargetAtClass');
    $context = 'class ' . $class
      ->getName();
    $docComment = $class
      ->getDocComment();
    $parser
      ->setTarget(Target::TARGET_CLASS);
    $parser
      ->parse($class
      ->getDocComment(), $context);
    $this
      ->fail();
  } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
    $this
      ->assertNotNull($exc
      ->getMessage());
  }
  try {
    $class = new \ReflectionClass('Doctrine\\Tests\\Common\\Annotations\\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('Doctrine\\Tests\\Common\\Annotations\\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());
  }
}