PhpParserTest.php 8.2 KB

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