-
-
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.
- Loading branch information
Showing
3 changed files
with
158 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Property 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()); | ||
Assert::true($default->isPublic(write: true)); | ||
Assert::null($default->getVisibility()); | ||
Assert::null($default->getVisibility(write: true)); | ||
|
||
// Public with private setter | ||
$restricted = $class->addProperty('second') | ||
->setType('string') | ||
->setVisibility(null, 'private'); | ||
Assert::true($restricted->isPublic()); | ||
Assert::false($restricted->isPublic(write: true)); | ||
Assert::true($restricted->isPrivate(write: true)); | ||
Assert::null($restricted->getVisibility()); | ||
Assert::same('private', $restricted->getVisibility(write: true)); | ||
|
||
// Public with protected setter using individual methods | ||
$mixed = $class->addProperty('third') | ||
->setType('string') | ||
->setPublic() | ||
->setProtected(write: true); | ||
Assert::true($mixed->isPublic()); | ||
Assert::false($mixed->isPublic(write: true)); | ||
Assert::true($mixed->isProtected(write: true)); | ||
Assert::same('public', $mixed->getVisibility()); | ||
Assert::same('protected', $mixed->getVisibility(write: true)); | ||
|
||
// Protected with private setter | ||
$nested = $class->addProperty('fourth') | ||
->setType('string') | ||
->setProtected() | ||
->setPrivate(write: true); | ||
Assert::false($nested->isPublic()); | ||
Assert::true($nested->isProtected()); | ||
Assert::true($nested->isPrivate(write: true)); | ||
Assert::same('protected', $nested->getVisibility()); | ||
Assert::same('private', $nested->getVisibility(write: true)); | ||
|
||
// 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); |