Completed
Push — old_php_cleanup ( 0e14ab )
by Grégoire
02:16
created

SymfonyCacheTest::getMockBuilderClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
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 PHPUnit\Framework\TestCase;
15
use Sonata\CacheBundle\Adapter\SymfonyCache;
16
use Symfony\Component\Filesystem\Filesystem;
17
use Symfony\Component\Routing\RouterInterface;
18
use phpmock\MockBuilder;
19
20
/**
21
 * Class SymfonyCacheTest.
22
 *
23
 * @author Vincent Composieux <[email protected]>
24
 */
25
class SymfonyCacheTest extends TestCase
26
{
27
    /**
28
     * @var SymfonyCache
29
     */
30
    protected $cache;
31
32
    /**
33
     * @var RouterInterface
34
     */
35
    protected $router;
36
37
    /**
38
     * @var Filesystem
39
     */
40
    protected $filesystem;
41
42
    /**
43
     * Sets up cache adapter.
44
     */
45
    protected function setUp()
46
    {
47
        $this->router = $this->createMock('Symfony\Component\Routing\RouterInterface');
48
        $this->filesystem = $this->createMock('Symfony\Component\Filesystem\Filesystem');
49
50
        $this->cache = new SymfonyCache(
51
            $this->router,
52
            $this->filesystem,
53
            '/cache/dir',
54
            'token',
55
            false,
56
            ['all', 'translations'],
57
            [],
58
            []
59
        );
60
    }
61
62
    /**
63
     * Tests cache initialization.
64
     */
65
    public function testInitCache()
66
    {
67
        $this->assertTrue($this->cache->flush([]));
68
        $this->assertTrue($this->cache->flushAll());
69
70
        $this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache set() method does not exists');
71
        $this->cache->set(['id' => 5], 'data');
72
73
        $this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache get() method does not exists');
74
        $this->cache->get(['id' => 5]);
75
76
        $this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache has() method does not exists');
77
        $this->cache->has(['id' => 5]);
78
    }
79
80
    /**
81
     * Tests cacheAction() method.
82
     */
83
    public function testCacheAction()
84
    {
85
        // Given
86
        $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...
87
        $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...
88
89
        // When
90
        $response = $this->cache->cacheAction('token', 'translations');
91
92
        // Then
93
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
94
95
        $this->assertEquals(200, $response->getStatusCode(), 'Response should be 200');
96
        $this->assertEquals('ok', $response->getContent(), 'Response should return "OK"');
97
98
        $this->assertEquals(2, $response->headers->get('Content-Length'));
99
        $this->assertEquals('must-revalidate, no-cache, private', $response->headers->get('Cache-Control'));
100
    }
101
102
    /**
103
     * Tests cacheAction() method with an invalid token.
104
     */
105
    public function testCacheActionWithInvalidToken()
106
    {
107
        // Given
108
        // When - Then expect exception
109
        $this->setExpectedException('Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException');
110
111
        $this->cache->cacheAction('invalid-token', 'type');
112
    }
113
114
    /**
115
     * Tests cacheAction() method with an invalid cache type.
116
     */
117
    public function testCacheActionWithInvalidType()
118
    {
119
        // Given
120
        // When - Then expect exception
121
        $this->setExpectedException('\RuntimeException', 'Type "invalid-type" is not defined, allowed types are: "all, translations"');
122
123
        $this->cache->cacheAction('token', 'invalid-type');
124
    }
125
126
    /**
127
     * Asserts the flush method throws an exception if the IP version of the server cannot be detected.
128
     */
129
    public function testFlushThrowsExceptionWithWrongIP()
130
    {
131
        $cache = new SymfonyCache(
132
            $this->router,
133
            $this->filesystem,
134
            '/cache/dir',
135
            'token',
136
            false,
137
            ['all', 'translations'],
138
            [
139
                ['ip' => 'wrong ip'],
140
            ],
141
            []
142
        );
143
144
        $this->setExpectedException('\InvalidArgumentException', '"wrong ip" is not a valid ip address');
145
146
        $cache->flush();
147
    }
148
149
    /**
150
     * Tests the flush method with IPv4.
151
     */
152
    public function testFlushWithIPv4()
153
    {
154
        $cache = new SymfonyCache(
155
            $this->router,
156
            $this->filesystem,
157
            '/cache/dir',
158
            'token',
159
            false,
160
            ['all', 'translations'],
161
            [
162
                ['ip' => '213.186.35.9', 'domain' => 'www.example.com', 'basic' => false, 'port' => 80],
163
            ],
164
            [
165
                'RCV' => ['sec' => 2, 'usec' => 0],
166
                'SND' => ['sec' => 2, 'usec' => 0],
167
            ]
168
        );
169
170
        $mocks = [];
171
172
        $builder = new MockBuilder();
173
        $mock = $builder->setNamespace('Sonata\CacheBundle\Adapter')
174
            ->setName('socket_create')
175
            ->setFunction(function () {
176
                $this->assertSame([AF_INET, SOCK_STREAM, SOL_TCP], func_get_args());
177
            })
178
            ->build();
179
        $mock->enable();
180
181
        $mocks[] = $mock;
182
183
        foreach (['socket_set_option', 'socket_connect', 'socket_write', 'socket_read'] as $function) {
184
            $builder = new MockBuilder();
185
            $mock = $builder->setNamespace('Sonata\CacheBundle\Adapter')
186
                ->setName($function)
187
                ->setFunction(function () {
188
                })
189
                ->build();
190
            $mock->enable();
191
192
            $mocks[] = $mock;
193
        }
194
195
        $cache->flush();
196
197
        foreach ($mocks as $mock) {
198
            $mock->disable();
199
        }
200
    }
201
202
    /**
203
     * Tests the flush method with IPv6.
204
     */
205
    public function testFlushWithIPv6()
206
    {
207
        $cache = new SymfonyCache(
208
            $this->router,
209
            $this->filesystem,
210
            '/cache/dir',
211
            'token',
212
            false,
213
            ['all', 'translations'],
214
            [
215
                ['ip' => '2001:41d0:1:209:FF:FF:FF:FF', 'domain' => 'www.example.com', 'basic' => false, 'port' => 80],
216
            ],
217
            [
218
                'RCV' => ['sec' => 2, 'usec' => 0],
219
                'SND' => ['sec' => 2, 'usec' => 0],
220
            ]
221
        );
222
223
        $mocks = [];
224
225
        $builder = new MockBuilder();
226
        $mock = $builder->setNamespace('Sonata\CacheBundle\Adapter')
227
            ->setName('socket_create')
228
            ->setFunction(function () {
229
                $this->assertSame([AF_INET6, SOCK_STREAM, SOL_TCP], func_get_args());
230
            })
231
            ->build();
232
        $mock->enable();
233
234
        $mocks[] = $mock;
235
236
        foreach (['socket_set_option', 'socket_connect', 'socket_write', 'socket_read'] as $function) {
237
            $builder = new MockBuilder();
238
            $mock = $builder->setNamespace('Sonata\CacheBundle\Adapter')
239
                ->setName($function)
240
                ->setFunction(function () {
241
                })
242
                ->build();
243
            $mock->enable();
244
245
            $mocks[] = $mock;
246
        }
247
248
        $cache->flush();
249
250
        foreach ($mocks as $mock) {
251
            $mock->disable();
252
        }
253
    }
254
}
255