Reflection.getDeclaringMethod.alias.phpt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Reflection::getDeclaringMethod
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Reflection;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. trait A
  10. {
  11. public function foo()
  12. {
  13. }
  14. }
  15. trait B
  16. {
  17. use A {
  18. A::foo as foo2;
  19. }
  20. }
  21. trait B2
  22. {
  23. use A {
  24. A::foo as foo2;
  25. }
  26. public function foo2()
  27. {
  28. }
  29. }
  30. class E1
  31. {
  32. use B {
  33. B::foo2 as alias;
  34. }
  35. }
  36. class E2
  37. {
  38. use B {
  39. B::foo2 as alias;
  40. }
  41. public function foo2()
  42. {
  43. }
  44. public function alias()
  45. {
  46. }
  47. }
  48. class E3
  49. {
  50. use B2 {
  51. B2::foo as foo3;
  52. }
  53. }
  54. function get(ReflectionMethod $m)
  55. {
  56. $res = Reflection::getMethodDeclaringMethod($m);
  57. return $res->getDeclaringClass()->name . '::' . $res->name;
  58. }
  59. // new ReflectionMethod and getMethod returns different method names, PHP #79636
  60. // Method in trait
  61. Assert::same('A::foo', get((new ReflectionClass('E3'))->getMethod('foo3')));
  62. Assert::same('A::foo', get(new ReflectionMethod('E3', 'foo3')));
  63. Assert::same('B2::foo2', get((new ReflectionClass('E3'))->getMethod('foo2')));
  64. Assert::same('B2::foo2', get(new ReflectionMethod('E3', 'foo2')));
  65. Assert::same('A::foo', get((new ReflectionClass('E3'))->getMethod('foo')));
  66. Assert::same('A::foo', get(new ReflectionMethod('E3', 'foo')));
  67. // Method in class
  68. Assert::same('E2::alias', get((new ReflectionClass('E2'))->getMethod('alias')));
  69. Assert::same('E2::alias', get(new ReflectionMethod('E2', 'alias')));
  70. Assert::same('E2::foo2', get((new ReflectionClass('E2'))->getMethod('foo2')));
  71. Assert::same('E2::foo2', get(new ReflectionMethod('E2', 'foo2')));
  72. // Method in trait
  73. Assert::same('A::foo', get((new ReflectionClass('E1'))->getMethod('alias')));
  74. Assert::same('A::foo', get(new ReflectionMethod('E1', 'alias')));
  75. // Method in trait
  76. Assert::same('B2::foo2', get((new ReflectionClass('B2'))->getMethod('foo2')));
  77. Assert::same('B2::foo2', get(new ReflectionMethod('B2', 'foo2')));
  78. Assert::same('A::foo', get((new ReflectionClass('B'))->getMethod('foo2')));
  79. Assert::same('A::foo', get(new ReflectionMethod('B', 'foo2')));
  80. Assert::same('A::foo', get((new ReflectionClass('A'))->getMethod('foo')));
  81. Assert::same('A::foo', get(new ReflectionMethod('A', 'foo')));