-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObjectPropertyRipperTest.php
More file actions
107 lines (89 loc) · 2.88 KB
/
ObjectPropertyRipperTest.php
File metadata and controls
107 lines (89 loc) · 2.88 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
<?php
/**
* @author Andrew Coulton <andrew@ingenerator.com>
* @licence BSD-3-Clause
*/
namespace test\unit\Ingenerator\PHPUtils\Object;
use Ingenerator\PHPUtils\Object\ObjectPropertyRipper;
use PHPUnit\Framework\TestCase;
use stdClass;
class ObjectPropertyRipperTest extends TestCase
{
public function test_it_rips_properties()
{
$this->assertEquals(
['something_private' => 'precious', 'something_protected' => 'water'],
ObjectPropertyRipper::rip(
new TestRippingClass,
['something_private', 'something_protected']
)
);
}
public function test_it_rips_one_property()
{
$this->assertEquals(
'precious',
ObjectPropertyRipper::ripOne(new TestRippingClass, 'something_private')
);
}
public function test_it_rips_all_properties()
{
$this->assertEquals(
[
'something_private' => 'precious',
'something_protected' => 'water',
'something_else' => 'else',
],
ObjectPropertyRipper::ripAll(new TestRippingClass)
);
}
public function test_it_can_rip_from_stdclass()
{
$c = new stdClass();
$c->data = 'something';
$c->other = 1;
$this->assertSame(
[
'data' => 'something',
'other' => 1,
],
ObjectPropertyRipper::ripAll($c),
);
$this->assertSame('something', ObjectPropertyRipper::ripOne($c, 'data'));
$this->assertSame(['other' => 1], ObjectPropertyRipper::rip($c, ['other']));
}
public function test_it_can_rip_from_class_that_inherits_from_stdclass()
{
// This is an edge case and should be fairly unlikely IRL, but it is valid.
$c = new class extends stdClass {
private string $hidden = 'whatever';
};
$c->data = 'something';
$c->other = 1;
$this->assertSame(
[
'hidden' => 'whatever',
'data' => 'something',
'other' => 1,
],
ObjectPropertyRipper::ripAll($c),
);
$this->assertSame('whatever', ObjectPropertyRipper::ripOne($c, 'hidden'));
$this->assertSame(['hidden' => 'whatever', 'other' => 1], ObjectPropertyRipper::rip($c, ['hidden', 'other']));
}
public function test_it_throws_if_ripping_all_from_an_object_with_private_parent_properties()
{
$this->expectException(\DomainException::class);
ObjectPropertyRipper::ripAll(new ExtensionRippingClass);
}
}
class TestRippingClass
{
private $something_private = 'precious';
protected $something_protected = 'water';
protected $something_else = 'else';
}
class ExtensionRippingClass extends TestRippingClass
{
private $child_private = 'other-private';
}