You are here

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

File

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

Class

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

Namespace

Drupal\Tests\Component\Annotation\Doctrine

Code

public function testBasicAnnotations() {
  $parser = $this
    ->createTestParser();

  // Marker annotation
  $result = $parser
    ->parse("@Name");
  $annot = $result[0];
  $this
    ->assertInstanceOf(Name::class, $annot);
  $this
    ->assertNull($annot->value);
  $this
    ->assertNull($annot->foo);

  // Associative arrays
  $result = $parser
    ->parse('@Name(foo={"key1" = "value1"})');
  $annot = $result[0];
  $this
    ->assertNull($annot->value);
  $this
    ->assertIsArray($annot->foo);
  $this
    ->assertTrue(isset($annot->foo['key1']));

  // Numerical arrays
  $result = $parser
    ->parse('@Name({2="foo", 4="bar"})');
  $annot = $result[0];
  $this
    ->assertIsArray($annot->value);
  $this
    ->assertEquals('foo', $annot->value[2]);
  $this
    ->assertEquals('bar', $annot->value[4]);
  $this
    ->assertFalse(isset($annot->value[0]));
  $this
    ->assertFalse(isset($annot->value[1]));
  $this
    ->assertFalse(isset($annot->value[3]));

  // Multiple values
  $result = $parser
    ->parse('@Name(@Name, @Name)');
  $annot = $result[0];
  $this
    ->assertInstanceOf(Name::class, $annot);
  $this
    ->assertIsArray($annot->value);
  $this
    ->assertInstanceOf(Name::class, $annot->value[0]);
  $this
    ->assertInstanceOf(Name::class, $annot->value[1]);

  // Multiple types as values
  $result = $parser
    ->parse('@Name(foo="Bar", @Name, {"key1"="value1", "key2"="value2"})');
  $annot = $result[0];
  $this
    ->assertInstanceOf(Name::class, $annot);
  $this
    ->assertIsArray($annot->value);
  $this
    ->assertInstanceOf(Name::class, $annot->value[0]);
  $this
    ->assertIsArray($annot->value[1]);
  $this
    ->assertEquals('value1', $annot->value[1]['key1']);
  $this
    ->assertEquals('value2', $annot->value[1]['key2']);

  // Complete docblock
  $docblock = <<<DOCBLOCK
/**
 * Some nifty class.
 *
 * @author Mr.X
 * @Name(foo="bar")
 */
DOCBLOCK;
  $result = $parser
    ->parse($docblock);
  $this
    ->assertCount(1, $result);
  $annot = $result[0];
  $this
    ->assertInstanceOf(Name::class, $annot);
  $this
    ->assertEquals("bar", $annot->foo);
  $this
    ->assertNull($annot->value);
}