• 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/VarnishPurgerTest.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\VarnishPurger;
17
use PHPUnit\Framework\TestCase;
18
use Prophecy\PhpUnit\ProphecyTrait;
19
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
20
use Symfony\Component\HttpClient\Response\MockResponse;
21
use Symfony\Contracts\HttpClient\HttpClientInterface;
22
use Symfony\Contracts\HttpClient\ResponseInterface;
23
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
24

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

32
    public function testPurge(): void
33
    {
34
        $clientProphecy1 = $this->prophesize(HttpClientInterface::class);
×
35
        $clientProphecy1->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/foo)($|\,)']])->shouldBeCalled();
×
36
        $clientProphecy1->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/foo|/bar)($|\,)']])->shouldBeCalled();
×
37

38
        $clientProphecy2 = $this->prophesize(HttpClientInterface::class);
×
39
        $clientProphecy2->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/foo)($|\,)']])->shouldBeCalled();
×
40
        $clientProphecy2->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/foo|/bar)($|\,)']])->shouldBeCalled();
×
41

42
        $clientProphecy3 = $this->prophesize(HttpClientInterface::class);
×
43
        $clientProphecy3->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/foo)($|\,)']])->shouldBeCalled();
×
44
        $clientProphecy3->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/bar)($|\,)']])->shouldBeCalled();
×
45

46
        $clientProphecy4 = $this->prophesize(HttpClientInterface::class);
×
47
        $clientProphecy4->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/foo)($|\,)']])->shouldBeCalled();
×
48
        $clientProphecy4->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/bar)($|\,)']])->shouldBeCalled();
×
49

50
        $purger = new VarnishPurger([$clientProphecy1->reveal(), $clientProphecy2->reveal()]);
×
51
        $purger->purge(['/foo']);
×
52
        $purger->purge(['/foo' => '/foo', '/bar' => '/bar']);
×
53

54
        $purger = new VarnishPurger([$clientProphecy3->reveal(), $clientProphecy4->reveal()], 12);
×
55
        $purger->purge(['/foo' => '/foo', '/bar' => '/bar']);
×
56
    }
57

58
    public function testEmptyTags(): void
59
    {
NEW
60
        $clientProphecy1 = $this->prophesize(HttpClientInterface::class);
×
61
        $clientProphecy1->request()->shouldNotBeCalled();
×
62

UNCOV
63
        $client = $clientProphecy1->reveal();
×
64
        $purger = new VarnishPurger([$client]);
×
65
        $purger->purge([]);
×
66
    }
67

68
    #[\PHPUnit\Framework\Attributes\DataProvider('provideChunkHeaderCases')]
69
    public function testItChunksHeaderToAvoidHittingVarnishLimit(int $maxHeaderLength, array $iris, array $regexesToSend): void
70
    {
NEW
71
        $client = new class implements HttpClientInterface {
×
72
            public array $sentRegexes = [];
73

74
            public function request(string $method, string $url, array $options = []): ResponseInterface
75
            {
76
                $this->sentRegexes[] = $options['headers']['ApiPlatform-Ban-Regex'];
×
77

NEW
78
                return new MockResponse();
×
79
            }
80

81
            public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface
82
            {
83
                throw new \LogicException('Not implemented');
×
84
            }
85

86
            public function withOptions(array $options): static
87
            {
NEW
88
                return $this;
×
89
            }
90
        };
×
91

92
        $purger = new VarnishPurger([$client], $maxHeaderLength);
×
93
        $purger->purge($iris);
×
94

NEW
95
        self::assertSame($regexesToSend, $client->sentRegexes);
×
96
    }
97

98
    public static function provideChunkHeaderCases(): \Generator
99
    {
100
        yield 'no iri' => [
×
101
            50,
×
102
            [],
×
103
            [],
×
104
        ];
×
105

106
        yield 'one iri' => [
×
107
            50,
×
108
            ['/foo'],
×
109
            ['(/foo)($|\,)'],
×
110
        ];
×
111

112
        yield 'few iris' => [
×
113
            50,
×
114
            ['/foo', '/bar'],
×
115
            ['(/foo|/bar)($|\,)'],
×
116
        ];
×
117

118
        yield 'iris to generate a header with exactly the maximum length' => [
×
119
            22,
×
120
            ['/foo', '/bar', '/baz'],
×
121
            ['(/foo|/bar|/baz)($|\,)'],
×
122
        ];
×
123

124
        yield 'iris to generate a header with exactly the maximum length and a smaller one' => [
×
125
            17,
×
126
            ['/foo', '/bar', '/baz'],
×
127
            [
×
128
                '(/foo|/bar)($|\,)',
×
129
                '(/baz)($|\,)',
×
130
            ],
×
131
        ];
×
132

133
        yield 'with last iri too long to be part of the same header' => [
×
134
            35,
×
135
            ['/foo', '/bar', '/some-longer-tag'],
×
136
            [
×
137
                '(/foo|/bar)($|\,)',
×
138
                '(/some\-longer\-tag)($|\,)',
×
139
            ],
×
140
        ];
×
141

142
        yield 'iris to have five headers' => [
×
143
            25,
×
144
            ['/foo/1', '/foo/2', '/foo/3', '/foo/4', '/foo/5', '/foo/6', '/foo/7', '/foo/8', '/foo/9', '/foo/10'],
×
145
            [
×
146
                '(/foo/1|/foo/2)($|\,)',
×
147
                '(/foo/3|/foo/4)($|\,)',
×
148
                '(/foo/5|/foo/6)($|\,)',
×
149
                '(/foo/7|/foo/8)($|\,)',
×
150
                '(/foo/9|/foo/10)($|\,)',
×
151
            ],
×
152
        ];
×
153

154
        yield 'with varnish default limit' => [
×
155
            8000,
×
156
            array_fill(0, 3000, '/foo'),
×
157
            [
×
158
                \sprintf('(%s)($|\,)', implode('|', array_fill(0, 1598, '/foo'))),
×
159
                \sprintf('(%s)($|\,)', implode('|', array_fill(0, 1402, '/foo'))),
×
160
            ],
×
161
        ];
×
162
    }
163

164
    public function testConstructor(): void
165
    {
166
        $clientProphecy = $this->prophesize(HttpClientInterface::class);
×
167
        $clientProphecy->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/foo)($|\,)']])->shouldBeCalled();
×
168
        $purger = new VarnishPurger(new RewindableGenerator(static function () use ($clientProphecy) {
×
169
            yield $clientProphecy->reveal();
×
170
        }, 1));
×
171

172
        $purger->purge(['/foo']);
×
173
    }
174

175
    public function testGetResponseHeader(): void
176
    {
177
        $clientProphecy = $this->prophesize(HttpClientInterface::class);
×
178

179
        $purger = new VarnishPurger([$clientProphecy->reveal()]);
×
180
        self::assertSame(['Cache-Tags' => '/foo'], $purger->getResponseHeaders(['/foo']));
×
181
    }
182
}
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