4
0

AnalysisTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. use OpenApi\Analysis;
  7. use OpenApi\StaticAnalyser;
  8. class AnalysisTest extends OpenApiTestCase
  9. {
  10. public function testRegisterProcessor()
  11. {
  12. $counter = 0;
  13. $analysis = new Analysis();
  14. $analysis->process();
  15. $this->assertSame(0, $counter);
  16. $countProcessor = function (Analysis $a) use (&$counter) {
  17. $counter++;
  18. };
  19. Analysis::registerProcessor($countProcessor);
  20. $analysis->process();
  21. $this->assertSame(1, $counter);
  22. Analysis::unregisterProcessor($countProcessor);
  23. $analysis->process();
  24. $this->assertSame(1, $counter);
  25. }
  26. public function testGetSubclasses()
  27. {
  28. $analyser = new StaticAnalyser();
  29. $analysis = $analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/Child.php');
  30. $analysis->addAnalysis($analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/GrandAncestor.php'));
  31. $analysis->addAnalysis($analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/Ancestor.php'));
  32. $this->assertCount(3, $analysis->classes, '3 classes should\'ve been detected');
  33. $subclasses = $analysis->getSubClasses('\OpenApiFixtures\GrandAncestor');
  34. $this->assertCount(2, $subclasses, 'GrandAncestor has 2 subclasses');
  35. $this->assertSame(['\OpenApiFixtures\Ancestor', '\AnotherNamespace\Child'], array_keys($subclasses));
  36. $this->assertSame(['\AnotherNamespace\Child'], array_keys($analysis->getSubClasses('\OpenApiFixtures\Ancestor')));
  37. }
  38. public function testGetAncestorClasses()
  39. {
  40. $analyser = new StaticAnalyser();
  41. $analysis = $analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/Child.php');
  42. $analysis->addAnalysis($analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/GrandAncestor.php'));
  43. $analysis->addAnalysis($analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/Ancestor.php'));
  44. $this->assertCount(3, $analysis->classes, '3 classes should\'ve been detected');
  45. $superclasses = $analysis->getSuperClasses('\AnotherNamespace\Child');
  46. $this->assertCount(2, $superclasses, 'Child has a chain of 2 super classes');
  47. $this->assertSame(['\OpenApiFixtures\Ancestor', '\OpenApiFixtures\GrandAncestor'], array_keys($superclasses));
  48. $this->assertSame(['\OpenApiFixtures\GrandAncestor'], array_keys($analysis->getSuperClasses('\OpenApiFixtures\Ancestor')));
  49. }
  50. }