-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathObject.property.phpt
More file actions
110 lines (75 loc) · 2.04 KB
/
Object.property.phpt
File metadata and controls
110 lines (75 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* Test: Nette\Object properties.
* @phpVersion < 7.2
*/
declare(strict_types=1);
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
class TestClass extends Nette\Object
{
public $declared;
private $foo;
private $bar;
public function __construct($foo = null, $bar = null)
{
$this->foo = $foo;
$this->bar = $bar;
}
public function foo()
{ // method getter has lower priority than getter
}
public function getFoo()
{
return $this->foo;
}
public function setFoo($foo)
{
$this->foo = $foo;
}
public function getBar()
{
return $this->bar;
}
public function setBazz($value)
{
$this->bar = $value;
}
public function gets() // or setupXyz, settle...
{
echo __METHOD__;
return 'ERROR';
}
}
$obj = new TestClass;
$obj->foo = 'hello';
Assert::same('hello', $obj->foo);
Assert::same('hello', $obj->Foo);
$obj->foo .= ' world';
Assert::same('hello world', $obj->foo);
// Undeclared property writing
Assert::exception(function () use ($obj) {
$obj->undeclared = 'value';
}, Nette\MemberAccessException::class, 'Cannot write to an undeclared property TestClass::$undeclared, did you mean $declared?');
// Undeclared property reading
Assert::false(isset($obj->S));
Assert::false(isset($obj->s));
Assert::false(isset($obj->undeclared));
Assert::exception(function () use ($obj) {
$val = $obj->undeclared;
}, Nette\MemberAccessException::class, 'Cannot read an undeclared property TestClass::$undeclared, did you mean $declared?');
// Read-only property
$obj = new TestClass('Hello', 'World');
Assert::true(isset($obj->bar));
Assert::same('World', $obj->bar);
Assert::exception(function () use ($obj) {
$obj->bar = 'value';
}, Nette\MemberAccessException::class, 'Cannot write to a read-only property TestClass::$bar.');
// write-only property
$obj = new TestClass;
Assert::false(isset($obj->bazz));
$obj->bazz = 'World';
Assert::same('World', $obj->bar);
Assert::exception(function () use ($obj) {
$val = $obj->bazz;
}, Nette\MemberAccessException::class, 'Cannot read a write-only property TestClass::$bazz.');