summaryrefslogtreecommitdiff
path: root/tests/units/Core/Cache/FileCacheTest.php
blob: b6336581f06819188aa2376bf9158df4e0eeea70 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php

namespace Kanboard\Core\Cache;

require_once __DIR__.'/../../Base.php';

function file_put_contents($filename, $data)
{
    return FileCacheTest::$functions->file_put_contents($filename, $data);
}

function file_get_contents($filename)
{
    return FileCacheTest::$functions->file_get_contents($filename);
}

function mkdir($filename, $mode = 0777, $recursif = false)
{
    return FileCacheTest::$functions->mkdir($filename, $mode, $recursif);
}

function is_dir($filename)
{
    return FileCacheTest::$functions->is_dir($filename);
}

function file_exists($filename)
{
    return FileCacheTest::$functions->file_exists($filename);
}

function unlink($filename)
{
    return FileCacheTest::$functions->unlink($filename);
}

class FileCacheTest extends \Base
{
    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    public static $functions;

    public function setUp()
    {
        parent::setup();

        self::$functions = $this
            ->getMockBuilder('stdClass')
            ->setMethods(array(
                'file_put_contents',
                'file_get_contents',
                'file_exists',
                'mkdir',
                'is_dir',
                'unlink',
            ))
            ->getMock();
    }

    public function tearDown()
    {
        parent::tearDown();
        self::$functions = null;
    }

    public function testSet()
    {
        $key = 'mykey';
        $data = 'data';
        $cache = new FileCache();

        self::$functions
            ->expects($this->at(0))
            ->method('is_dir')
            ->with(
                $this->equalTo(CACHE_DIR)
            )
            ->will($this->returnValue(false));

        self::$functions
            ->expects($this->at(1))
            ->method('mkdir')
            ->with(
                $this->equalTo(CACHE_DIR),
                0755
            )
            ->will($this->returnValue(true));

        self::$functions
            ->expects($this->at(2))
            ->method('file_put_contents')
            ->with(
                $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key),
                $this->equalTo(serialize($data))
            )
            ->will($this->returnValue(true));

        $cache->set($key, $data);
    }

    public function testGet()
    {
        $key = 'mykey';
        $data = 'data';
        $cache = new FileCache();

        self::$functions
            ->expects($this->at(0))
            ->method('file_exists')
            ->with(
                $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key)
            )
            ->will($this->returnValue(true));

        self::$functions
            ->expects($this->at(1))
            ->method('file_get_contents')
            ->with(
                $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key)
            )
            ->will($this->returnValue(serialize($data)));

        $this->assertSame($data, $cache->get($key));
    }

    public function testGetWithKeyNotFound()
    {
        $key = 'mykey';
        $cache = new FileCache();

        self::$functions
            ->expects($this->at(0))
            ->method('file_exists')
            ->with(
                $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key)
            )
            ->will($this->returnValue(false));

        $this->assertNull($cache->get($key));
    }

    public function testRemoveWithKeyNotFound()
    {
        $key = 'mykey';
        $cache = new FileCache();

        self::$functions
            ->expects($this->at(0))
            ->method('file_exists')
            ->with(
                $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key)
            )
            ->will($this->returnValue(false));

        self::$functions
            ->expects($this->never())
            ->method('unlink');

        $cache->remove($key);
    }

    public function testRemove()
    {
        $key = 'mykey';
        $cache = new FileCache();

        self::$functions
            ->expects($this->at(0))
            ->method('file_exists')
            ->with(
                $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key)
            )
            ->will($this->returnValue(true));

        self::$functions
            ->expects($this->at(1))
            ->method('unlink')
            ->with(
                $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key)
            )
            ->will($this->returnValue(true));

        $cache->remove($key);
    }
}