DCOM58Test.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace EasySwoole\DoctrineAnnotation\Tests\Ticket;
  3. use EasySwoole\DoctrineAnnotation\AnnotationReader;
  4. use EasySwoole\DoctrineAnnotation\DocParser;
  5. use EasySwoole\DoctrineAnnotation\SimpleAnnotationReader;
  6. use PHPUnit\Framework\TestCase;
  7. use ReflectionClass;
  8. use function array_combine;
  9. use function array_map;
  10. //Some class named Entity in the global namespace
  11. include __DIR__ . '/DCOM58Entity.php';
  12. /**
  13. * @group DCOM58
  14. */
  15. class DCOM58Test extends TestCase
  16. {
  17. public function testIssue(): void
  18. {
  19. $reader = new AnnotationReader();
  20. $result = $reader->getClassAnnotations(new ReflectionClass(__NAMESPACE__ . '\MappedClass'));
  21. $classAnnotations = array_combine(
  22. array_map('get_class', $result),
  23. $result
  24. );
  25. self::assertArrayNotHasKey('', $classAnnotations, 'Class "xxx" is not a valid entity or mapped super class.');
  26. }
  27. public function testIssueGlobalNamespace(): void
  28. {
  29. $docblock = '@Entity';
  30. $parser = new DocParser();
  31. $parser->setImports(['__NAMESPACE__' => 'EasySwoole\DoctrineAnnotation\Tests\Ticket\Doctrine\ORM\Mapping']);
  32. $annots = $parser->parse($docblock);
  33. self::assertCount(1, $annots);
  34. self::assertInstanceOf(Doctrine\ORM\Mapping\Entity::class, $annots[0]);
  35. }
  36. public function testIssueNamespaces(): void
  37. {
  38. $docblock = '@Entity';
  39. $parser = new DocParser();
  40. $parser->addNamespace('EasySwoole\DoctrineAnnotation\Tests\Ticket\Doctrine\ORM');
  41. $annots = $parser->parse($docblock);
  42. self::assertCount(1, $annots);
  43. self::assertInstanceOf(Doctrine\ORM\Entity::class, $annots[0]);
  44. }
  45. public function testIssueMultipleNamespaces(): void
  46. {
  47. $docblock = '@Entity';
  48. $parser = new DocParser();
  49. $parser->addNamespace('EasySwoole\DoctrineAnnotation\Tests\Ticket\Doctrine\ORM\Mapping');
  50. $parser->addNamespace('EasySwoole\DoctrineAnnotation\Tests\Ticket\Doctrine\ORM');
  51. $annots = $parser->parse($docblock);
  52. self::assertCount(1, $annots);
  53. self::assertInstanceOf(Doctrine\ORM\Mapping\Entity::class, $annots[0]);
  54. }
  55. public function testIssueWithNamespacesOrImports(): void
  56. {
  57. $docblock = '@Entity';
  58. $parser = new DocParser();
  59. $annots = $parser->parse($docblock);
  60. self::assertCount(1, $annots);
  61. self::assertInstanceOf(\Entity::class, $annots[0]);
  62. }
  63. public function testIssueSimpleAnnotationReader(): void
  64. {
  65. $reader = new SimpleAnnotationReader();
  66. $reader->addNamespace('EasySwoole\DoctrineAnnotation\Tests\Ticket\Doctrine\ORM\Mapping');
  67. $annots = $reader->getClassAnnotations(new ReflectionClass(__NAMESPACE__ . '\MappedClass'));
  68. self::assertCount(1, $annots);
  69. self::assertInstanceOf(Doctrine\ORM\Mapping\Entity::class, $annots[0]);
  70. }
  71. }
  72. /**
  73. * @Entity
  74. */
  75. class MappedClass
  76. {
  77. }
  78. namespace EasySwoole\DoctrineAnnotation\Tests\Ticket\Doctrine\ORM\Mapping;
  79. /**
  80. * @Annotation
  81. */
  82. class Entity
  83. {
  84. }
  85. namespace EasySwoole\DoctrineAnnotation\Tests\Ticket\Doctrine\ORM;
  86. /**
  87. * @Annotation
  88. */
  89. class Entity
  90. {
  91. }