SmartObject.unsetProperty.phpt 716 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Test: Nette\SmartObject properties.
  4. */
  5. declare(strict_types=1);
  6. use Tester\Assert;
  7. require __DIR__ . '/../bootstrap.php';
  8. class TestClass
  9. {
  10. use Nette\SmartObject;
  11. public $foo;
  12. }
  13. test('', function () {
  14. $obj = new TestClass;
  15. unset($obj->foo);
  16. Assert::false(isset($obj->foo));
  17. // re-set
  18. $obj->foo = 'hello';
  19. Assert::same('hello', $obj->foo);
  20. });
  21. test('double unset', function () {
  22. $obj = new TestClass;
  23. unset($obj->foo, $obj->foo);
  24. });
  25. test('reading of unset property', function () {
  26. Assert::exception(function () {
  27. $obj = new TestClass;
  28. unset($obj->foo);
  29. $val = $obj->foo;
  30. }, Nette\MemberAccessException::class, 'Cannot read an undeclared property TestClass::$foo.');
  31. });