|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: PropertyLike visibility |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Nette\PhpGenerator\ClassType; |
| 10 | +use Tester\Assert; |
| 11 | + |
| 12 | +require __DIR__ . '/../bootstrap.php'; |
| 13 | + |
| 14 | + |
| 15 | +$class = new ClassType('Demo'); |
| 16 | + |
| 17 | +// Default visibility (public) |
| 18 | +$default = $class->addProperty('first') |
| 19 | + ->setType('string'); |
| 20 | +Assert::true($default->isPublic()); |
| 21 | +Assert::false($default->isProtected()); |
| 22 | +Assert::false($default->isPrivate()); |
| 23 | +Assert::null($default->getVisibility()); |
| 24 | + |
| 25 | +// Explicit public |
| 26 | +$public = $class->addProperty('second') |
| 27 | + ->setType('string') |
| 28 | + ->setPublic(); |
| 29 | +Assert::true($public->isPublic()); |
| 30 | +Assert::false($public->isProtected()); |
| 31 | +Assert::false($public->isPrivate()); |
| 32 | +Assert::same('public', $public->getVisibility()); |
| 33 | + |
| 34 | +// Protected |
| 35 | +$protected = $class->addProperty('third') |
| 36 | + ->setType('string') |
| 37 | + ->setProtected(); |
| 38 | +Assert::false($protected->isPublic()); |
| 39 | +Assert::true($protected->isProtected()); |
| 40 | +Assert::false($protected->isPrivate()); |
| 41 | +Assert::same('protected', $protected->getVisibility()); |
| 42 | + |
| 43 | +// Private |
| 44 | +$private = $class->addProperty('fourth') |
| 45 | + ->setType('string') |
| 46 | + ->setPrivate(); |
| 47 | +Assert::false($private->isPublic()); |
| 48 | +Assert::false($private->isProtected()); |
| 49 | +Assert::true($private->isPrivate()); |
| 50 | +Assert::same('private', $private->getVisibility()); |
| 51 | + |
| 52 | +// Change visibility |
| 53 | +$changing = $class->addProperty('fifth') |
| 54 | + ->setType('string') |
| 55 | + ->setPublic(); |
| 56 | +$changing->setVisibility('protected'); |
| 57 | +Assert::false($changing->isPublic()); |
| 58 | +Assert::true($changing->isProtected()); |
| 59 | +Assert::false($changing->isPrivate()); |
| 60 | + |
| 61 | +// Test invalid visibility |
| 62 | +Assert::exception( |
| 63 | + fn() => $changing->setVisibility('invalid'), |
| 64 | + Nette\InvalidArgumentException::class, |
| 65 | + 'Argument must be public|protected|private.', |
| 66 | +); |
| 67 | + |
| 68 | +same(<<<'XX' |
| 69 | + class Demo |
| 70 | + { |
| 71 | + public string $first; |
| 72 | + public string $second; |
| 73 | + protected string $third; |
| 74 | + private string $fourth; |
| 75 | + protected string $fifth; |
| 76 | + } |
| 77 | + |
| 78 | + XX, (string) $class); |
0 commit comments