SmartObject.property.phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Test: Nette\SmartObject properties.
  4. */
  5. declare(strict_types=1);
  6. use Tester\Assert;
  7. require __DIR__ . '/../bootstrap.php';
  8. /**
  9. * @property int $foo
  10. * @property int $bar
  11. * @property int $bazz
  12. * @property int $s
  13. * @property-deprecated int $depr
  14. */
  15. class TestClass
  16. {
  17. use Nette\SmartObject;
  18. public $declared;
  19. private $foo;
  20. private $bar;
  21. public function __construct($foo = null, $bar = null)
  22. {
  23. $this->foo = $foo;
  24. $this->bar = $bar;
  25. }
  26. public function foo()
  27. { // method getter has lower priority than getter
  28. }
  29. public function getFoo()
  30. {
  31. return $this->foo;
  32. }
  33. public function setFoo($foo)
  34. {
  35. $this->foo = $foo;
  36. }
  37. protected function getBar()
  38. {
  39. return $this->bar;
  40. }
  41. public function setBazz($value)
  42. {
  43. $this->bar = $value;
  44. }
  45. public function gets() // or setupXyz, settle...
  46. {
  47. echo __METHOD__;
  48. return 'ERROR';
  49. }
  50. public function setDepr($value)
  51. {
  52. }
  53. public function getDepr()
  54. {
  55. }
  56. }
  57. $obj = new TestClass;
  58. $obj->foo = 'hello';
  59. Assert::same('hello', $obj->foo);
  60. $obj->foo .= ' world';
  61. Assert::same('hello world', $obj->foo);
  62. // Undeclared property writing
  63. Assert::exception(
  64. fn() => $obj->undeclared = 'value',
  65. Nette\MemberAccessException::class,
  66. 'Cannot write to an undeclared property TestClass::$undeclared, did you mean $declared?',
  67. );
  68. // Undeclared property reading
  69. Assert::false(isset($obj->S));
  70. Assert::false(isset($obj->s));
  71. Assert::false(isset($obj->undeclared));
  72. Assert::exception(
  73. fn() => $obj->undeclared,
  74. Nette\MemberAccessException::class,
  75. 'Cannot read an undeclared property TestClass::$undeclared, did you mean $declared?',
  76. );
  77. // Read-only property
  78. $obj = new TestClass('Hello', 'World');
  79. Assert::true(isset($obj->bar));
  80. Assert::same('World', $obj->bar);
  81. Assert::exception(
  82. fn() => $obj->bar = 'value',
  83. Nette\MemberAccessException::class,
  84. 'Cannot write to a read-only property TestClass::$bar.',
  85. );
  86. // write-only property
  87. $obj = new TestClass;
  88. Assert::true(isset($obj->bazz));
  89. $obj->bazz = 'World';
  90. Assert::same('World', $obj->bar);
  91. Assert::exception(
  92. fn() => $obj->bazz,
  93. Nette\MemberAccessException::class,
  94. 'Cannot read a write-only property TestClass::$bazz.',
  95. );
  96. // deprecated property
  97. Assert::error(function () {
  98. $obj = new TestClass;
  99. $obj->depr = 10;
  100. }, E_USER_DEPRECATED, 'Property TestClass::$depr is deprecated, use TestClass::setDepr() method in %a%SmartObject.property.phpt on line %d%.');
  101. Assert::error(function () {
  102. $obj = new TestClass;
  103. $val = $obj->depr;
  104. }, E_USER_DEPRECATED, 'Property TestClass::$depr is deprecated, use TestClass::getDepr() method in %a%SmartObject.property.phpt on line %d%.');