123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace EasySwoole\DoctrineAnnotation\Tests;
- use EasySwoole\DoctrineAnnotation\PhpParser;
- use PHPUnit\Framework\TestCase;
- use ReflectionClass;
- use stdClass;
- require_once __DIR__ . '/Fixtures/NonNamespacedClass.php';
- require_once __DIR__ . '/Fixtures/GlobalNamespacesPerFileWithClassAsFirst.php';
- require_once __DIR__ . '/Fixtures/GlobalNamespacesPerFileWithClassAsLast.php';
- class PhpParserTest extends TestCase
- {
- public function testParseClassWithMultipleClassesInFile(): void
- {
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\MultipleClassesInFile');
- $parser = new PhpParser();
- self::assertEquals([
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- ], $parser->parseClass($class));
- }
- public function testParseClassWithMultipleImportsInUseStatement(): void
- {
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\MultipleImportsInUseStatement');
- $parser = new PhpParser();
- self::assertEquals([
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- ], $parser->parseClass($class));
- }
- /**
- * @requires PHP 7.0
- */
- public function testParseClassWithGroupUseStatement(): void
- {
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\GroupUseStatement');
- $parser = new PhpParser();
- self::assertEquals([
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'supersecure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ], $parser->parseClass($class));
- }
- public function testParseClassWhenNotUserDefined(): void
- {
- $parser = new PhpParser();
- self::assertEquals([], $parser->parseClass(new ReflectionClass(stdClass::class)));
- }
- public function testClassFileDoesNotExist(): void
- {
- $class = $this->createMock(ReflectionClass::class);
- $class->expects($this->once())
- ->method('getFilename')
- ->will($this->returnValue('/valid/class/Fake.php(35) : eval()d code'));
- $parser = new PhpParser();
- self::assertEquals([], $parser->parseClass($class));
- }
- public function testParseClassWhenClassIsNotNamespaced(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(\AnnotationsTestsFixturesNonNamespacedClass::class);
- self::assertEquals([
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ], $parser->parseClass($class));
- }
- public function testParseClassWhenClassIsInterface(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\HereForTesting');
- self::assertEquals([
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- ], $parser->parseClass($class));
- }
- public function testClassWithFullyQualifiedUseStatements(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\ClassWithFullyQualifiedUseStatements');
- self::assertEquals([
- 'secure' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ], $parser->parseClass($class));
- }
- public function testNamespaceAndClassCommentedOut(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceAndClassCommentedOut');
- self::assertEquals([
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ], $parser->parseClass($class));
- }
- public function testEqualNamespacesPerFileWithClassAsFirst(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\EqualNamespacesPerFileWithClassAsFirst');
- self::assertEquals([
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- ], $parser->parseClass($class));
- }
- public function testEqualNamespacesPerFileWithClassAsLast(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\EqualNamespacesPerFileWithClassAsLast');
- self::assertEquals([
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ], $parser->parseClass($class));
- }
- public function testDifferentNamespacesPerFileWithClassAsFirst(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\DifferentNamespacesPerFileWithClassAsFirst');
- self::assertEquals([
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- ], $parser->parseClass($class));
- }
- public function testDifferentNamespacesPerFileWithClassAsLast(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\DifferentNamespacesPerFileWithClassAsLast');
- self::assertEquals([
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ], $parser->parseClass($class));
- }
- public function testGlobalNamespacesPerFileWithClassAsFirst(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(\GlobalNamespacesPerFileWithClassAsFirst::class);
- self::assertEquals([
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- ], $parser->parseClass($class));
- }
- public function testGlobalNamespacesPerFileWithClassAsLast(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(\GlobalNamespacesPerFileWithClassAsLast::class);
- self::assertEquals([
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ], $parser->parseClass($class));
- }
- public function testNamespaceWithClosureDeclaration(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceWithClosureDeclaration');
- self::assertEquals([
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ], $parser->parseClass($class));
- }
- public function testIfPointerResetsOnMultipleParsingTries(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceWithClosureDeclaration');
- self::assertEquals([
- 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure',
- 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route',
- 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template',
- ], $parser->parseClass($class));
- self::assertEquals([
- '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(): void
- {
- $parser = new PhpParser();
- $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\ClassWithClosure');
- self::assertEquals([
- 'annotationtargetall' => __NAMESPACE__ . '\Fixtures\AnnotationTargetAll',
- 'annotationtargetannotation' => __NAMESPACE__ . '\Fixtures\AnnotationTargetAnnotation',
- ], $parser->parseClass($class));
- }
- }
|