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

api-platform / core / 15999509586

01 Jul 2025 12:36PM UTC coverage: 21.821% (-0.2%) from 22.065%
15999509586

push

github

web-flow
chore: more phpstan fixes (#7265)

2 of 25 new or added lines in 6 files covered. (8.0%)

38 existing lines in 6 files now uncovered.

11388 of 52189 relevant lines covered (21.82%)

10.89 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\Psr7\Response;
19
use PHPUnit\Framework\TestCase;
20
use Prophecy\PhpUnit\ProphecyTrait;
21
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
22
use Symfony\Component\HttpClient\Response\MockResponse;
23
use Symfony\Contracts\HttpClient\HttpClientInterface;
24
use Symfony\Contracts\HttpClient\ResponseInterface;
25
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
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
    {
NEW
104
        $client = new class implements HttpClientInterface {
×
105
            public array $sentKeys = [];
106

107
            public function request(string $method, string $url, array $options = []): ResponseInterface
108
            {
109
                $this->sentKeys[] = $options['headers']['xkey'];
×
110

NEW
111
                return new MockResponse();
×
112
            }
113

114
            public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface
115
            {
116
                throw new \LogicException('Not implemented');
×
117
            }
118

119
            public function withOptions(array $options): static
120
            {
NEW
121
                return $this;
×
122
            }
123
        };
×
124

125
        $purger = new VarnishXKeyPurger([$client], $maxHeaderLength);
×
126
        $purger->purge($iris);
×
127

NEW
128
        self::assertSame($keysToSend, $client->sentKeys);
×
129
    }
130

131
    public static function provideChunkHeaderCases(): \Generator
132
    {
133
        yield 'no iri' => [
×
134
            50,
×
135
            [],
×
136
            [],
×
137
        ];
×
138

139
        yield 'one iri' => [
×
140
            50,
×
141
            ['/foo'],
×
142
            ['/foo'],
×
143
        ];
×
144

145
        yield 'few iris' => [
×
146
            50,
×
147
            ['/foo', '/bar', '/ab', '/cd'],
×
148
            ['/foo /bar /ab /cd'],
×
149
        ];
×
150

151
        yield 'iris to generate a header with exactly the maximum length' => [
×
152
            27,
×
153
            ['/foofoofoofoo', '/barbarbarbar'],
×
154
            ['/foofoofoofoo /barbarbarbar'],
×
155
        ];
×
156

157
        yield 'iris to generate a header with exactly the maximum length and a smaller one' => [
×
158
            21,
×
159
            ['/foobarfoo', '/barfoobar', '/baz'],
×
160
            [
×
161
                '/foobarfoo /barfoobar',
×
162
                '/baz',
×
163
            ],
×
164
        ];
×
165

166
        yield 'with last iri too long to be part of the same header' => [
×
167
            18,
×
168
            ['/foo', '/bar', '/some-longer-tag'],
×
169
            [
×
170
                '/foo /bar',
×
171
                '/some-longer-tag',
×
172
            ],
×
173
        ];
×
174

175
        yield 'iris to have five headers' => [
×
176
            13,
×
177
            ['/foo/1', '/foo/2', '/foo/3', '/foo/4', '/foo/5', '/foo/6', '/foo/7', '/foo/8', '/foo/9', '/foo/a'],
×
178
            ['/foo/1 /foo/2', '/foo/3 /foo/4', '/foo/5 /foo/6', '/foo/7 /foo/8', '/foo/9 /foo/a'],
×
179
        ];
×
180

181
        yield 'iris to have three headers of same size and one bigger' => [
×
182
            13,
×
183
            ['/foo/1', '/foo/2', '/foo/3', '/foo/4', '/foo/5', '/foo/6', '/foo/8910'],
×
184
            ['/foo/1 /foo/2', '/foo/3 /foo/4', '/foo/5 /foo/6', '/foo/8910'],
×
185
        ];
×
186

187
        yield 'with varnish default limit' => [
×
188
            8000,
×
189
            array_fill(0, 3002, '/foo'),
×
190
            [
×
191
                implode(' ', array_fill(0, 1600, '/foo')),
×
192
                implode(' ', array_fill(0, 1402, '/foo')),
×
193
            ],
×
194
        ];
×
195
    }
196

197
    public function testConstructor(): void
198
    {
199
        $clientProphecy = $this->prophesize(ClientInterface::class);
×
200
        $clientProphecy->request('PURGE', '', ['headers' => ['xkey' => '/foo']])->willReturn(new Response())->shouldBeCalled();
×
201
        $purger = new VarnishXKeyPurger(new RewindableGenerator(static function () use ($clientProphecy) {
×
202
            yield $clientProphecy->reveal();
×
203
        }, 1));
×
204

205
        $purger->purge(['/foo']);
×
206
    }
207
}
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