• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10315659289

09 Aug 2024 07:49AM UTC coverage: 7.841% (-0.006%) from 7.847%
10315659289

push

github

soyuka
style: cs fixes

70 of 529 new or added lines in 176 files covered. (13.23%)

160 existing lines in 58 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/HttpCache/Tests/VarnishXKeyPurgerTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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
declare(strict_types=1);
13

14
namespace ApiPlatform\HttpCache\Tests;
15

16
use ApiPlatform\HttpCache\VarnishXKeyPurger;
17
use GuzzleHttp\ClientInterface;
18
use GuzzleHttp\Promise\PromiseInterface;
19
use GuzzleHttp\Psr7\Response;
20
use PHPUnit\Framework\TestCase;
21
use Prophecy\PhpUnit\ProphecyTrait;
22
use Psr\Http\Message\RequestInterface;
23
use Psr\Http\Message\ResponseInterface;
24
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
25
use Symfony\Contracts\HttpClient\HttpClientInterface;
26

27
/**
28
 * @author Kévin Dunglas <dunglas@gmail.com>
29
 */
30
class VarnishXKeyPurgerTest extends TestCase
31
{
32
    use ProphecyTrait;
33

34
    public function testPurge(): void
35
    {
36
        $clientProphecy1 = $this->prophesize(ClientInterface::class);
×
37
        $clientProphecy1->request('PURGE', '', ['headers' => ['xkey' => '/foo']])->willReturn(new Response())->shouldBeCalled();
×
38
        $clientProphecy1->request('PURGE', '', ['headers' => ['xkey' => '/foo /bar']])->willReturn(new Response())->shouldBeCalled();
×
39

40
        $clientProphecy2 = $this->prophesize(ClientInterface::class);
×
41
        $clientProphecy2->request('PURGE', '', ['headers' => ['xkey' => '/foo']])->willReturn(new Response())->shouldBeCalled();
×
42
        $clientProphecy2->request('PURGE', '', ['headers' => ['xkey' => '/foo /bar']])->willReturn(new Response())->shouldBeCalled();
×
43

44
        $clientProphecy3 = $this->prophesize(ClientInterface::class);
×
45
        $clientProphecy3->request('PURGE', '', ['headers' => ['xkey' => '/foo /bar']])->willReturn(new Response())->shouldBeCalled();
×
46

47
        $clientProphecy4 = $this->prophesize(ClientInterface::class);
×
48
        $clientProphecy4->request('PURGE', '', ['headers' => ['xkey' => '/foo /bar']])->willReturn(new Response())->shouldBeCalled();
×
49

50
        /** @var HttpClientInterface $client1 */
51
        $client1 = $clientProphecy1->reveal();
×
52
        /** @var HttpClientInterface $client2 */
53
        $client2 = $clientProphecy2->reveal();
×
54
        $purger = new VarnishXKeyPurger([$client1, $client2]);
×
55
        $purger->purge(['/foo']);
×
56
        $purger->purge(['/foo' => '/foo', '/bar' => '/bar']);
×
57

58
        /** @var HttpClientInterface $client3 */
59
        $client3 = $clientProphecy3->reveal();
×
60
        /** @var HttpClientInterface $client4 */
61
        $client4 = $clientProphecy4->reveal();
×
62
        $purger = new VarnishXKeyPurger([$client3, $client4], 12);
×
63
        $purger->purge(['/foo' => '/foo', '/bar' => '/bar']);
×
64
    }
65

66
    public function testEmptyTags(): void
67
    {
68
        $clientProphecy1 = $this->prophesize(ClientInterface::class);
×
69
        $clientProphecy1->request()->shouldNotBeCalled();
×
70

71
        /** @var HttpClientInterface $client1 */
72
        $client1 = $clientProphecy1->reveal();
×
73
        $purger = new VarnishXKeyPurger([$client1]);
×
74
        $purger->purge([]);
×
75
    }
76

77
    public function testHeaderTooLong(): void
78
    {
79
        $this->expectExceptionMessage('IRI "/foobar-long-foobar-toolong-foofoo-barbar" is too long to fit current max header length (currently set to "20"). You can increase it using the "api_platform.http_cache.invalidation.max_header_length" parameter.');
×
80

81
        $clientProphecy1 = $this->prophesize(ClientInterface::class);
×
82
        $clientProphecy1->request('PURGE', '', ['headers' => ['xkey' => '/foobar-long-foobar-toolong-foofoo-barbar']])->willReturn(new Response())->shouldNotBeCalled();
×
83

84
        /** @var HttpClientInterface $client1 */
85
        $client1 = $clientProphecy1->reveal();
×
86
        $purger = new VarnishXKeyPurger([$client1], 20);
×
87
        $purger->purge(['/foobar-long-foobar-toolong-foofoo-barbar']);
×
88
    }
89

90
    public function testCustomGlue(): void
91
    {
92
        $clientProphecy1 = $this->prophesize(ClientInterface::class);
×
93
        $clientProphecy1->request('PURGE', '', ['headers' => ['xkey' => '/foo,/bar,/baz']])->willReturn(new Response())->shouldBeCalled();
×
94

95
        /** @var HttpClientInterface $client1 */
96
        $client1 = $clientProphecy1->reveal();
×
97
        $purger = new VarnishXKeyPurger([$client1], 50, ',');
×
98
        $purger->purge(['/foo', '/bar', '/baz']);
×
99
    }
100

101
    #[\PHPUnit\Framework\Attributes\DataProvider('provideChunkHeaderCases')]
102
    public function testItChunksHeaderToAvoidHittingVarnishLimit(int $maxHeaderLength, array $iris, array $keysToSend): void
103
    {
104
        /** @var HttpClientInterface $client */
NEW
105
        $client = new class implements ClientInterface {
×
106
            public array $sentKeys = [];
107

108
            public function send(RequestInterface $request, array $options = []): ResponseInterface
109
            {
110
                throw new \LogicException('Not implemented');
×
111
            }
112

113
            public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface
114
            {
115
                throw new \LogicException('Not implemented');
×
116
            }
117

118
            public function request($method, $uri, array $options = []): ResponseInterface
119
            {
120
                $this->sentKeys[] = $options['headers']['xkey'];
×
121

122
                return new Response();
×
123
            }
124

125
            public function requestAsync($method, $uri, array $options = []): PromiseInterface
126
            {
127
                throw new \LogicException('Not implemented');
×
128
            }
129

130
            public function getConfig($option = null): void
131
            {
132
                throw new \LogicException('Not implemented');
×
133
            }
134
        };
×
135

136
        $purger = new VarnishXKeyPurger([$client], $maxHeaderLength);
×
137
        $purger->purge($iris);
×
138

139
        self::assertSame($keysToSend, $client->sentKeys); // @phpstan-ignore-line
×
140
    }
141

142
    public static function provideChunkHeaderCases(): \Generator
143
    {
144
        yield 'no iri' => [
×
145
            50,
×
146
            [],
×
147
            [],
×
148
        ];
×
149

150
        yield 'one iri' => [
×
151
            50,
×
152
            ['/foo'],
×
153
            ['/foo'],
×
154
        ];
×
155

156
        yield 'few iris' => [
×
157
            50,
×
158
            ['/foo', '/bar', '/ab', '/cd'],
×
159
            ['/foo /bar /ab /cd'],
×
160
        ];
×
161

162
        yield 'iris to generate a header with exactly the maximum length' => [
×
163
            27,
×
164
            ['/foofoofoofoo', '/barbarbarbar'],
×
165
            ['/foofoofoofoo /barbarbarbar'],
×
166
        ];
×
167

168
        yield 'iris to generate a header with exactly the maximum length and a smaller one' => [
×
169
            21,
×
170
            ['/foobarfoo', '/barfoobar', '/baz'],
×
171
            [
×
172
                '/foobarfoo /barfoobar',
×
173
                '/baz',
×
174
            ],
×
175
        ];
×
176

177
        yield 'with last iri too long to be part of the same header' => [
×
178
            18,
×
179
            ['/foo', '/bar', '/some-longer-tag'],
×
180
            [
×
181
                '/foo /bar',
×
182
                '/some-longer-tag',
×
183
            ],
×
184
        ];
×
185

186
        yield 'iris to have five headers' => [
×
187
            13,
×
188
            ['/foo/1', '/foo/2', '/foo/3', '/foo/4', '/foo/5', '/foo/6', '/foo/7', '/foo/8', '/foo/9', '/foo/a'],
×
189
            ['/foo/1 /foo/2', '/foo/3 /foo/4', '/foo/5 /foo/6', '/foo/7 /foo/8', '/foo/9 /foo/a'],
×
190
        ];
×
191

192
        yield 'iris to have three headers of same size and one bigger' => [
×
193
            13,
×
194
            ['/foo/1', '/foo/2', '/foo/3', '/foo/4', '/foo/5', '/foo/6', '/foo/8910'],
×
195
            ['/foo/1 /foo/2', '/foo/3 /foo/4', '/foo/5 /foo/6', '/foo/8910'],
×
196
        ];
×
197

198
        yield 'with varnish default limit' => [
×
199
            8000,
×
200
            array_fill(0, 3002, '/foo'),
×
201
            [
×
202
                implode(' ', array_fill(0, 1600, '/foo')),
×
203
                implode(' ', array_fill(0, 1402, '/foo')),
×
204
            ],
×
205
        ];
×
206
    }
207

208
    public function testConstructor(): void
209
    {
210
        $clientProphecy = $this->prophesize(ClientInterface::class);
×
211
        $clientProphecy->request('PURGE', '', ['headers' => ['xkey' => '/foo']])->willReturn(new Response())->shouldBeCalled();
×
212
        $purger = new VarnishXKeyPurger(new RewindableGenerator(static function () use ($clientProphecy) {
×
213
            yield $clientProphecy->reveal();
×
214
        }, 1));
×
215

216
        $purger->purge(['/foo']);
×
217
    }
218
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc