-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for asymmetric visibility
- Loading branch information
Showing
4 changed files
with
205 additions
and
4 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
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,81 @@ | ||
<?php | ||
|
||
/** | ||
* Test: PropertyLike asymmetric visibility | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\PhpGenerator\ClassType; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
$class = new ClassType('Demo'); | ||
|
||
// Default visibility | ||
$default = $class->addProperty('first') | ||
->setType('string'); | ||
Assert::true($default->isPublic('get')); | ||
Assert::true($default->isPublic('set')); | ||
Assert::null($default->getVisibility()); | ||
Assert::null($default->getVisibility('set')); | ||
|
||
// Public with private setter | ||
$restricted = $class->addProperty('second') | ||
->setType('string') | ||
->setVisibility(null, 'private'); | ||
Assert::true($restricted->isPublic()); | ||
Assert::false($restricted->isPublic('set')); | ||
Assert::true($restricted->isPrivate('set')); | ||
Assert::null($restricted->getVisibility()); | ||
Assert::same('private', $restricted->getVisibility('set')); | ||
|
||
// Public with protected setter using individual methods | ||
$mixed = $class->addProperty('third') | ||
->setType('string') | ||
->setPublic() | ||
->setProtected('set'); | ||
Assert::true($mixed->isPublic()); | ||
Assert::false($mixed->isPublic('set')); | ||
Assert::true($mixed->isProtected('set')); | ||
Assert::same('public', $mixed->getVisibility()); | ||
Assert::same('protected', $mixed->getVisibility('set')); | ||
|
||
// Protected with private setter | ||
$nested = $class->addProperty('fourth') | ||
->setType('string') | ||
->setProtected() | ||
->setPrivate('set'); | ||
Assert::false($nested->isPublic()); | ||
Assert::true($nested->isProtected()); | ||
Assert::true($nested->isPrivate('set')); | ||
Assert::same('protected', $nested->getVisibility()); | ||
Assert::same('private', $nested->getVisibility('set')); | ||
|
||
// Test invalid getter visibility | ||
Assert::exception( | ||
fn() => $default->setVisibility('invalid', 'public'), | ||
Nette\InvalidArgumentException::class, | ||
'Argument must be public|protected|private.', | ||
); | ||
|
||
// Test invalid setter visibility | ||
Assert::exception( | ||
fn() => $default->setVisibility('public', 'invalid'), | ||
Nette\InvalidArgumentException::class, | ||
'Argument must be public|protected|private.', | ||
); | ||
|
||
|
||
same(<<<'XX' | ||
class Demo | ||
{ | ||
public string $first; | ||
private(set) string $second; | ||
public protected(set) string $third; | ||
protected private(set) string $fourth; | ||
} | ||
|
||
XX, (string) $class); |