|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Tester\Assert; |
| 6 | + |
| 7 | +require __DIR__ . '/../bootstrap.php'; |
| 8 | + |
| 9 | + |
| 10 | +class Foo |
| 11 | +{ |
| 12 | + public function bar(int $a, ...$b): void |
| 13 | + { |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +// missing parent |
| 19 | +$class = new Nette\PhpGenerator\ClassType('Test'); |
| 20 | +Assert::exception( |
| 21 | + fn() => $class->inheritMethod('bar'), |
| 22 | + Nette\InvalidStateException::class, |
| 23 | + "Class 'Test' has neither setExtends() nor setImplements() set.", |
| 24 | +); |
| 25 | + |
| 26 | +$class->setExtends('Unknown1'); |
| 27 | +$class->addImplement('Unknown2'); |
| 28 | +Assert::exception( |
| 29 | + fn() => $class->inheritMethod('bar'), |
| 30 | + Nette\InvalidStateException::class, |
| 31 | + "Method 'bar' has not been found in any ancestor: Unknown1, Unknown2", |
| 32 | +); |
| 33 | + |
| 34 | + |
| 35 | +// implemented method |
| 36 | +$class = new Nette\PhpGenerator\ClassType('Test'); |
| 37 | +$class->setExtends(Foo::class); |
| 38 | +$method = $class->inheritMethod('bar'); |
| 39 | +Assert::match(<<<'XX' |
| 40 | + public function bar(int $a, ...$b): void |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + XX, (string) $method); |
| 45 | + |
| 46 | +$class = new Nette\PhpGenerator\ClassType('Test'); |
| 47 | +$class->setExtends(Foo::class); |
| 48 | +$method = $class->inheritMethod('Bar', callParent: true); // intentionally case insensitive |
| 49 | +Assert::match(<<<'XX' |
| 50 | + public function bar(int $a, ...$b): void |
| 51 | + { |
| 52 | + parent::bar($a, ...$b); |
| 53 | + } |
| 54 | + |
| 55 | + XX, (string) $method); |
| 56 | + |
| 57 | + |
| 58 | +// exists |
| 59 | +$class = new Nette\PhpGenerator\ClassType('Test'); |
| 60 | +$method = $class->addMethod('bar'); |
| 61 | +Assert::same($method, $class->inheritMethod('bar', returnIfExists: true)); |
| 62 | +Assert::exception( |
| 63 | + fn() => $class->inheritMethod('bar', returnIfExists: false), |
| 64 | + Nette\InvalidStateException::class, |
| 65 | + "Cannot inherit method 'bar', because it already exists.", |
| 66 | +); |
0 commit comments