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

SymfonyCacheTest::getMockBuilderClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 7
nc 3
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) { $that->assertSame(array(AF_INET, SOCK_STREAM, SOL_TCP), func_get_args()); })
164
            ->build();
165
        $mock->enable();
166
167
        $mocks[] = $mock;
168
169
        foreach (array('socket_set_option', 'socket_connect', 'socket_write', 'socket_read') as $function) {
170
            $builder = new $mockBuilderClass();
171
            $mock    = $builder->setNamespace('Sonata\CacheBundle\Adapter')
172
                ->setName($function)
173
                ->setFunction(function () {})
174
                ->build();
175
            $mock->enable();
176
177
            $mocks[] = $mock;
178
        }
179
180
        $cache->flush();
181
182
        foreach ($mocks as $mock) {
183
            $mock->disable();
184
        }
185
    }
186
187
    /**
188
     * Tests the flush method with IPv6
189
     */
190
    public function testFlushWithIPv6()
191
    {
192
        $mockBuilderClass = $this->getMockBuilderClass();
193
194
        $cache = new SymfonyCache(
195
            $this->router,
196
            $this->filesystem,
197
            '/cache/dir',
198
            'token',
199
            false,
200
            array('all', 'translations'),
201
            array(
202
                array('ip' => '2001:41d0:1:209:FF:FF:FF:FF', 'domain' => 'www.example.com', 'basic' => false, 'port' => 80),
203
            )
204
        );
205
206
        $that  = $this;
207
        $mocks = array();
208
209
        $builder = new $mockBuilderClass();
210
        $mock    = $builder->setNamespace('Sonata\CacheBundle\Adapter')
211
            ->setName('socket_create')
212
            ->setFunction(function () use ($that) { $that->assertSame(array(AF_INET6, SOCK_STREAM, SOL_TCP), func_get_args()); })
213
            ->build();
214
        $mock->enable();
215
216
        $mocks[] = $mock;
217
218
        foreach (array('socket_set_option', 'socket_connect', 'socket_write', 'socket_read') as $function) {
219
            $builder = new $mockBuilderClass();
220
            $mock    = $builder->setNamespace('Sonata\CacheBundle\Adapter')
221
                ->setName($function)
222
                ->setFunction(function () {})
223
                ->build();
224
            $mock->enable();
225
226
            $mocks[] = $mock;
227
        }
228
229
        $cache->flush();
230
231
        foreach ($mocks as $mock) {
232
            $mock->disable();
233
        }
234
    }
235
236
    /**
237
     * Gets the mock builder class according to the lib version (ie the PHP version)
238
     *
239
     * @return string
240
     */
241
    private function getMockBuilderClass()
242
    {
243
        if (class_exists('phpmock\MockBuilder')) {
244
            return 'phpmock\MockBuilder';
245
        } elseif (class_exists('malkusch\phpmock\MockBuilder')) {
246
            return 'malkusch\phpmock\MockBuilder';
247
        } else {
248
            $this->fail('Unable to find the MockBuilder class to mock build-in PHP functions');
249
        }
250
    }
251
}
252