@@ -15,39 +15,42 @@ class Foo
1515}
1616
1717
18- // missing parent
19- $ class = new ClassType ('Test ' );
20- $ manipulator = new ClassManipulator ($ class );
21- Assert::exception (
22- fn () => $ manipulator ->inheritMethod ('bar ' ),
23- Nette \InvalidStateException::class,
24- "Class 'Test' has neither setExtends() nor setImplements() set. " ,
25- );
26-
27- $ class ->setExtends ('Unknown1 ' );
28- $ class ->addImplement ('Unknown2 ' );
29- Assert::exception (
30- fn () => $ manipulator ->inheritMethod ('bar ' ),
31- Nette \InvalidStateException::class,
32- "Method 'bar' has not been found in any ancestor: Unknown1, Unknown2 " ,
33- );
34-
35-
36- // implement method
37- $ class = new ClassType ('Test ' );
38- $ class ->setExtends (Foo::class);
39- $ manipulator = new ClassManipulator ($ class );
40- $ method = $ manipulator ->inheritMethod ('bar ' );
41- Assert::match (<<<'XX'
42- public function bar(int $a, ...$b): void
43- {
44- }
45-
46- XX, (string ) $ method );
47-
48- Assert::same ($ method , $ manipulator ->inheritMethod ('bar ' , returnIfExists: true ));
49- Assert::exception (
50- fn () => $ manipulator ->inheritMethod ('bar ' , returnIfExists: false ),
51- Nette \InvalidStateException::class,
52- "Cannot inherit method 'bar', because it already exists. " ,
53- );
18+ testException ('inheritMethod throws exception when class has no parent ' , function () {
19+ $ class = new ClassType ('Test ' );
20+ $ manipulator = new ClassManipulator ($ class );
21+ $ manipulator ->inheritMethod ('bar ' );
22+ }, Nette \InvalidStateException::class, "Class 'Test' has neither setExtends() nor setImplements() set. " );
23+
24+
25+ testException ('inheritMethod throws exception when method not found in ancestors ' , function () {
26+ $ class = new ClassType ('Test ' );
27+ $ class ->setExtends ('Unknown1 ' );
28+ $ class ->addImplement ('Unknown2 ' );
29+ $ manipulator = new ClassManipulator ($ class );
30+ $ manipulator ->inheritMethod ('bar ' );
31+ }, Nette \InvalidStateException::class, "Method 'bar' has not been found in any ancestor: Unknown1, Unknown2 " );
32+
33+
34+ test ('inheritMethod creates method from parent ' , function () {
35+ $ class = new ClassType ('Test ' );
36+ $ class ->setExtends (Foo::class);
37+ $ manipulator = new ClassManipulator ($ class );
38+ $ method = $ manipulator ->inheritMethod ('bar ' );
39+ Assert::match (<<<'XX'
40+ public function bar(int $a, ...$b): void
41+ {
42+ }
43+
44+ XX, (string ) $ method );
45+
46+ Assert::same ($ method , $ manipulator ->inheritMethod ('bar ' , returnIfExists: true ));
47+ });
48+
49+
50+ testException ('inheritMethod throws exception when method already exists ' , function () {
51+ $ class = new ClassType ('Test ' );
52+ $ class ->setExtends (Foo::class);
53+ $ manipulator = new ClassManipulator ($ class );
54+ $ manipulator ->inheritMethod ('bar ' );
55+ $ manipulator ->inheritMethod ('bar ' , returnIfExists: false );
56+ }, Nette \InvalidStateException::class, "Cannot inherit method 'bar', because it already exists. " );
0 commit comments