Reflection.getDeclaringMethod.phpt 905 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 bar()
  12. {
  13. }
  14. }
  15. trait B
  16. {
  17. use A;
  18. public function foo()
  19. {
  20. }
  21. }
  22. trait E
  23. {
  24. public function baz()
  25. {
  26. }
  27. }
  28. class C
  29. {
  30. use B;
  31. use E;
  32. public function own()
  33. {
  34. }
  35. }
  36. class D extends C
  37. {
  38. }
  39. function get(ReflectionMethod $m)
  40. {
  41. $res = Reflection::getMethodDeclaringMethod($m);
  42. return $res->getDeclaringClass()->name . '::' . $res->name;
  43. }
  44. // Method in trait
  45. Assert::same('B::foo', get(new ReflectionMethod('D', 'foo')));
  46. // Method in parent trait
  47. Assert::same('A::bar', get(new ReflectionMethod('D', 'bar')));
  48. // Method in class itself
  49. Assert::same('C::own', get(new ReflectionMethod('D', 'own')));
  50. // Method in second trait
  51. Assert::same('E::baz', get(new ReflectionMethod('D', 'baz')));