|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the sauls/helpers package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
|
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
|
7
|
|
|
* @copyright 2018 |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Sauls\Component\Helper; |
|
14
|
|
|
|
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
use Sauls\Component\Helper\Exception\ClassPropertyNotSetException; |
|
17
|
|
|
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException; |
|
18
|
|
|
use Sauls\Component\Helper\Stubs\DummyObject; |
|
19
|
|
|
|
|
20
|
|
|
class ObjectTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @test |
|
24
|
|
|
* |
|
25
|
|
|
* @throws Exception\PropertyNotAccessibleException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function should_configure_object_with_given_properties() |
|
28
|
|
|
{ |
|
29
|
|
|
$dummyObject = configure_object(new DummyObject(), [ |
|
30
|
|
|
'property1' => 'vvv', |
|
31
|
|
|
'property2' => 'zzz', |
|
32
|
|
|
'property3' => 'xxx', |
|
33
|
|
|
]); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertEquals('vvv', $dummyObject->getProperty1()); |
|
36
|
|
|
$this->assertEquals('zzz', $dummyObject->property2); |
|
37
|
|
|
$this->assertEquals('xxx', $dummyObject->property3); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @test |
|
42
|
|
|
* @throws PropertyNotAccessibleException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function should_throw_property_not_accessible_exception_when_configuring_object() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->expectException(PropertyNotAccessibleException::class); |
|
47
|
|
|
|
|
48
|
|
|
configure_object(new DummyObject(), [ |
|
49
|
|
|
'secret' => 'Not so secret...', |
|
50
|
|
|
]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @testR |
|
55
|
|
|
* @throws PropertyNotAccessibleException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function should_get_object_property_value() |
|
58
|
|
|
{ |
|
59
|
|
|
$dummyObject = new DummyObject(); |
|
60
|
|
|
$dummyObject->property2 = 'This is test'; |
|
61
|
|
|
$this->assertSame('This is test', get_object_property_value($dummyObject, 'property2')); |
|
62
|
|
|
$this->assertSame('', get_object_property_value($dummyObject, 'property1')); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @test |
|
67
|
|
|
* @throws PropertyNotAccessibleException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function should_throw_property_not_accessible_exception_when_trying_to_get_non_existent_property() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->expectException(PropertyNotAccessibleException::class); |
|
72
|
|
|
$dummyObject = new DummyObject(); |
|
73
|
|
|
get_object_property_value($dummyObject, 'variable1'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @test |
|
78
|
|
|
* @throws Exception\ClassPropertyNotSetException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function should_set_object_property_value() |
|
81
|
|
|
{ |
|
82
|
|
|
$dummyObject = new DummyObject(); |
|
83
|
|
|
|
|
84
|
|
|
set_object_property_value($dummyObject, 'property1', 'Ignoring programming principles!!'); |
|
85
|
|
|
set_object_property_value($dummyObject, 'property2', 'p2'); |
|
86
|
|
|
set_object_property_value($dummyObject, 'property3', 'test'); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertSame('Ignoring programming principles!!', $dummyObject->getProperty1()); |
|
89
|
|
|
$this->assertSame('p2', $dummyObject->property2); |
|
90
|
|
|
$this->assertSame('test', $dummyObject->property3); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @test |
|
95
|
|
|
* @throws ClassPropertyNotSetException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function should_throw_class_property_not_set_exception_when_setting_non_existent_property() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->expectException(ClassPropertyNotSetException::class); |
|
100
|
|
|
|
|
101
|
|
|
$dummyObject = new DummyObject(); |
|
102
|
|
|
|
|
103
|
|
|
set_object_property_value($dummyObject, 'varbiable22', 'Is it real?'); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|