You are here

public function DocParserTest::testAnnotationWithRequiredAttributes 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::testAnnotationWithRequiredAttributes()

File

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

Class

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

Namespace

Drupal\Tests\Component\Annotation\Doctrine

Code

public function testAnnotationWithRequiredAttributes() {
  $parser = $this
    ->createTestParser();
  $context = 'property SomeClassName::invalidProperty.';
  $parser
    ->setTarget(Target::TARGET_PROPERTY);
  $docblock = '@Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationWithRequiredAttributes("Some Value", annot = @Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationTargetAnnotation)';
  $result = $parser
    ->parse($docblock);
  $this
    ->assertTrue(sizeof($result) === 1);
  $this
    ->assertInstanceOf('Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationWithRequiredAttributes', $result[0]);
  $this
    ->assertEquals("Some Value", $result[0]
    ->getValue());
  $this
    ->assertInstanceOf('Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationTargetAnnotation', $result[0]
    ->getAnnot());
  $docblock = '@Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationWithRequiredAttributes("Some Value")';
  try {
    $result = $parser
      ->parse($docblock, $context);
    $this
      ->fail();
  } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
    $this
      ->assertContains('Attribute "annot" of @Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationWithRequiredAttributes declared on property SomeClassName::invalidProperty. expects a(n) Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationTargetAnnotation. This value should not be null.', $exc
      ->getMessage());
  }
  $docblock = '@Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationWithRequiredAttributes(annot = @Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationTargetAnnotation)';
  try {
    $result = $parser
      ->parse($docblock, $context);
    $this
      ->fail();
  } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
    $this
      ->assertContains('Attribute "value" of @Drupal\\Tests\\Component\\Annotation\\Doctrine\\Fixtures\\AnnotationWithRequiredAttributes declared on property SomeClassName::invalidProperty. expects a(n) string. This value should not be null.', $exc
      ->getMessage());
  }
}