DbTest.php 6.62 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

namespace ZfcUserTest\Authentication\Storage;

use ZfcUser\Authentication\Storage\Db;

class DbTest extends \PHPUnit_Framework_TestCase
{
    /**
     * The object to be tested.
     *
     * @var Db
     */
    protected $db;

    /**
     * Mock of Storage.
     *
     * @var storage
     */
    protected $storage;

    /**
     * Mock of Mapper.
     *
     * @var mapper
     */
    protected $mapper;

    public function setUp()
    {
        $db = new Db;
        $this->db = $db;

Bertrand Gauthier's avatar
Bertrand Gauthier committed
35
        $this->storage = $this->getMock('Laminas\Authentication\Storage\Session');
Bertrand Gauthier's avatar
Bertrand Gauthier committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        $this->mapper = $this->getMock('ZfcUser\Mapper\User');
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::isEmpty
     */
    public function testIsEmpty()
    {
        $this->storage->expects($this->once())
                      ->method('isEmpty')
                      ->will($this->returnValue(true));

        $this->db->setStorage($this->storage);

        $this->assertTrue($this->db->isEmpty());
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::read
     */
    public function testReadWithResolvedEntitySet()
    {
        $reflectionClass = new \ReflectionClass('ZfcUser\Authentication\Storage\Db');
        $reflectionProperty = $reflectionClass->getProperty('resolvedIdentity');
        $reflectionProperty->setAccessible(true);
        $reflectionProperty->setValue($this->db, 'zfcUser');

        $this->assertSame('zfcUser', $this->db->read());
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::read
     */
    public function testReadWithoutResolvedEntitySetIdentityIntUserFound()
    {
        $this->storage->expects($this->once())
                      ->method('read')
                      ->will($this->returnValue(1));

        $this->db->setStorage($this->storage);

        $user = $this->getMock('ZfcUser\Entity\User');
        $user->setUsername('zfcUser');

        $this->mapper->expects($this->once())
                     ->method('findById')
                     ->with(1)
                     ->will($this->returnValue($user));

        $this->db->setMapper($this->mapper);

        $result = $this->db->read();

        $this->assertSame($user, $result);
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::read
     */
    public function testReadWithoutResolvedEntitySetIdentityIntUserNotFound()
    {
        $this->storage->expects($this->once())
                      ->method('read')
                      ->will($this->returnValue(1));

        $this->db->setStorage($this->storage);

        $this->mapper->expects($this->once())
                     ->method('findById')
                     ->with(1)
                     ->will($this->returnValue(false));

        $this->db->setMapper($this->mapper);

        $result = $this->db->read();

        $this->assertNull($result);
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::read
     */
    public function testReadWithoutResolvedEntitySetIdentityObject()
    {
        $user = $this->getMock('ZfcUser\Entity\User');
        $user->setUsername('zfcUser');

        $this->storage->expects($this->once())
                      ->method('read')
                      ->will($this->returnValue($user));

        $this->db->setStorage($this->storage);

        $result = $this->db->read();

        $this->assertSame($user, $result);
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::write
     */
    public function testWrite()
    {
        $reflectionClass = new \ReflectionClass('ZfcUser\Authentication\Storage\Db');
        $reflectionProperty = $reflectionClass->getProperty('resolvedIdentity');
        $reflectionProperty->setAccessible(true);

        $this->storage->expects($this->once())
                      ->method('write')
                      ->with('zfcUser');

        $this->db->setStorage($this->storage);

        $this->db->write('zfcUser');

        $this->assertNull($reflectionProperty->getValue($this->db));
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::clear
     */
    public function testClear()
    {
        $reflectionClass = new \ReflectionClass('ZfcUser\Authentication\Storage\Db');
        $reflectionProperty = $reflectionClass->getProperty('resolvedIdentity');
        $reflectionProperty->setAccessible(true);

        $this->storage->expects($this->once())
            ->method('clear');

        $this->db->setStorage($this->storage);

        $this->db->clear();

        $this->assertNull($reflectionProperty->getValue($this->db));
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::getMapper
     */
    public function testGetMapperWithNoMapperSet()
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
178
        $sm = $this->getMock('Laminas\ServiceManager\ServiceManager');
Bertrand Gauthier's avatar
Bertrand Gauthier committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
        $sm->expects($this->once())
           ->method('get')
           ->with('zfcuser_user_mapper')
           ->will($this->returnValue($this->mapper));

        $this->db->setServiceManager($sm);

        $this->assertInstanceOf('ZfcUser\Mapper\UserInterface', $this->db->getMapper());
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::setMapper
     * @covers ZfcUser\Authentication\Storage\Db::getMapper
     */
    public function testSetGetMapper()
    {
        $mapper = new \ZfcUser\Mapper\User;
        $mapper->setTableName('zfcUser');

        $this->db->setMapper($mapper);

        $this->assertInstanceOf('ZfcUser\Mapper\User', $this->db->getMapper());
        $this->assertSame('zfcUser', $this->db->getMapper()->getTableName());
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::setServiceManager
     * @covers ZfcUser\Authentication\Storage\Db::getServiceManager
     */
    public function testSetGetServicemanager()
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
210
        $sm = $this->getMock('Laminas\ServiceManager\ServiceManager');
Bertrand Gauthier's avatar
Bertrand Gauthier committed
211
212
213

        $this->db->setServiceManager($sm);

Bertrand Gauthier's avatar
Bertrand Gauthier committed
214
        $this->assertInstanceOf('Laminas\ServiceManager\ServiceLocatorInterface', $this->db->getServiceManager());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
215
216
217
218
219
220
221
222
223
        $this->assertSame($sm, $this->db->getServiceManager());
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::getStorage
     * @covers ZfcUser\Authentication\Storage\Db::setStorage
     */
    public function testGetStorageWithoutStorageSet()
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
224
        $this->assertInstanceOf('Laminas\Authentication\Storage\Session', $this->db->getStorage());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
225
226
227
228
229
230
231
232
    }

    /**
     * @covers ZfcUser\Authentication\Storage\Db::getStorage
     * @covers ZfcUser\Authentication\Storage\Db::setStorage
     */
    public function testSetGetStorage()
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
233
        $storage = new \Laminas\Authentication\Storage\Session('ZfcUserStorage');
Bertrand Gauthier's avatar
Bertrand Gauthier committed
234
235
        $this->db->setStorage($storage);

Bertrand Gauthier's avatar
Bertrand Gauthier committed
236
        $this->assertInstanceOf('Laminas\Authentication\Storage\Session', $this->db->getStorage());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
237
238
    }
}