AATestObj   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
cbo 0
dl 0
loc 8
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
1
<?php
2
3
namespace Fwk\Db;
4
5
class TestObj
6
{
7
    public $test;
8
9
    protected $protected;
10
11
    private $private;
12
13
    private $privWithoutGetter;
14
15
    private $privWithoutSetter;
0 ignored issues
show
Unused Code introduced by
The property $privWithoutSetter 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...
16
17
    public function getProtected()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
18
    {
19
        return $this->protected;
20
    }
21
22
    public function setProtected($protected)
23
    {
24
        $this->protected = $protected;
25
    }
26
27
    public function getPrivate()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
28
    {
29
        return $this->private;
30
    }
31
32
    public function setPrivate($private)
33
    {
34
        $this->private = $private;
35
    }
36
37
    public function setPrivWithoutGetter($privWithoutGetter)
38
    {
39
        $this->privWithoutGetter = $privWithoutGetter;
40
    }
41
}
42
43
class AATestObj extends \ArrayObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
44
{
45
    public function __construct($array)
46
    {
47
        parent::__construct($array);
48
        $this->setFlags(\ArrayObject::ARRAY_AS_PROPS);
49
    }
50
}
51
52
/**
53
 * Test class for Accessor.
54
 * Generated by PHPUnit on 2012-05-27 at 17:46:42.
55
 */
56
class AccessorTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
57
{
58
    /**
59
     * @var Accessor
60
     */
61
    protected $object;
62
63
    /**
64
     * @var TestObj
65
     */
66
    protected $testable;
67
68
    /**
69
     * Sets up the fixture, for example, opens a network connection.
70
     * This method is called before a test is executed.
71
     */
72
    protected function setUp()
73
    {
74
        $this->testable = new TestObj();
75
        $this->testable->test = "testing";
76
        $this->testable->setProtected("protectedValue");
77
        $this->testable->setPrivate("privateValue");
78
        $this->testable->setPrivWithoutGetter("noGetter");
79
80
        $this->object = new Accessor($this->testable);
81
    }
82
83
    protected function tearDown()
84
    {
85
        unset($this->object);
86
    }
87
88
    /**
89
     */
90
    public function testGetAA()
91
    {
92
        $aaObj = new AATestObj(array(
93
            'set' => true,
94
            'test' => 'coucou'
95
        ));
96
        $this->object = new Accessor($aaObj);
97
98
        $this->assertEquals(true, $this->object->get('set'));
99
        $this->assertEquals("coucou", $this->object->get('test'));
100
    }
101
102
    /**
103
     */
104
    public function testSetAA()
105
    {
106
        $aaObj = new AATestObj(array(
107
            'set' => true,
108
            'test' => 'coucou'
109
        ));
110
        $this->object = new Accessor($aaObj);
111
        $this->assertTrue($this->object->set('testing', 'ok'));
112
    }
113
114
    /**
115
     */
116
    public function testGet()
117
    {
118
        $this->assertEquals("testing", $this->object->get('test'));
119
        $this->assertEquals("privateValue", $this->object->get('private'));
120
        $this->assertEquals("protectedValue", $this->object->get('protected'));
121
122
        $this->assertFalse($this->object->get('privWithoutGetter'));
123
124
        // test with override should fetch the value
125
        $this->object->overrideVisibility(true);
126
        $this->assertEquals("noGetter", $this->object->get('privWithoutGetter'));
127
    }
128
129
    /**
130
     */
131
    public function testSet()
132
    {
133
        $this->assertFalse($this->object->set('testing', 'nonExistant'));
134
        $this->assertTrue($this->object->set('test', "testeuh"));
135
        $this->assertTrue($this->object->set('private', 'priv'));
136
        $this->assertTrue($this->object->set('protected', 'prot'));
137
        $this->assertFalse($this->object->set('privWithoutSetter', 'shouldFail'));
138
139
        // test with override should set the value
140
        $this->object->overrideVisibility(true);
141
        $this->assertTrue($this->object->set('privWithoutSetter', "newValue"));
142
    }
143
144
    /**
145
     */
146
    public function testGetReflector()
147
    {
148
        $this->assertInstanceOf('\ReflectionObject', $this->object->getReflector());
149
    }
150
151
    /**
152
     */
153
    public function testToArrayAndSetValues()
154
    {
155
        $values = array(
156
            'test'  => "testeuh",
157
            "private"   => "priv",
158
            "protected" => "prot",
159
            "privWithoutGetter" => "coucou"
160
        );
161
162
        $this->object->setValues($values);
163
164
        $result = array(
165
            "test"  => "testeuh",
166
            "private" =>  "priv",
167
            "protected" => "prot",
168
            "privWithoutGetter" => false,
169
            "privWithoutSetter" => false
170
        );
171
172
        $this->assertEquals($result, $this->object->toArray());
173
174
        $this->object->overrideVisibility(true);
175
        $result['privWithoutGetter'] = "coucou";
176
177
        $this->assertEquals($result, $this->object->toArray());
178
    }
179
180
    /**
181
     */
182
    public function testHashCode()
183
    {
184
        $obj = new \stdClass;
185
        $obj->test = "pwet";
186
187
        $firstHash = $this->object->hashCode();
188
        $this->assertEquals($firstHash,$this->object->hashCode());
189
        $values = array(
190
            'test'  => "testeuh",
191
            "private"   => array('coucou' => 'pwet'),
192
            "protected" => "prot",
193
            "privWithoutGetter" => $obj
194
        );
195
196
        $this->object->setValues($values);
197
        $this->assertNotEquals($firstHash,$this->object->hashCode());
198
    }
199
200
    /**
201
     */
202
    public function testFactory()
203
    {
204
        $this->assertEquals($this->object, Accessor::factory($this->testable));
205
        $this->setExpectedException('\InvalidArgumentException');
206
        Accessor::factory('stringArgumentIsInvalid');
207
    }
208
}
209