Completed
Pull Request — 2.x (#114)
by Rémi
02:09
created

testCacheActionWithInvalidToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\CacheBundle\Tests\Adapter;
13
14
use Sonata\CacheBundle\Adapter\SymfonyCache;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\Routing\RouterInterface;
17
18
/**
19
 * Class SymfonyCacheTest.
20
 *
21
 * @author Vincent Composieux <[email protected]>
22
 */
23
class SymfonyCacheTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @var SymfonyCache
27
     */
28
    protected $cache;
29
30
    /**
31
     * @var RouterInterface
32
     */
33
    protected $router;
34
35
    /**
36
     * @var Filesystem
37
     */
38
    protected $filesystem;
39
40
    /**
41
     * Sets up cache adapter.
42
     */
43
    protected function setUp()
44
    {
45
        $this->router = $this->getMock('Symfony\Component\Routing\RouterInterface');
46
        $this->filesystem = $this->getMock('Symfony\Component\Filesystem\Filesystem');
47
48
        $this->cache = new SymfonyCache($this->router, $this->filesystem, '/cache/dir', 'token', false, array('all', 'translations'), array());
49
    }
50
51
    /**
52
     * Tests cache initialization.
53
     */
54
    public function testInitCache()
55
    {
56
        $this->assertTrue($this->cache->flush(array()));
57
        $this->assertTrue($this->cache->flushAll());
58
59
        $this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache set() method does not exists');
60
        $this->cache->set(array('id' => 5), 'data');
61
62
        $this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache get() method does not exists');
63
        $this->cache->get(array('id' => 5));
64
65
        $this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache has() method does not exists');
66
        $this->cache->has(array('id' => 5));
67
    }
68
69
    /**
70
     * Tests cacheAction() method.
71
     */
72
    public function testCacheAction()
73
    {
74
        // Given
75
        $this->filesystem->expects($this->once())->method('exists')->will($this->returnValue(true));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        $this->filesystem->expects($this->once())->method('remove');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
78
        // When
79
        $response = $this->cache->cacheAction('token', 'translations');
80
81
        // Then
82
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
83
84
        $this->assertEquals(200, $response->getStatusCode(), 'Response should be 200');
85
        $this->assertEquals('ok', $response->getContent(), 'Response should return "OK"');
86
87
        $this->assertEquals(2, $response->headers->get('Content-Length'));
88
        $this->assertEquals('must-revalidate, no-cache, private', $response->headers->get('Cache-Control'));
89
    }
90
91
    /**
92
     * Tests cacheAction() method with an invalid token.
93
     */
94
    public function testCacheActionWithInvalidToken()
95
    {
96
        // Given
97
        // When - Then expect exception
98
        $this->setExpectedException('Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException');
99
100
        $this->cache->cacheAction('invalid-token', 'type');
101
    }
102
103
    /**
104
     * Tests cacheAction() method with an invalid cache type.
105
     */
106
    public function testCacheActionWithInvalidType()
107
    {
108
        // Given
109
        // When - Then expect exception
110
        $this->setExpectedException('\RuntimeException', 'Type "invalid-type" is not defined, allowed types are: "all, translations"');
111
112
        $this->cache->cacheAction('token', 'invalid-type');
113
    }
114
115
    /**
116
     * Asserts the flush method throws an exception if the version IP of the server cannot be detected.
117
     *
118
     * @expectedException        \InvalidArgumentException
119
     * @expectedExceptionMessage "wrong ip" is not a valid ip address
120
     */
121
    public function testFlushThrowsExceptionWithWrongIP()
122
    {
123
        $cache = new SymfonyCache(
124
            $this->router,
125
            $this->filesystem,
126
            '/cache/dir',
127
            'token',
128
            false,
129
            array('all', 'translations'),
130
            array(
131
                array('ip' => 'wrong ip'),
132
            )
133
        );
134
135
        $cache->flush();
136
    }
137
138
    /**
139
     * Tests the flush method with IPv4.
140
     */
141
    public function testFlushWithIPv4()
142
    {
143
        $mockBuilderClass = $this->getMockBuilderClass();
144
145
        $cache = new SymfonyCache(
146
            $this->router,
147
            $this->filesystem,
148
            '/cache/dir',
149
            'token',
150
            false,
151
            array('all', 'translations'),
152
            array(
153
                array('ip' => '213.186.35.9', 'domain' => 'www.example.com', 'basic' => false, 'port' => 80),
154
            )
155
        );
156
157
        $that = $this;
158
        $mocks = array();
159
160
        $builder = new $mockBuilderClass();
161
        $mock = $builder->setNamespace('Sonata\CacheBundle\Adapter')
162
            ->setName('socket_create')
163
            ->setFunction(function () use ($that) {
164
                $that->assertSame(array(AF_INET, SOCK_STREAM, SOL_TCP), func_get_args());
165
            })
166
            ->build();
167
        $mock->enable();
168
169
        $mocks[] = $mock;
170
171
        foreach (array('socket_set_option', 'socket_connect', 'socket_write', 'socket_read') as $function) {
172
            $builder = new $mockBuilderClass();
173
            $mock = $builder->setNamespace('Sonata\CacheBundle\Adapter')
174
                ->setName($function)
175
                ->setFunction(function () {
176
                })
177
                ->build();
178
            $mock->enable();
179
180
            $mocks[] = $mock;
181
        }
182
183
        $cache->flush();
184
185
        foreach ($mocks as $mock) {
186
            $mock->disable();
187
        }
188
    }
189
190
    /**
191
     * Tests the flush method with IPv6.
192
     */
193
    public function testFlushWithIPv6()
194
    {
195
        $mockBuilderClass = $this->getMockBuilderClass();
196
197
        $cache = new SymfonyCache(
198
            $this->router,
199
            $this->filesystem,
200
            '/cache/dir',
201
            'token',
202
            false,
203
            array('all', 'translations'),
204
            array(
205
                array('ip' => '2001:41d0:1:209:FF:FF:FF:FF', 'domain' => 'www.example.com', 'basic' => false, 'port' => 80),
206
            )
207
        );
208
209
        $that = $this;
210
        $mocks = array();
211
212
        $builder = new $mockBuilderClass();
213
        $mock = $builder->setNamespace('Sonata\CacheBundle\Adapter')
214
            ->setName('socket_create')
215
            ->setFunction(function () use ($that) {
216
                $that->assertSame(array(AF_INET6, SOCK_STREAM, SOL_TCP), func_get_args());
217
            })
218
            ->build();
219
        $mock->enable();
220
221
        $mocks[] = $mock;
222
223
        foreach (array('socket_set_option', 'socket_connect', 'socket_write', 'socket_read') as $function) {
224
            $builder = new $mockBuilderClass();
225
            $mock = $builder->setNamespace('Sonata\CacheBundle\Adapter')
226
                ->setName($function)
227
                ->setFunction(function () {
228
                })
229
                ->build();
230
            $mock->enable();
231
232
            $mocks[] = $mock;
233
        }
234
235
        $cache->flush();
236
237
        foreach ($mocks as $mock) {
238
            $mock->disable();
239
        }
240
    }
241
242
    /**
243
     * Gets the mock builder class according to the lib version (ie the PHP version).
244
     *
245
     * @return string
246
     */
247
    private function getMockBuilderClass()
248
    {
249
        if (class_exists('phpmock\MockBuilder')) {
250
            return 'phpmock\MockBuilder';
251
        } elseif (class_exists('malkusch\phpmock\MockBuilder')) {
252
            return 'malkusch\phpmock\MockBuilder';
253
        }
254
255
        $this->fail('Unable to find the MockBuilder class to mock build-in PHP functions');
256
    }
257
}
258