ChainTest.php 5.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace UnicaenAuthTest\Authentication\Storage;

use UnicaenAuth\Authentication\Storage\Chain;

/**
 * Description of ChainTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
12
class ChainTest extends \PHPUnit\Framework\TestCase
13
14
15
16
17
{
    protected $storage;
    protected $innerStorage;
    protected $eventManager;
    protected $event;
18

19
20
21
22
23
24
25
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->storage      = new Chain();
26
27
28
29
        $this->innerStorage = $this->getMock('Zend\Authentication\Storage\Session', ['isEmpty', 'read', 'write', 'clear']);
        $this->eventManager = $this->getMock('Zend\EventManager\EventManager', ['trigger']);
        $this->event        = $this->getMock('UnicaenAuth\Authentication\Storage\ChainEvent', ['getContents']);

30
31
32
33
        $this->storage->setStorage($this->innerStorage)
                ->setEventManager($this->eventManager)
                ->setEvent($this->event);
    }
34

35
36
37
38
39
    public function testCanRetrieveDefaultInnerStorage()
    {
        $storage = new Chain();
        $this->assertInstanceOf('Zend\Authentication\Storage\StorageInterface', $storage->getStorage());
    }
40

41
42
43
44
45
46
    public function testCanSetInnerStorage()
    {
        $storage = new Chain();
        $storage->setStorage($inner = new \Zend\Authentication\Storage\Session());
        $this->assertSame($inner, $storage->getStorage());
    }
47

48
49
50
51
52
    public function testCanRetrieveDefaultEventManager()
    {
        $storage = new Chain();
        $this->assertInstanceOf('Zend\EventManager\EventManagerInterface', $storage->getEventManager());
    }
53

54
55
56
57
58
59
    public function testCanSetEventManager()
    {
        $storage = new Chain();
        $storage->setEventManager($em = new \Zend\EventManager\EventManager());
        $this->assertSame($em, $storage->getEventManager());
    }
60

61
62
63
64
65
    public function testCanRetrieveDefaultEvent()
    {
        $storage = new Chain();
        $this->assertInstanceOf('UnicaenAuth\Authentication\Storage\ChainEvent', $storage->getEvent());
    }
66

67
68
69
70
71
72
    public function testCanSetEvent()
    {
        $storage = new Chain();
        $storage->setEvent($e = new \UnicaenAuth\Authentication\Storage\ChainEvent());
        $this->assertSame($e, $storage->getEvent());
    }
73

74
75
76
77
78
79
80
81
82
    public function testCanGetEmptiness()
    {
        $this->eventManager->expects($this->never())
                ->method('trigger');
        $this->innerStorage->expects($this->once())
                ->method('isEmpty')
                ->will($this->returnValue('result'));
        $this->assertEquals('result', $this->storage->isEmpty());
    }
83

84
85
86
87
88
89
90
91
    public function testCanWrite()
    {
        $this->innerStorage->expects($this->once())
                ->method('write')
                ->with(12);
        $this->eventManager->expects($this->once())
                ->method('trigger')
                ->with('write', $this->logicalAnd(
92
93
                        $this->isInstanceOf('UnicaenAuth\Authentication\Storage\ChainEvent'),
                        $this->attributeEqualTo('params', ['contents' => 12])));
94
95
        $this->storage->write(12);
    }
96

97
98
99
100
101
102
103
    public function testReadingReturnsCollectedIdentities()
    {
        $this->eventManager->expects($this->once())
                ->method('trigger')
                ->with('read', $this->isInstanceOf('UnicaenAuth\Authentication\Storage\ChainEvent'));
        $this->event->expects($this->once())
                ->method('getContents')
104
                ->will($this->returnValue($expected = ['db' => 'db identity', 'ldap' => 'ldap identity']));
105
106
        $this->assertEquals($expected, $this->storage->read());
    }
107

108
109
110
111
112
113
114
115
116
117
    /**
     * @depends testReadingReturnsCollectedIdentities
     */
    public function testReadingDoesNotCollectIdentitiesTwice()
    {
        $this->eventManager->expects($this->once())
                ->method('trigger')
                ->with('read', $this->isInstanceOf('UnicaenAuth\Authentication\Storage\ChainEvent'));
        $this->event->expects($this->once())
                ->method('getContents')
118
                ->will($this->returnValue($expected = ['db' => 'db identity', 'ldap' => 'ldap identity']));
119
        $this->assertEquals($expected, $this->storage->read());
120

121
122
123
124
125
126
        $this->eventManager->expects($this->never())
                ->method('trigger');
        $this->event->expects($this->never())
                ->method('getContents');
        $this->assertEquals($expected, $this->storage->read());
    }
127

128
129
    public function getEmptyEventContents()
    {
130
131
132
133
        return [
            [[]],
            [null],
        ];
134
    }
135

136
137
138
139
140
141
142
143
144
145
146
147
148
    /**
     * @dataProvider getEmptyEventContents
     */
    public function testReadingReturnsNullIfNoIdentitiesCollected($contents)
    {
        $this->eventManager->expects($this->once())
                ->method('trigger')
                ->with('read', $this->isInstanceOf('UnicaenAuth\Authentication\Storage\ChainEvent'));
        $this->event->expects($this->once())
                ->method('getContents')
                ->will($this->returnValue($contents));
        $this->assertNull($this->storage->read());
    }
149

150
151
152
153
154
155
156
157
158
159
160
161
162
    /**
     * @depends testReadingReturnsCollectedIdentities
     */
    public function testCanClear()
    {
        $this->innerStorage->expects($this->once())
                ->method('clear');
        $this->eventManager->expects($this->once())
                ->method('trigger')
                ->with('clear', $this->isInstanceOf('UnicaenAuth\Authentication\Storage\ChainEvent'));
        $this->storage->clear();
    }
}