Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
/*
3
 * Copyright 2016 Google Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
 
18
namespace Google\Auth\Tests;
19
 
20
use Google\Auth\Cache\MemoryCacheItemPool;
21
use Psr\Cache\InvalidArgumentException;
22
 
23
class MemoryCacheItemPoolTest extends \PHPUnit_Framework_TestCase
24
{
25
    private $pool;
26
 
27
    public function setUp()
28
    {
29
        $this->pool = new MemoryCacheItemPool();
30
    }
31
 
32
    public function saveItem($key, $value)
33
    {
34
        $item = $this->pool->getItem($key);
35
        $item->set($value);
36
        $this->assertTrue($this->pool->save($item));
37
 
38
        return $item;
39
    }
40
 
41
    public function testGetsFreshItem()
42
    {
43
        $item = $this->pool->getItem('item');
44
 
45
        $this->assertInstanceOf('Google\Auth\Cache\Item', $item);
46
        $this->assertNull($item->get());
47
        $this->assertFalse($item->isHit());
48
    }
49
 
50
    public function testGetsExistingItem()
51
    {
52
        $key = 'item';
53
        $value = 'value';
54
        $this->saveItem($key, $value);
55
        $item = $this->pool->getItem($key);
56
 
57
        $this->assertInstanceOf('Google\Auth\Cache\Item', $item);
58
        $this->assertEquals($value, $item->get());
59
        $this->assertTrue($item->isHit());
60
    }
61
 
62
    public function testGetsMultipleItems()
63
    {
64
        $keys = ['item1', 'item2'];
65
        $items = $this->pool->getItems($keys);
66
 
67
        $this->assertEquals($keys, array_keys($items));
68
        $this->assertContainsOnlyInstancesOf('Google\Auth\Cache\Item', $items);
69
    }
70
 
71
    public function testHasItem()
72
    {
73
        $existsKey = 'does-exist';
74
        $this->saveItem($existsKey, 'value');
75
 
76
        $this->assertTrue($this->pool->hasItem($existsKey));
77
        $this->assertFalse($this->pool->hasItem('does-not-exist'));
78
    }
79
 
80
    public function testClear()
81
    {
82
        $key = 'item';
83
        $this->saveItem($key, 'value');
84
 
85
        $this->assertTrue($this->pool->hasItem($key));
86
        $this->assertTrue($this->pool->clear());
87
        $this->assertFalse($this->pool->hasItem($key));
88
    }
89
 
90
    public function testDeletesItem()
91
    {
92
        $key = 'item';
93
        $this->saveItem($key, 'value');
94
 
95
        $this->assertTrue($this->pool->deleteItem($key));
96
        $this->assertFalse($this->pool->hasItem($key));
97
    }
98
 
99
    public function testDeletesItems()
100
    {
101
        $keys = ['item1', 'item2'];
102
 
103
        foreach ($keys as $key) {
104
            $this->saveItem($key, 'value');
105
        }
106
 
107
        $this->assertTrue($this->pool->deleteItems($keys));
108
        $this->assertFalse($this->pool->hasItem($keys[0]));
109
        $this->assertFalse($this->pool->hasItem($keys[1]));
110
    }
111
 
112
    public function testDoesNotDeleteItemsWithInvalidKey()
113
    {
114
        $keys = ['item1', '{item2}', 'item3'];
115
        $value = 'value';
116
        $this->saveItem($keys[0], $value);
117
        $this->saveItem($keys[2], $value);
118
 
119
        try {
120
            $this->pool->deleteItems($keys);
121
        } catch (InvalidArgumentException $ex) {
122
            // continue execution
123
        }
124
 
125
        $this->assertTrue($this->pool->hasItem($keys[0]));
126
        $this->assertTrue($this->pool->hasItem($keys[2]));
127
    }
128
 
129
    public function testSavesItem()
130
    {
131
        $key = 'item';
132
        $this->saveItem($key, 'value');
133
 
134
        $this->assertTrue($this->pool->hasItem($key));
135
    }
136
 
137
    public function testSavesDeferredItem()
138
    {
139
        $item = $this->pool->getItem('item');
140
        $this->assertTrue($this->pool->saveDeferred($item));
141
    }
142
 
143
    public function testCommitsDeferredItems()
144
    {
145
        $keys = ['item1', 'item2'];
146
 
147
        foreach ($keys as $key) {
148
            $item = $this->pool->getItem($key);
149
            $item->set('value');
150
            $this->pool->saveDeferred($item);
151
        }
152
 
153
        $this->assertTrue($this->pool->commit());
154
        $this->assertTrue($this->pool->hasItem($keys[0]));
155
        $this->assertTrue($this->pool->hasItem($keys[1]));
156
    }
157
 
158
    /**
159
     * @expectedException \Psr\Cache\InvalidArgumentException
160
     * @dataProvider invalidKeys
161
     */
162
    public function testCheckInvalidKeys($key)
163
    {
164
        $this->pool->getItem($key);
165
        $this->pool->getItems([$key]);
166
        $this->pool->hasItem($key);
167
        $this->pool->deleteItem($key);
168
        $this->pool->deleteItems([$key]);
169
    }
170
 
171
    public function invalidKeys()
172
    {
173
        return [
174
            [1],
175
            [true],
176
            [null],
177
            [new \DateTime()],
178
            ['{'],
179
            ['}'],
180
            ['('],
181
            [')'],
182
            ['/'],
183
            ['\\'],
184
            ['@'],
185
            [':'],
186
            [[]]
187
        ];
188
    }
189
}