DbTest.php 5.43 KB
Newer Older
1
2
3
4
5
6
7
<?php
namespace UnicaenAuthTest\Authentication\Storage;

use PDOException;
use PHPUnit_Framework_TestCase;
use UnicaenAuth\Authentication\Storage\Db;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
8
use Zend\ServiceManager\ServiceManager;
9
use ZfcUser\Entity\User;
10
use UnicaenAuth\Authentication\Storage\ChainEvent;
11
12
13
14
15
16
17
18
19

/**
 * Description of DbTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class DbTest extends PHPUnit_Framework_TestCase
{
    protected $storage;
20
    protected $serviceManager;
21
    protected $mapper;
22
    protected $event;
23

24
25
26
27
28
29
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
30
31
32
        $this->serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', ['get']);
        $this->innerStorage   = $this->getMock('Zend\Authentication\Storage\Session', ['write', 'clear', 'read']);
        $this->mapper         = $this->getMock('ZfcUser\Mapper\User', ['findById', 'findByUsername']);
33
34
35
36
        $this->event          = new ChainEvent();
        $this->storage        = new Db();

        $this->storage->setMapper($this->mapper)
37
                      ->setServiceLocator($this->serviceManager)
38
39
                      ->setStorage($this->innerStorage);
    }
40

41
42
43
44
45
    public function testCanRetrieveDefaultInnerStorage()
    {
        $storage = new Db();
        $this->assertInstanceOf('Zend\Authentication\Storage\StorageInterface', $storage->getStorage());
    }
46

47
48
49
50
51
52
    public function testCanRetrieveMapperFromServiceManager()
    {
        $this->serviceManager->expects($this->once())
                ->method('get')
                ->with('zfcuser_user_mapper')
                ->will($this->returnValue('mapper'));
53

54
55
56
        $this->storage->setMapper(null);
        $this->assertEquals('mapper', $this->storage->getMapper());
    }
57

58
59
60
    public function testCanWrite()
    {
        $this->event->setParam('contents', 12);
61

62
63
64
        $this->innerStorage->expects($this->once())
                     ->method('write')
                     ->with(12);
65

66
        $this->storage->write($this->event);
67
    }
68

69
70
    public function getException()
    {
71
72
73
74
        return [
            [new PDOException()],
            [new ServiceNotFoundException()],
        ];
75
    }
76

77
78
79
80
81
    /**
     * @dataProvider getException
     */
    public function testReadingReturnsNullIfFindByIdThrowsException($exception)
    {
82
83
84
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue(12));
85
86
87
        $this->mapper->expects($this->once())
                     ->method('findById')
                     ->will($this->throwException($exception));
88

89
        $this->storage->read($this->event);
90
        $this->assertEquals(['db' => null], $this->event->getContents());
91
    }
92

93
    public function testReadingReturnsEntityIfUserIsFoundById()
94
95
    {
        $entity = new User();
96

97
98
99
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue(12));
100
101
102
        $this->mapper->expects($this->once())
                     ->method('findById')
                     ->will($this->returnValue($entity));
103

104
        $this->storage->read($this->event);
105
        $this->assertEquals(['db' => $entity], $this->event->getContents());
106
    }
107

108
109
110
111
112
    /**
     * @dataProvider getException
     */
    public function testReadingReturnsNullIfFindByUsernameThrowsException($exception)
    {
113
114
115
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue('username'));
116
117
118
119
120
121
        $this->mapper->expects($this->once())
                     ->method('findById')
                     ->will($this->returnValue(null));
        $this->mapper->expects($this->once())
                     ->method('findByUsername')
                     ->will($this->throwException($exception));
122

123
        $this->storage->read($this->event);
124
        $this->assertEquals(['db' => null], $this->event->getContents());
125
    }
126

127
128
129
    public function testReadingReturnsEntityIfUserFoundByUsername()
    {
        $entity = new User();
130

131
132
133
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue('username'));
134
135
136
137
138
139
        $this->mapper->expects($this->once())
                     ->method('findById')
                     ->will($this->returnValue(null));
        $this->mapper->expects($this->once())
                     ->method('findByUsername')
                     ->will($this->returnValue($entity));
140

141
        $this->storage->read($this->event);
142
        $this->assertEquals(['db' => $entity], $this->event->getContents());
143
    }
144

145
146
147
    public function testReadingReturnsEntityInInnerStorage()
    {
        $entity = new User();
148

149
150
151
152
153
154
155
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue($entity));
        $this->mapper->expects($this->never())
                     ->method('findById');
        $this->mapper->expects($this->never())
                     ->method('findByUsername');
156

157
        $this->storage->read($this->event);
158
        $this->assertEquals(['db' => $entity], $this->event->getContents());
159
    }
160

161
162
163
164
    public function testCanClear()
    {
        $this->innerStorage->expects($this->once())
                     ->method('clear');
165

166
        $this->storage->clear($this->event);
167
168
    }
}