123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace Doctrine\Tests\Common\Annotations;
- use ReflectionClass;
- use Doctrine\Common\Annotations\PhpParser;
- require_once __DIR__.'/Fixtures/NonNamespacedClass.php';
- require_once __DIR__.'/Fixtures/GlobalNamespacesPerFileWithClassAsFirst.php';
- require_once __DIR__.'/Fixtures/GlobalNamespacesPerFileWithClassAsLast.php';
- class PhpParserTest extends \PHPUnit_Framework_TestCase
- {
- public function testParseClassWithMultipleClassesInFile()
- {
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\MultipleClassesInFile');
- $parser = new PhpParser();
- $this->assertEquals(array(
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- ), $parser->parseClass($class));
- }
- public function testParseClassWithMultipleImportsInUseStatement()
- {
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\MultipleImportsInUseStatement');
- $parser = new PhpParser();
- $this->assertEquals(array(
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- ), $parser->parseClass($class));
- }
- public function testParseClassWhenNotUserDefined()
- {
- $parser = new PhpParser();
- $this->assertEquals(array(), $parser->parseClass(new \ReflectionClass('\stdClass')));
- }
- public function testClassFileDoesNotExist()
- {
- $class = $this->getMockBuilder('\ReflectionClass')
- ->disableOriginalConstructor()
- ->getMock();
- $class->expects($this->once())
- ->method('getFilename')
- ->will($this->returnValue('/valid/class/Fake.php(35) : eval()d code'));
- $parser = new PhpParser();
- $this->assertEquals(array(), $parser->parseClass($class));
- }
- public function testParseClassWhenClassIsNotNamespaced()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass('\AnnotationsTestsFixturesNonNamespacedClass');
- $this->assertEquals(array(
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ), $parser->parseClass($class));
- }
- public function testParseClassWhenClassIsInterface()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\TestInterface');
- $this->assertEquals(array(
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- ), $parser->parseClass($class));
- }
- public function testClassWithFullyQualifiedUseStatements()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\ClassWithFullyQualifiedUseStatements');
- $this->assertEquals(array(
- 'secure' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ), $parser->parseClass($class));
- }
- public function testNamespaceAndClassCommentedOut()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceAndClassCommentedOut');
- $this->assertEquals(array(
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ), $parser->parseClass($class));
- }
- public function testEqualNamespacesPerFileWithClassAsFirst()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\EqualNamespacesPerFileWithClassAsFirst');
- $this->assertEquals(array(
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- ), $parser->parseClass($class));
- }
- public function testEqualNamespacesPerFileWithClassAsLast()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\EqualNamespacesPerFileWithClassAsLast');
- $this->assertEquals(array(
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ), $parser->parseClass($class));
- }
- public function testDifferentNamespacesPerFileWithClassAsFirst()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\DifferentNamespacesPerFileWithClassAsFirst');
- $this->assertEquals(array(
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- ), $parser->parseClass($class));
- }
- public function testDifferentNamespacesPerFileWithClassAsLast()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\DifferentNamespacesPerFileWithClassAsLast');
- $this->assertEquals(array(
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ), $parser->parseClass($class));
- }
- public function testGlobalNamespacesPerFileWithClassAsFirst()
- {
- $parser = new PhpParser();
- $class = new \ReflectionClass('\GlobalNamespacesPerFileWithClassAsFirst');
- $this->assertEquals(array(
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- ), $parser->parseClass($class));
- }
- public function testGlobalNamespacesPerFileWithClassAsLast()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass('\GlobalNamespacesPerFileWithClassAsLast');
- $this->assertEquals(array(
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ), $parser->parseClass($class));
- }
- public function testNamespaceWithClosureDeclaration()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceWithClosureDeclaration');
- $this->assertEquals(array(
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ), $parser->parseClass($class));
- }
- public function testIfPointerResetsOnMultipleParsingTries()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceWithClosureDeclaration');
- $this->assertEquals(array(
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ), $parser->parseClass($class));
- $this->assertEquals(array(
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ), $parser->parseClass($class));
- }
- /**
- * @group DCOM-97
- * @group regression
- */
- public function testClassWithClosure()
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\ClassWithClosure');
- $this->assertEquals(array(
- 'annotationtargetall' => __NAMESPACE__ . '\Fixtures\AnnotationTargetAll',
- 'annotationtargetannotation' => __NAMESPACE__ . '\Fixtures\AnnotationTargetAnnotation',
- ), $parser->parseClass($class));
- }
- }
|