EntryTest::testObjectHasChanges()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace Fwk\Db\Registry;
4
use MyProject\Proxies\__CG__\stdClass;
5
6
7
/**
8
 * Test class for Registry Entry.
9
 */
10
class EntryTest extends \PHPUnit_Framework_TestCase
11
{
12
    protected $object;
13
14
    protected function tearDown()
15
    {
16
        unset($this->object);
17
    }
18
19
    public function testEntryDataMethods()
20
    {
21
        $objArr = array(
22
            'id'    => 1,
23
            'test'  => 'testValue',
24
            'again' => 'anotherValue',
25
            'prop'  => 'propValue'
26
        );
27
28
        $obj = (object)$objArr;
29
        $this->object = new Entry($obj, array('id' => 1), RegistryState::REGISTERED, array(
30
           'constructorData'    => true
31
        ));
32
33
        $this->assertEquals(true, $this->object->data('constructorData'));
34
        $this->assertEquals(true, $this->object['constructorData']);
35
        $this->object->mergeData(array('constructorData' => 'overriden'));
36
        $this->assertEquals('overriden', $this->object->data('constructorData'));
37
        $this->assertFalse($this->object->data('myTest'));
38
        $this->object['myTest'] = 'testing';
39
        $this->assertEquals('testing', $this->object->data('myTest'));
40
        unset($this->object['myTest']);
41
        $this->assertFalse($this->object->data('myTest'));
42
        $this->assertNull($this->object->data('myTest', null));
43
    }
44
45
    public function testConstructorInvalidArgument()
46
    {
47
        $this->setExpectedException('InvalidArgumentException');
48
        $this->object = new Entry(array('id' => 1), array('id' => 1), RegistryState::REGISTERED, array(
49
            'constructorData'    => true
50
        ));
51
    }
52
53
    public function testStateChanges()
54
    {
55
        $objArr = array(
56
            'id'    => 1,
57
            'test'  => 'testValue',
58
            'again' => 'anotherValue',
59
            'prop'  => 'propValue'
60
        );
61
62
        $obj = (object)$objArr;
63
        $this->object = new Entry($obj, array('id' => 1));
64
65
        $this->assertTrue($this->object->isState(RegistryState::UNKNOWN));
66
        $this->object->fresh();
67
        $this->assertTrue($this->object->isState(RegistryState::FRESH));
68
    }
69
70
    public function testObjectHasChanges()
71
    {
72
        $objArr = array(
73
            'id'    => 1,
74
            'test'  => 'testValue',
75
            'again' => 'anotherValue',
76
            'prop'  => 'propValue'
77
        );
78
79
        $obj = (object)$objArr;
80
        $this->object = new Entry($obj, array('id' => 1), RegistryState::FRESH);
81
82
        $this->assertFalse($this->object->hasChanged());
83
        $obj->test = 'changedValue';
84
        $this->assertTrue($this->object->hasChanged());
85
        $this->assertEquals($obj, $this->object->getObject());
86
        $this->assertArrayHasKey('test', $this->object->getChangedValues());
87
    }
88
89
    public function testMatching()
90
    {
91
        $objArr = array(
92
            'id'    => 1,
93
            'test'  => 'testValue',
94
            'again' => 'anotherValue',
95
            'prop'  => 'propValue'
96
        );
97
98
        $obj = (object)$objArr;
99
        $this->object = new Entry($obj, array('id' => 1), RegistryState::FRESH);
100
101
        $this->assertFalse($this->object->match(array('id' => 2)));
102
        $this->assertFalse($this->object->match(array('id' => 1), "App\\Model\\Test")); // id = ok, class = ko
103
        $this->assertFalse($this->object->match(array('id' => 1, 'otherId' => 'nope'), "stdClass")); // id = ko, class = ok
104
        $this->assertTrue($this->object->match(array('id' => 1)));
105
        $this->assertTrue($this->object->match(array('id' => 1), 'stdClass'));
106
        $this->assertFalse($this->object->matchObject(new \stdClass()));
107
        $this->assertTrue($this->object->matchObject($obj));
108
    }
109
}