RegistryTest::testGetState()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Fwk\Db\Registry;
4
use Fwk\Db\Connection;
5
use Fwk\Db\Events\FreshEvent;
6
use Fwk\Db\Table;
7
8
/**
9
 * Test class for Registry.
10
 * Generated by PHPUnit on 2011-05-25 at 10:33:22.
11
 */
12
class RegistryTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var Registry
16
     */
17
    protected $object;
18
19
    /**
20
     * Sets up the fixture, for example, opens a network connection.
21
     * This method is called before a test is executed.
22
     */
23
    protected function setUp()
24
    {
25
        $this->object = new Registry('test');
26
    }
27
28
    /**
29
     */
30
    public function testStore()
31
    {
32
        $obj = new \stdClass();
33
        $this->assertEquals(null, $this->object->get(array('keyOne' => 1, 'keyTwo' => 2)));
34
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2));
35
        $this->assertEquals($obj, $this->object->get(array('keyOne' => 1, 'keyTwo' => 2)));
36
    }
37
38
    public function testStoreWithListener()
39
    {
40
        $obj = new \stdClass();
41
        $obj->listen = false;
42
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2), RegistryState::REGISTERED, array('listeners' => array('fresh' => function(FreshEvent $event) {
43
            $event->getEntity()->listen = true;
44
        })));
45
        $this->assertFalse($obj->listen);
46
        $this->object->getEventDispatcher($obj)->notify(new FreshEvent(new Connection(), new Table('test'), $obj));
47
        $this->assertTrue($obj->listen);
48
    }
49
50
    /**
51
     */
52
    public function testGet()
53
    {
54
        $obj   = new \stdClass();
55
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2));
56
        $this->assertEquals($obj, $this->object->get(array('keyOne' => 1, 'keyTwo' => 2)));
57
    }
58
59
    /**
60
     */
61
    public function testRemove()
62
    {
63
        $obj   = new \stdClass();
64
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2));
65
        $this->assertEquals($obj, $this->object->get(array('keyOne' => 1, 'keyTwo' => 2)));
66
67
        $this->object->remove($obj);
68
        $this->assertEquals(null, $this->object->get(array('keyOne' => 1, 'keyTwo' => 2)));
69
    }
70
71
    /**
72
     */
73
    public function testRemoveByIdentifiers()
74
    {
75
        $obj   = new \stdClass();
76
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2));
77
        $this->assertEquals($obj, $this->object->get(array('keyOne' => 1, 'keyTwo' => 2)));
78
79
        $this->object->removeByIdentifiers(array('keyOne' => 1, 'keyTwo' => 2));
80
        $this->assertEquals(null, $this->object->get(array('keyOne' => 1, 'keyTwo' => 2)));
81
    }
82
83
    public function testCount()
84
    {
85
        $obj = new \stdClass();
86
        $this->assertEquals(0, $this->object->count());
87
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2));
88
        $this->assertEquals(1, $this->object->count());
89
    }
90
91
92
    public function testRemoveUnknownEntity()
93
    {
94
        $this->setExpectedException('Fwk\Db\Exceptions\UnregisteredEntityException');
95
        $this->object->remove(new \stdClass());
96
    }
97
98
    public function testChangedValuesUnknownEntity()
99
    {
100
        $this->setExpectedException('Fwk\Db\Exceptions\UnregisteredEntityException');
101
        $this->object->getChangedValues(new \stdClass());
102
    }
103
104
    public function testGetTableName()
105
    {
106
        $this->assertEquals('test', $this->object->getTableName());
107
    }
108
109
    public function testGetState()
110
    {
111
        $obj   = new \stdClass();
112
        $this->assertEquals(RegistryState::UNREGISTERED, $this->object->getState($obj));
113
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2));
114
        $this->assertEquals(RegistryState::UNKNOWN, $this->object->getState($obj));
115
    }
116
117
    public function testIsChanged()
118
    {
119
        $obj   = new \stdClass();
120
        $obj->coucou    = "pwet";
121
        $obj->test      = "pfff";
122
123
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2));
124
        $this->object->defineInitialValues($obj, null, null);
125
        $this->assertEquals(RegistryState::FRESH, $this->object->getState($obj));
126
127
        $this->assertFalse($this->object->getEntry($obj)->hasChanged());
128
129
        $obj->coucou    = "coucou";
130
        $this->assertTrue($this->object->getEntry($obj)->hasChanged());
131
        $this->assertEquals(RegistryState::CHANGED, $this->object->getState($obj));
132
    }
133
134
    public function testToArray()
135
    {
136
        $obj   = new \stdClass();
137
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2));
138
        $this->assertEquals(array($obj), $this->object->toArray());
139
    }
140
141
    public function testGetIterator()
142
    {
143
        $this->assertInstanceOf('\ArrayIterator', $this->object->getIterator());
144
    }
145
146
    public function testGetEventDispatcher()
147
    {
148
        $obj   = new \stdClass();
149
        $this->object->store($obj, array('keyOne' => 1, 'keyTwo' => 2));
150
        $this->assertInstanceOf('\Fwk\Events\Dispatcher', $this->object->getEventDispatcher($obj));
151
    }
152
153
    public function testGetEventDispatcherFail()
154
    {
155
        $obj   = new \stdClass();
156
        $this->setExpectedException('\Fwk\Db\Exceptions\UnregisteredEntityException');
157
        $this->object->getEventDispatcher($obj);
158
    }
159
160
    public function testUnknownAction()
161
    {
162
        $obj = new \stdClass;
163
        $this->object->store($obj);
164
        $this->object->markForAction($obj, "UnknownAction");
165
166
        $this->setExpectedException('\Fwk\Db\Exception');
167
        $this->object->getWorkersQueue();
168
    }
169
170
    public function testStoreAlreadyStored()
171
    {
172
        $obj = new \stdClass();
173
        $this->object->store($obj);
174
        $this->assertTrue($this->object->contains($obj));
175
        $this->object->store($obj);
176
    }
177
}
178