Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
/*
3
 * Copyright 2015 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\CacheTrait;
21
 
22
class CacheTraitTest extends \PHPUnit_Framework_TestCase
23
{
24
    private $mockFetcher;
25
    private $mockCacheItem;
26
    private $mockCache;
27
 
28
    public function setUp()
29
    {
30
        $this->mockFetcher =
31
            $this
32
                ->getMockBuilder('Google\Auth\FetchAuthTokenInterface')
33
                ->getMock();
34
        $this->mockCacheItem =
35
            $this
36
                ->getMockBuilder('Psr\Cache\CacheItemInterface')
37
                ->getMock();
38
        $this->mockCache =
39
            $this
40
                ->getMockBuilder('Psr\Cache\CacheItemPoolInterface')
41
                ->getMock();
42
    }
43
 
44
    public function testSuccessfullyPullsFromCache()
45
    {
46
        $expectedValue = '1234';
47
        $this->mockCacheItem
48
            ->expects($this->once())
49
            ->method('get')
50
            ->will($this->returnValue($expectedValue));
51
        $this->mockCache
52
            ->expects($this->once())
53
            ->method('getItem')
54
            ->will($this->returnValue($this->mockCacheItem));
55
 
56
        $implementation = new CacheTraitImplementation([
57
            'cache' => $this->mockCache,
58
        ]);
59
 
60
        $cachedValue = $implementation->gCachedValue();
61
        $this->assertEquals($expectedValue, $cachedValue);
62
    }
63
 
64
    public function testFailsPullFromCacheWithNoCache()
65
    {
66
        $implementation = new CacheTraitImplementation();
67
 
68
        $cachedValue = $implementation->gCachedValue();
69
        $this->assertEquals(null, $cachedValue);
70
    }
71
 
72
    public function testFailsPullFromCacheWithoutKey()
73
    {
74
        $implementation = new CacheTraitImplementation([
75
            'cache' => $this->mockCache,
76
            'key'   => null,
77
        ]);
78
 
79
        $cachedValue = $implementation->gCachedValue();
80
    }
81
 
82
    public function testSuccessfullySetsToCache()
83
    {
84
        $value = '1234';
85
        $this->mockCacheItem
86
            ->expects($this->once())
87
            ->method('set')
88
            ->with($value);
89
        $this->mockCache
90
            ->expects($this->once())
91
            ->method('getItem')
92
            ->with($this->equalTo('key'))
93
            ->will($this->returnValue($this->mockCacheItem));
94
 
95
        $implementation = new CacheTraitImplementation([
96
            'cache' => $this->mockCache,
97
        ]);
98
 
99
        $implementation->sCachedValue($value);
100
    }
101
 
102
    public function testFailsSetToCacheWithNoCache()
103
    {
104
        $implementation = new CacheTraitImplementation();
105
 
106
        $implementation->sCachedValue('1234');
107
 
108
        $cachedValue = $implementation->sCachedValue('1234');
109
        $this->assertNull($cachedValue);
110
    }
111
 
112
    public function testFailsSetToCacheWithoutKey()
113
    {
114
        $implementation = new CacheTraitImplementation([
115
            'cache' => $this->mockCache,
116
            'key'   => null,
117
        ]);
118
 
119
        $cachedValue = $implementation->sCachedValue('1234');
120
        $this->assertNull($cachedValue);
121
    }
122
}
123
 
124
class CacheTraitImplementation
125
{
126
    use CacheTrait;
127
 
128
    private $cache;
129
    private $cacheConfig;
130
 
131
    public function __construct(array $config = [])
132
    {
133
        $this->key = array_key_exists('key', $config) ? $config['key'] : 'key';
134
        $this->cache = isset($config['cache']) ? $config['cache'] : null;
135
        $this->cacheConfig = [
136
            'prefix' => '',
137
            'lifetime' => 1000,
138
        ];
139
    }
140
 
141
    // allows us to keep trait methods private
142
    public function gCachedValue()
143
    {
144
        return $this->getCachedValue($this->key);
145
    }
146
 
147
    public function sCachedValue($v)
148
    {
149
        $this->setCachedValue($this->key, $v);
150
    }
151
}