-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ClassType: added inheritMethod() & inheritProperty()
- Loading branch information
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
class Foo | ||
{ | ||
public function bar(int $a, ...$b): void | ||
{ | ||
} | ||
} | ||
|
||
|
||
// missing parent | ||
$class = new Nette\PhpGenerator\ClassType('Test'); | ||
Assert::exception( | ||
fn() => $class->inheritMethod('bar'), | ||
Nette\InvalidStateException::class, | ||
"Class 'Test' has neither setExtends() nor setImplements() set.", | ||
); | ||
|
||
$class->setExtends('Unknown1'); | ||
$class->addImplement('Unknown2'); | ||
Assert::exception( | ||
fn() => $class->inheritMethod('bar'), | ||
Nette\InvalidStateException::class, | ||
"Method 'bar' has not been found in any ancestor: Unknown1, Unknown2", | ||
); | ||
|
||
|
||
// implement method | ||
$class = new Nette\PhpGenerator\ClassType('Test'); | ||
$class->setExtends(Foo::class); | ||
$method = $class->inheritMethod('bar'); | ||
Assert::match(<<<'XX' | ||
public function bar(int $a, ...$b): void | ||
{ | ||
} | ||
|
||
XX, (string) $method); | ||
|
||
Assert::same($method, $class->inheritMethod('bar', returnIfExists: true)); | ||
Assert::exception( | ||
fn() => $class->inheritMethod('bar', returnIfExists: false), | ||
Nette\InvalidStateException::class, | ||
"Cannot inherit method 'bar', because it already exists.", | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
class Foo | ||
{ | ||
public array $bar = [123]; | ||
} | ||
|
||
|
||
// missing parent | ||
$class = new Nette\PhpGenerator\ClassType('Test'); | ||
Assert::exception( | ||
fn() => $class->inheritProperty('bar'), | ||
Nette\InvalidStateException::class, | ||
"Class 'Test' has not setExtends() set.", | ||
); | ||
|
||
$class->setExtends('Unknown'); | ||
Assert::exception( | ||
fn() => $class->inheritProperty('bar'), | ||
Nette\InvalidStateException::class, | ||
"Property 'bar' has not been found in ancestor Unknown", | ||
); | ||
|
||
|
||
// implement property | ||
$class = new Nette\PhpGenerator\ClassType('Test'); | ||
$class->setExtends(Foo::class); | ||
$prop = $class->inheritProperty('bar'); | ||
Assert::match(<<<'XX' | ||
class Test extends Foo | ||
{ | ||
public array $bar = [123]; | ||
} | ||
|
||
XX, (string) $class); | ||
|
||
Assert::same($prop, $class->inheritProperty('bar', returnIfExists: true)); | ||
Assert::exception( | ||
fn() => $class->inheritProperty('bar', returnIfExists: false), | ||
Nette\InvalidStateException::class, | ||
"Cannot inherit property 'bar', because it already exists.", | ||
); |