2009-07-07 00:34:54 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\Common\Annotations;
|
|
|
|
|
|
|
|
use Doctrine\Common\Annotations\Parser;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
class ParserTest extends \Doctrine\Tests\DoctrineTestCase
|
|
|
|
{
|
|
|
|
public function testBasicAnnotations()
|
|
|
|
{
|
2009-10-30 23:58:06 +03:00
|
|
|
$parser = $this->createTestParser();
|
2009-07-07 00:34:54 +04:00
|
|
|
|
|
|
|
// Marker annotation
|
|
|
|
$result = $parser->parse("@Name");
|
|
|
|
$annot = $result['Doctrine\Tests\Common\Annotations\Name'];
|
|
|
|
$this->assertTrue($annot instanceof Name);
|
|
|
|
$this->assertNull($annot->value);
|
|
|
|
$this->assertNull($annot->foo);
|
|
|
|
|
|
|
|
// Associative arrays
|
|
|
|
$result = $parser->parse('@Name(foo={"key1" = "value1"})');
|
|
|
|
$annot = $result['Doctrine\Tests\Common\Annotations\Name'];
|
|
|
|
$this->assertNull($annot->value);
|
|
|
|
$this->assertTrue(is_array($annot->foo));
|
|
|
|
$this->assertTrue(isset($annot->foo['key1']));
|
2010-03-31 21:19:32 +04:00
|
|
|
|
|
|
|
// Numerical arrays
|
|
|
|
$result = $parser->parse('@Name({2="foo", 4="bar"})');
|
|
|
|
$annot = $result['Doctrine\Tests\Common\Annotations\Name'];
|
|
|
|
$this->assertTrue(is_array($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]));
|
2009-07-07 00:34:54 +04:00
|
|
|
|
|
|
|
// Nested arrays with nested annotations
|
2009-07-08 19:25:41 +04:00
|
|
|
$result = $parser->parse('@Name(foo={1,2, {"key"=@Name}})');
|
2009-07-07 00:34:54 +04:00
|
|
|
$annot = $result['Doctrine\Tests\Common\Annotations\Name'];
|
2009-07-08 19:25:41 +04:00
|
|
|
|
2009-07-07 00:34:54 +04:00
|
|
|
$this->assertTrue($annot instanceof Name);
|
2009-07-08 19:25:41 +04:00
|
|
|
$this->assertNull($annot->value);
|
|
|
|
$this->assertEquals(3, count($annot->foo));
|
|
|
|
$this->assertEquals(1, $annot->foo[0]);
|
|
|
|
$this->assertEquals(2, $annot->foo[1]);
|
|
|
|
$this->assertTrue(is_array($annot->foo[2]));
|
2009-07-07 00:34:54 +04:00
|
|
|
|
2009-07-08 19:25:41 +04:00
|
|
|
$nestedArray = $annot->foo[2];
|
|
|
|
$this->assertTrue(isset($nestedArray['key']));
|
|
|
|
$this->assertTrue($nestedArray['key'] instanceof Name);
|
2009-07-07 00:34:54 +04:00
|
|
|
|
|
|
|
// Complete docblock
|
|
|
|
$docblock = <<<DOCBLOCK
|
|
|
|
/**
|
|
|
|
* Some nifty class.
|
|
|
|
*
|
|
|
|
* @author Mr.X
|
|
|
|
* @Name(foo="bar")
|
|
|
|
*/
|
|
|
|
DOCBLOCK;
|
|
|
|
|
|
|
|
$result = $parser->parse($docblock);
|
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
$annot = $result['Doctrine\Tests\Common\Annotations\Name'];
|
|
|
|
$this->assertTrue($annot instanceof Name);
|
|
|
|
$this->assertEquals("bar", $annot->foo);
|
|
|
|
$this->assertNull($annot->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNamespacedAnnotations()
|
|
|
|
{
|
|
|
|
$parser = new Parser;
|
|
|
|
|
|
|
|
$docblock = <<<DOCBLOCK
|
|
|
|
/**
|
|
|
|
* Some nifty class.
|
|
|
|
*
|
|
|
|
* @author Mr.X
|
2009-07-07 15:25:58 +04:00
|
|
|
* @Doctrine\Tests\Common\Annotations\Name(foo="bar")
|
2009-07-07 00:34:54 +04:00
|
|
|
*/
|
|
|
|
DOCBLOCK;
|
|
|
|
|
2009-10-30 03:20:17 +03:00
|
|
|
$result = $parser->parse($docblock);
|
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
$annot = $result['Doctrine\Tests\Common\Annotations\Name'];
|
|
|
|
$this->assertTrue($annot instanceof Name);
|
|
|
|
$this->assertEquals("bar", $annot->foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-77
|
|
|
|
*/
|
|
|
|
public function testAnnotationWithoutClassIsIgnoredWithoutWarning()
|
|
|
|
{
|
|
|
|
$parser = new Parser();
|
|
|
|
$result = $parser->parse("@param");
|
|
|
|
|
|
|
|
$this->assertEquals(0, count($result));
|
2009-07-07 00:34:54 +04:00
|
|
|
}
|
2009-10-30 03:36:21 +03:00
|
|
|
|
|
|
|
public function testAnnotationDontAcceptSingleQuotes()
|
|
|
|
{
|
|
|
|
$this->setExpectedException(
|
|
|
|
'Doctrine\Common\Annotations\AnnotationException',
|
2009-11-04 04:52:40 +03:00
|
|
|
"[Syntax Error] Expected PlainValue, got ''' at position 10."
|
2009-10-30 03:36:21 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$parser = $this->createTestParser();
|
|
|
|
$parser->parse("@Name(foo='bar')");
|
|
|
|
}
|
2010-02-24 17:33:12 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group parse
|
|
|
|
*/
|
|
|
|
public function testAnnotationNamespaceAlias()
|
|
|
|
{
|
2010-02-24 22:19:04 +03:00
|
|
|
$parser = $this->createTestParser();
|
2010-02-24 17:33:12 +03:00
|
|
|
$parser->setAnnotationNamespaceAlias('Doctrine\Tests\Common\Annotations\\', 'alias');
|
|
|
|
$docblock = <<<DOCBLOCK
|
|
|
|
/**
|
|
|
|
* Some nifty class.
|
|
|
|
*
|
|
|
|
* @author Mr.X
|
|
|
|
* @alias:Name(foo="stuff")
|
|
|
|
*/
|
|
|
|
DOCBLOCK;
|
|
|
|
|
|
|
|
$result = $parser->parse($docblock);
|
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
$annot = $result['Doctrine\Tests\Common\Annotations\Name'];
|
|
|
|
$this->assertTrue($annot instanceof Name);
|
|
|
|
$this->assertEquals("stuff", $annot->foo);
|
|
|
|
}
|
2009-10-30 03:36:21 +03:00
|
|
|
|
|
|
|
public function createTestParser()
|
|
|
|
{
|
|
|
|
$parser = new Parser();
|
|
|
|
$parser->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');
|
|
|
|
return $parser;
|
|
|
|
}
|
2009-10-30 23:58:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-78
|
|
|
|
*/
|
|
|
|
public function testSyntaxErrorWithContextDescription()
|
|
|
|
{
|
|
|
|
$this->setExpectedException(
|
|
|
|
'Doctrine\Common\Annotations\AnnotationException',
|
2009-11-04 04:52:40 +03:00
|
|
|
"[Syntax Error] Expected PlainValue, got ''' at position 10 ".
|
2009-10-30 23:58:06 +03:00
|
|
|
"in class \Doctrine\Tests\Common\Annotations\Name"
|
|
|
|
);
|
|
|
|
|
|
|
|
$parser = $this->createTestParser();
|
|
|
|
$parser->parse("@Name(foo='bar')", "class \Doctrine\Tests\Common\Annotations\Name");
|
|
|
|
}
|
2009-12-22 05:15:12 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-183
|
|
|
|
*/
|
|
|
|
public function testSyntaxErrorWithUnknownCharacters()
|
|
|
|
{
|
|
|
|
$docblock = <<<DOCBLOCK
|
|
|
|
/**
|
|
|
|
* @test at.
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
}
|
|
|
|
DOCBLOCK;
|
|
|
|
|
|
|
|
//$lexer = new \Doctrine\Common\Annotations\Lexer();
|
|
|
|
//$lexer->setInput(trim($docblock, '/ *'));
|
|
|
|
//var_dump($lexer);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$parser = $this->createTestParser();
|
|
|
|
$result = $parser->parse($docblock);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->fail($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2009-07-07 00:34:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Name extends \Doctrine\Common\Annotations\Annotation {
|
|
|
|
public $foo;
|
|
|
|
}
|