ValidateRelationsTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. /**
  7. * Test if the nesting/parent relations are coherent.
  8. */
  9. class ValidateRelationsTest extends OpenApiTestCase
  10. {
  11. /**
  12. *
  13. * @dataProvider getAnnotationClasses
  14. *
  15. * @param string $class
  16. */
  17. public function testAncestors($class)
  18. {
  19. foreach ($class::$_parents as $parent) {
  20. $found = false;
  21. foreach (array_keys($parent::$_nested) as $nested) {
  22. if ($nested === $class) {
  23. $found = true;
  24. break;
  25. }
  26. }
  27. if ($found === false) {
  28. $this->fail($class.' not found in '.$parent."::\$_nested. Found:\n ".implode("\n ", array_keys($parent::$_nested)));
  29. }
  30. }
  31. }
  32. /**
  33. *
  34. * @dataProvider getAnnotationClasses
  35. *
  36. * @param string $class
  37. */
  38. public function testNested($class)
  39. {
  40. foreach (array_keys($class::$_nested) as $nested) {
  41. $found = false;
  42. foreach ($nested::$_parents as $parent) {
  43. if ($parent === $class) {
  44. $found = true;
  45. break;
  46. }
  47. }
  48. if ($found === false) {
  49. $this->fail($class.' not found in '.$nested."::\$parent. Found:\n ".implode("\n ", $nested::$_parents));
  50. }
  51. }
  52. }
  53. /**
  54. * dataProvider for testExample
  55. *
  56. * @return array
  57. */
  58. public function getAnnotationClasses()
  59. {
  60. $classes = [];
  61. $dir = new \DirectoryIterator(__DIR__.'/../src/Annotations');
  62. foreach ($dir as $entry) {
  63. if ($entry->getFilename() === 'AbstractAnnotation.php') {
  64. continue;
  65. }
  66. if ($entry->getExtension() === 'php') {
  67. $classes[] = ['OpenApi\Annotations\\'.substr($entry->getFilename(), 0, -4)];
  68. }
  69. }
  70. return $classes;
  71. }
  72. }