SmartObject.undeclaredMethod.phpt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Test: Nette\SmartObject error messages for undeclared method.
  4. */
  5. declare(strict_types=1);
  6. use Tester\Assert;
  7. require __DIR__ . '/../bootstrap.php';
  8. class ParentClass
  9. {
  10. use Nette\SmartObject;
  11. public function callPrivate()
  12. {
  13. $this->privateMethod();
  14. }
  15. public function callPrivateStatic()
  16. {
  17. static::privateStaticMethod();
  18. }
  19. private function callPrivateParent()
  20. {
  21. }
  22. }
  23. class InterClass extends ParentClass
  24. {
  25. public function callParents()
  26. {
  27. parent::callParents();
  28. }
  29. }
  30. class ChildClass extends InterClass
  31. {
  32. public function callParents()
  33. {
  34. parent::callParents();
  35. }
  36. public function callMissingParent()
  37. {
  38. parent::callMissingParent();
  39. }
  40. public static function callMissingParentStatic()
  41. {
  42. parent::callMissingParentStatic();
  43. }
  44. public function callPrivateParent()
  45. {
  46. parent::callPrivateParent();
  47. }
  48. protected function protectedMethod()
  49. {
  50. }
  51. protected static function protectedStaticMethod()
  52. {
  53. }
  54. private function privateMethod()
  55. {
  56. }
  57. private static function privateStaticMethod()
  58. {
  59. }
  60. }
  61. $obj = new ParentClass;
  62. Assert::exception(
  63. fn() => $obj->undef(),
  64. Nette\MemberAccessException::class,
  65. 'Call to undefined method ParentClass::undef().',
  66. );
  67. $obj = new ChildClass;
  68. Assert::exception(
  69. fn() => $obj->undef(),
  70. Nette\MemberAccessException::class,
  71. 'Call to undefined method ChildClass::undef().',
  72. );
  73. Assert::exception(
  74. fn() => $obj->callParents(),
  75. Nette\MemberAccessException::class,
  76. 'Call to undefined method ParentClass::callParents().',
  77. );
  78. Assert::exception(
  79. fn() => $obj->callMissingParent(),
  80. Nette\MemberAccessException::class,
  81. 'Call to undefined method InterClass::callMissingParent().',
  82. );
  83. Assert::exception(
  84. fn() => $obj->callMissingParentStatic(),
  85. Nette\MemberAccessException::class,
  86. 'Call to undefined static method InterClass::callMissingParentStatic().',
  87. );
  88. Assert::exception(
  89. fn() => $obj::callMissingParentStatic(),
  90. Nette\MemberAccessException::class,
  91. 'Call to undefined static method InterClass::callMissingParentStatic().',
  92. );
  93. Assert::exception(
  94. fn() => $obj->callPrivateParent(),
  95. Nette\MemberAccessException::class,
  96. PHP_VERSION_ID < 80100
  97. ? 'Call to undefined static method InterClass::callPrivateParent().' // differs from native error message
  98. : 'Call to undefined method InterClass::callPrivateParent().',
  99. );
  100. Assert::exception(
  101. fn() => $obj->protectedMethod(),
  102. Nette\MemberAccessException::class,
  103. 'Call to protected method ChildClass::protectedMethod() from global scope.',
  104. );
  105. Assert::exception(
  106. fn() => $obj->protectedStaticMethod(),
  107. Nette\MemberAccessException::class,
  108. 'Call to protected method ChildClass::protectedStaticMethod() from global scope.',
  109. );
  110. Assert::exception(
  111. fn() => $obj::protectedStaticMethod(),
  112. Nette\MemberAccessException::class,
  113. 'Call to protected method ChildClass::protectedStaticMethod() from global scope.',
  114. );
  115. Assert::exception(
  116. fn() => $obj->callPrivate(),
  117. Nette\MemberAccessException::class,
  118. 'Call to private method ChildClass::privateMethod() from scope ParentClass.',
  119. );
  120. Assert::exception(
  121. fn() => $obj->callPrivateStatic(),
  122. Nette\MemberAccessException::class,
  123. 'Call to private method ChildClass::privateStaticMethod() from scope ParentClass.',
  124. );