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\Item;
21
 
22
class ItemTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function getItem($key)
25
    {
26
        return new Item($key);
27
    }
28
 
29
    public function testGetsKey()
30
    {
31
        $key = 'item';
32
 
33
        $this->assertEquals($key, $this->getItem($key)->getKey());
34
    }
35
 
36
    public function testGetsNull()
37
    {
38
        $item = $this->getItem('item');
39
 
40
        $this->assertNull($item->get());
41
        $this->assertFalse($item->isHit());
42
    }
43
 
44
    public function testGetsValue()
45
    {
46
        $value = 'value';
47
        $item = $this->getItem('item');
48
        $item->set($value);
49
 
50
        $this->assertEquals('value', $item->get());
51
    }
52
 
53
    /**
54
     * @dataProvider values
55
     */
56
    public function testSetsValue($value)
57
    {
58
        $item = $this->getItem('item');
59
        $item->set($value);
60
 
61
        $this->assertEquals($value, $item->get());
62
    }
63
 
64
    public function values()
65
    {
66
        return [
67
            [1],
68
            [1.5],
69
            [true],
70
            [null],
71
            [new \DateTime()],
72
            [['test']],
73
            ['value']
74
        ];
75
    }
76
 
77
    public function testIsHit()
78
    {
79
        $item = $this->getItem('item');
80
 
81
        $this->assertFalse($item->isHit());
82
 
83
        $item->set('value');
84
 
85
        $this->assertTrue($item->isHit());
86
    }
87
 
88
    public function testExpiresAt()
89
    {
90
        $item = $this->getItem('item');
91
        $item->set('value');
92
        $item->expiresAt(new \DateTime('now + 1 hour'));
93
 
94
        $this->assertTrue($item->isHit());
95
 
96
        $item->expiresAt(null);
97
 
98
        $this->assertTrue($item->isHit());
99
 
100
        $item->expiresAt(new \DateTime('yesterday'));
101
 
102
        $this->assertFalse($item->isHit());
103
    }
104
 
105
    public function testExpiresAfter()
106
    {
107
        $item = $this->getItem('item');
108
        $item->set('value');
109
        $item->expiresAfter(30);
110
 
111
        $this->assertTrue($item->isHit());
112
 
113
        $item->expiresAfter(0);
114
 
115
        $this->assertFalse($item->isHit());
116
 
117
        $item->expiresAfter(new \DateInterval('PT30S'));
118
 
119
        $this->assertTrue($item->isHit());
120
 
121
        $item->expiresAfter(null);
122
 
123
        $this->assertTrue($item->isHit());
124
    }
125
}