|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: PHP native error messages for undeclared method. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Tester\Assert; |
| 10 | + |
| 11 | +require __DIR__ . '/../bootstrap.php'; |
| 12 | + |
| 13 | + |
| 14 | +class ParentClass |
| 15 | +{ |
| 16 | + public function callPrivate() |
| 17 | + { |
| 18 | + $this->privateMethod(); |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | + public function callPrivateStatic() |
| 23 | + { |
| 24 | + static::privateStaticMethod(); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + private function callPrivateParent() |
| 29 | + { |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +class InterClass extends ParentClass |
| 35 | +{ |
| 36 | + public function callParents() |
| 37 | + { |
| 38 | + parent::callParents(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +class ChildClass extends InterClass |
| 44 | +{ |
| 45 | + public function callParents() |
| 46 | + { |
| 47 | + parent::callParents(); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + public function callMissingParent() |
| 52 | + { |
| 53 | + parent::callMissingParent(); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + public static function callMissingParentStatic() |
| 58 | + { |
| 59 | + parent::callMissingParentStatic(); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public function callPrivateParent() |
| 64 | + { |
| 65 | + parent::callPrivateParent(); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + protected function protectedMethod() |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + protected static function protectedStaticMethod() |
| 75 | + { |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + private function privateMethod() |
| 80 | + { |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + private static function privateStaticMethod() |
| 85 | + { |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +$obj = new ParentClass; |
| 92 | +Assert::exception( |
| 93 | + fn() => $obj->undef(), |
| 94 | + Error::class, |
| 95 | + 'Call to undefined method ParentClass::undef()', |
| 96 | +); |
| 97 | + |
| 98 | +$obj = new ChildClass; |
| 99 | +Assert::exception( |
| 100 | + fn() => $obj->undef(), |
| 101 | + Error::class, |
| 102 | + 'Call to undefined method ChildClass::undef()', |
| 103 | +); |
| 104 | + |
| 105 | +Assert::exception( |
| 106 | + fn() => $obj->callParents(), |
| 107 | + Error::class, |
| 108 | + 'Call to undefined method ParentClass::callParents()', |
| 109 | +); |
| 110 | + |
| 111 | +Assert::exception( |
| 112 | + fn() => $obj->callMissingParent(), |
| 113 | + Error::class, |
| 114 | + 'Call to undefined method InterClass::callMissingParent()', |
| 115 | +); |
| 116 | + |
| 117 | +Assert::exception( |
| 118 | + fn() => $obj->callMissingParentStatic(), |
| 119 | + Error::class, |
| 120 | + 'Call to undefined method InterClass::callMissingParentStatic()', |
| 121 | +); |
| 122 | + |
| 123 | +Assert::exception( |
| 124 | + fn() => $obj::callMissingParentStatic(), |
| 125 | + Error::class, |
| 126 | + 'Call to undefined method InterClass::callMissingParentStatic()', |
| 127 | +); |
| 128 | + |
| 129 | +Assert::exception( |
| 130 | + fn() => $obj->callPrivateParent(), |
| 131 | + Error::class, |
| 132 | + 'Call to private method ParentClass::callPrivateParent() from scope ChildClass', |
| 133 | +); |
| 134 | + |
| 135 | +Assert::exception( |
| 136 | + fn() => $obj->protectedMethod(), |
| 137 | + Error::class, |
| 138 | + 'Call to protected method ChildClass::protectedMethod() from global scope', |
| 139 | +); |
| 140 | + |
| 141 | +Assert::exception( |
| 142 | + fn() => $obj->protectedStaticMethod(), |
| 143 | + Error::class, |
| 144 | + 'Call to protected method ChildClass::protectedStaticMethod() from global scope', |
| 145 | +); |
| 146 | + |
| 147 | +Assert::exception( |
| 148 | + fn() => $obj::protectedStaticMethod(), |
| 149 | + Error::class, |
| 150 | + 'Call to protected method ChildClass::protectedStaticMethod() from global scope', |
| 151 | +); |
| 152 | + |
| 153 | +Assert::exception( |
| 154 | + fn() => $obj->callPrivate(), |
| 155 | + Error::class, |
| 156 | + 'Call to private method ChildClass::privateMethod() from scope ParentClass', |
| 157 | +); |
| 158 | + |
| 159 | +Assert::exception( |
| 160 | + fn() => $obj->callPrivateStatic(), |
| 161 | + Error::class, |
| 162 | + 'Call to private method ChildClass::privateStaticMethod() from scope ParentClass', |
| 163 | +); |
0 commit comments