This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
ChainTest.php
150 lines (125 loc) · 4.24 KB
/
ChainTest.php
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
35
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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Uri
*/
namespace ZendTest\Authentication\Storage;
use Zend\Authentication\Storage\Chain,
Zend\Authentication\Storage\StorageInterface,
Zend\Authentication\Storage\NonPersistent;
use PHPUnit_Framework_TestCase as TestCase;
/**
* @category Zend
* @package Zend_Auth
* @subpackage UnitTests
* @group Zend_Auth
*/
class ChainTest extends TestCase
{
const ID = 1337;
/**
* Ensure chain without storage behavious as empty storage.
*/
public function testEmptyChain()
{
$chain = new Chain;
$this->assertTrue($chain->isEmpty());
}
/**
* Ensure chain with single empty storage behavious as expected.
*/
public function testSingularChainEmpty()
{
$chain = new Chain;
$chain->add($this->storageFactory());
$this->assertTrue($chain->isEmpty());
}
/**
* Ensure chain with single non-empty storage behavious as expected.
*/
public function testSingularChainNonEmpty()
{
$chain = new Chain;
$chain->add($this->storageFactory(self::ID));
$this->assertFalse($chain->isEmpty());
$this->assertEquals(self::ID, $chain->read());
}
/**
* Ensure the priority of storage engines is correctly used.
*/
public function testChainPriority()
{
$storageA = $this->storageFactory();
$storageB = $this->storageFactory(self::ID);
$chain = new Chain;
$chain->add($storageA); // Defaults to 1
$chain->add($storageB, 10);
$chain->isEmpty();
// Storage B has higher priority AND is non-empty. Thus
// storage A should been used at all and remain empty.
$this->assertTrue($storageA->isEmpty());
}
/**
* Ensure that a chain with empty storages is considered empty and
* won't populated any of its underlying storages.
*/
public function testEmptyChainIsEmpty()
{
$emptyStorageA = $this->storageFactory();
$emptyStorageB = $this->storageFactory();
$chain = new Chain;
$chain->add($emptyStorageA);
$chain->add($emptyStorageB);
$this->assertTrue($chain->isEmpty());
// Storage A and B remain empty
$this->assertTrue($emptyStorageA->isEmpty());
$this->assertTrue($emptyStorageB->isEmpty());
}
/**
* Ensure that chain will yield non-empty if one of its underlying storage
* engines is non-empty.
*
* Make sure that storage engines with higher priority then the first non-empty
* storage engine get populated with that same content.
*/
public function testSuccessfullReadWillPopulateStoragesWithHigherPriority()
{
$emptyStorageA = $this->storageFactory();
$emptyStorageB = $this->storageFactory();
$storageC = $this->storageFactory(self::ID);
$emptyStorageD = $this->storageFactory();
$chain = new Chain;
$chain->add($emptyStorageA);
$chain->add($emptyStorageB);
$chain->add($storageC);
$chain->add($emptyStorageD);
// Chain is non empty
$this->assertFalse($chain->isEmpty());
$this->assertEquals(self::ID, $chain->read());
// Storage A and B are filled
$this->assertFalse($emptyStorageA->isEmpty());
$this->assertEquals(self::ID, $emptyStorageA->read());
$this->assertFalse($emptyStorageA->isEmpty());
$this->assertEquals(self::ID, $emptyStorageB->read());
// Storage C and D remain identical
$this->assertFalse($storageC->isEmpty());
$this->assertEquals(self::ID, $storageC->read());
$this->assertTrue($emptyStorageD->isEmpty());
}
/**
* @param mixed $identity
* @return StorageInterface
*/
protected function storageFactory($identity = null)
{
$storage = new NonPersistent();
if ($identity !== null) {
$storage->write($identity);
}
return $storage;
}
}