PhpParserTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Doctrine\Tests\Common\Annotations;
  3. use ReflectionClass;
  4. use Doctrine\Common\Annotations\PhpParser;
  5. require_once __DIR__.'/Fixtures/NonNamespacedClass.php';
  6. require_once __DIR__.'/Fixtures/GlobalNamespacesPerFileWithClassAsFirst.php';
  7. require_once __DIR__.'/Fixtures/GlobalNamespacesPerFileWithClassAsLast.php';
  8. class PhpParserTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public function testParseClassWithMultipleClassesInFile()
  11. {
  12. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\MultipleClassesInFile');
  13. $parser = new PhpParser();
  14. $this->assertEquals(array(
  15. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  16. 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  17. ), $parser->parseClass($class));
  18. }
  19. public function testParseClassWithMultipleImportsInUseStatement()
  20. {
  21. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\MultipleImportsInUseStatement');
  22. $parser = new PhpParser();
  23. $this->assertEquals(array(
  24. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  25. 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  26. ), $parser->parseClass($class));
  27. }
  28. public function testParseClassWhenNotUserDefined()
  29. {
  30. $parser = new PhpParser();
  31. $this->assertEquals(array(), $parser->parseClass(new \ReflectionClass('\stdClass')));
  32. }
  33. public function testClassFileDoesNotExist()
  34. {
  35. $class = $this->getMockBuilder('\ReflectionClass')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $class->expects($this->once())
  39. ->method('getFilename')
  40. ->will($this->returnValue('/valid/class/Fake.php(35) : eval()d code'));
  41. $parser = new PhpParser();
  42. $this->assertEquals(array(), $parser->parseClass($class));
  43. }
  44. public function testParseClassWhenClassIsNotNamespaced()
  45. {
  46. $parser = new PhpParser();
  47. $class = new ReflectionClass('\AnnotationsTestsFixturesNonNamespacedClass');
  48. $this->assertEquals(array(
  49. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  50. 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
  51. ), $parser->parseClass($class));
  52. }
  53. public function testParseClassWhenClassIsInterface()
  54. {
  55. $parser = new PhpParser();
  56. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\TestInterface');
  57. $this->assertEquals(array(
  58. 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  59. ), $parser->parseClass($class));
  60. }
  61. public function testClassWithFullyQualifiedUseStatements()
  62. {
  63. $parser = new PhpParser();
  64. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\ClassWithFullyQualifiedUseStatements');
  65. $this->assertEquals(array(
  66. 'secure' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  67. 'route' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Route',
  68. 'template' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Template',
  69. ), $parser->parseClass($class));
  70. }
  71. public function testNamespaceAndClassCommentedOut()
  72. {
  73. $parser = new PhpParser();
  74. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceAndClassCommentedOut');
  75. $this->assertEquals(array(
  76. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  77. 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
  78. ), $parser->parseClass($class));
  79. }
  80. public function testEqualNamespacesPerFileWithClassAsFirst()
  81. {
  82. $parser = new PhpParser();
  83. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\EqualNamespacesPerFileWithClassAsFirst');
  84. $this->assertEquals(array(
  85. 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  86. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  87. ), $parser->parseClass($class));
  88. }
  89. public function testEqualNamespacesPerFileWithClassAsLast()
  90. {
  91. $parser = new PhpParser();
  92. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\EqualNamespacesPerFileWithClassAsLast');
  93. $this->assertEquals(array(
  94. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  95. 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
  96. ), $parser->parseClass($class));
  97. }
  98. public function testDifferentNamespacesPerFileWithClassAsFirst()
  99. {
  100. $parser = new PhpParser();
  101. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\DifferentNamespacesPerFileWithClassAsFirst');
  102. $this->assertEquals(array(
  103. 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  104. ), $parser->parseClass($class));
  105. }
  106. public function testDifferentNamespacesPerFileWithClassAsLast()
  107. {
  108. $parser = new PhpParser();
  109. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\DifferentNamespacesPerFileWithClassAsLast');
  110. $this->assertEquals(array(
  111. 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
  112. ), $parser->parseClass($class));
  113. }
  114. public function testGlobalNamespacesPerFileWithClassAsFirst()
  115. {
  116. $parser = new PhpParser();
  117. $class = new \ReflectionClass('\GlobalNamespacesPerFileWithClassAsFirst');
  118. $this->assertEquals(array(
  119. 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  120. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  121. ), $parser->parseClass($class));
  122. }
  123. public function testGlobalNamespacesPerFileWithClassAsLast()
  124. {
  125. $parser = new PhpParser();
  126. $class = new ReflectionClass('\GlobalNamespacesPerFileWithClassAsLast');
  127. $this->assertEquals(array(
  128. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  129. 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
  130. ), $parser->parseClass($class));
  131. }
  132. public function testNamespaceWithClosureDeclaration()
  133. {
  134. $parser = new PhpParser();
  135. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceWithClosureDeclaration');
  136. $this->assertEquals(array(
  137. 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  138. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  139. 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
  140. ), $parser->parseClass($class));
  141. }
  142. public function testIfPointerResetsOnMultipleParsingTries()
  143. {
  144. $parser = new PhpParser();
  145. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceWithClosureDeclaration');
  146. $this->assertEquals(array(
  147. 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  148. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  149. 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
  150. ), $parser->parseClass($class));
  151. $this->assertEquals(array(
  152. 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
  153. 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
  154. 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
  155. ), $parser->parseClass($class));
  156. }
  157. /**
  158. * @group DCOM-97
  159. * @group regression
  160. */
  161. public function testClassWithClosure()
  162. {
  163. $parser = new PhpParser();
  164. $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\ClassWithClosure');
  165. $this->assertEquals(array(
  166. 'annotationtargetall' => __NAMESPACE__ . '\Fixtures\AnnotationTargetAll',
  167. 'annotationtargetannotation' => __NAMESPACE__ . '\Fixtures\AnnotationTargetAnnotation',
  168. ), $parser->parseClass($class));
  169. }
  170. }