Completed
Push — 1.x ( 770e50...c04239 )
by Théo
02:28 queued 01:12
created

test_it_can_retrieve_an_object_property()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 9.4285
1
<?php
2
3
namespace DeepCopyTest\Reflection;
4
5
use DeepCopy\Reflection\ReflectionHelper;
6
use PHPUnit_Framework_TestCase;
7
use ReflectionClass;
8
use ReflectionProperty;
9
10
/**
11
 * @covers \DeepCopy\Reflection\ReflectionHelper
12
 */
13
class ReflectionHelperTest extends PHPUnit_Framework_TestCase
14
{
15
    public function test_it_retrieves_the_object_properties()
16
    {
17
        $child = new ReflectionHelperTestChild();
18
        $childReflectionClass = new ReflectionClass($child);
19
20
        $expectedProps = array(
21
            'a1',
22
            'a2',
23
            'a3',
24
            'a10',
25
            'a11',
26
            'a12',
27
            'a20',
28
            'a21',
29
            'a22',
30
            'a100',
31
            'a101',
32
            'a102',
33
        );
34
35
        $actualProps = ReflectionHelper::getProperties($childReflectionClass);
36
37
        $this->assertSame($expectedProps, array_keys($actualProps));
38
    }
39
40
    /**
41
     * @dataProvider provideProperties
42
     */
43
    public function test_it_can_retrieve_an_object_property($name)
44
    {
45
        $object = new ReflectionHelperTestChild();
46
47
        $property = ReflectionHelper::getProperty($object, $name);
48
49
        $this->assertInstanceOf(ReflectionProperty::class, $property);
50
51
        $this->assertSame($name, $property->getName());
52
    }
53
54
    public function provideProperties()
55
    {
56
        return [
57
            'public property' => ['a10'],
58
            'private property' => ['a102'],
59
            'private property of ancestor' => ['a3']
60
        ];
61
    }
62
63
    /**
64
     * @expectedException \DeepCopy\Exception\PropertyException
65
     */
66
    public function test_it_cannot_retrieve_a_non_existent_prperty()
67
    {
68
        $object = new ReflectionHelperTestChild();
69
70
        ReflectionHelper::getProperty($object, 'non existent property');
71
    }
72
}
73
74
class ReflectionHelperTestParent
75
{
76
    public $a1;
77
    protected $a2;
78
    private $a3;
0 ignored issues
show
Unused Code introduced by Filippo Tessarotto
The property $a3 is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
79
80
    public $a10;
81
    protected $a11;
82
    private $a12;
0 ignored issues
show
Unused Code introduced by Théo FIDRY
The property $a12 is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
83
84
    public static $a20;
85
    protected static $a21;
86
    private static $a22;
0 ignored issues
show
Unused Code introduced by Théo FIDRY
The property $a22 is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
87
}
88
89
class ReflectionHelperTestChild extends ReflectionHelperTestParent
90
{
91
    public $a1;
92
    protected $a2;
93
    private $a3;
0 ignored issues
show
Comprehensibility introduced by Filippo Tessarotto
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by Filippo Tessarotto
The property $a3 is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
94
95
    public $a100;
96
    protected $a101;
97
    private $a102;
0 ignored issues
show
Unused Code introduced by Théo FIDRY
The property $a102 is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
98
}
99