• 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/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 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 VarnishPurgerTest extends TestCase
31
{
32
    use ProphecyTrait;
33

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

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

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

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

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

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

60
    public function testEmptyTags(): void
61
    {
62
        $clientProphecy1 = $this->prophesize(ClientInterface::class);
×
63
        $clientProphecy1->request()->shouldNotBeCalled();
×
64

65
        /** @var HttpClientInterface $client */
66
        $client = $clientProphecy1->reveal();
×
67
        $purger = new VarnishPurger([$client]);
×
68
        $purger->purge([]);
×
69
    }
70

71
    #[\PHPUnit\Framework\Attributes\DataProvider('provideChunkHeaderCases')]
72
    public function testItChunksHeaderToAvoidHittingVarnishLimit(int $maxHeaderLength, array $iris, array $regexesToSend): void
73
    {
74
        /** @var HttpClientInterface $client */
NEW
75
        $client = new class implements ClientInterface {
×
76
            public array $sentRegexes = [];
77

78
            public function send(RequestInterface $request, array $options = []): ResponseInterface
79
            {
80
                throw new \LogicException('Not implemented');
×
81
            }
82

83
            public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface
84
            {
85
                throw new \LogicException('Not implemented');
×
86
            }
87

88
            public function request($method, $uri, array $options = []): ResponseInterface
89
            {
90
                $this->sentRegexes[] = $options['headers']['ApiPlatform-Ban-Regex'];
×
91

92
                return new Response();
×
93
            }
94

95
            public function requestAsync($method, $uri, array $options = []): PromiseInterface
96
            {
97
                throw new \LogicException('Not implemented');
×
98
            }
99

100
            public function getConfig($option = null): void
101
            {
102
                throw new \LogicException('Not implemented');
×
103
            }
104
        };
×
105

106
        $purger = new VarnishPurger([$client], $maxHeaderLength);
×
107
        $purger->purge($iris);
×
108

109
        self::assertSame($regexesToSend, $client->sentRegexes); // @phpstan-ignore-line
×
110
    }
111

112
    public static function provideChunkHeaderCases(): \Generator
113
    {
114
        yield 'no iri' => [
×
115
            50,
×
116
            [],
×
117
            [],
×
118
        ];
×
119

120
        yield 'one iri' => [
×
121
            50,
×
122
            ['/foo'],
×
123
            ['(/foo)($|\,)'],
×
124
        ];
×
125

126
        yield 'few iris' => [
×
127
            50,
×
128
            ['/foo', '/bar'],
×
129
            ['(/foo|/bar)($|\,)'],
×
130
        ];
×
131

132
        yield 'iris to generate a header with exactly the maximum length' => [
×
133
            22,
×
134
            ['/foo', '/bar', '/baz'],
×
135
            ['(/foo|/bar|/baz)($|\,)'],
×
136
        ];
×
137

138
        yield 'iris to generate a header with exactly the maximum length and a smaller one' => [
×
139
            17,
×
140
            ['/foo', '/bar', '/baz'],
×
141
            [
×
142
                '(/foo|/bar)($|\,)',
×
143
                '(/baz)($|\,)',
×
144
            ],
×
145
        ];
×
146

147
        yield 'with last iri too long to be part of the same header' => [
×
148
            35,
×
149
            ['/foo', '/bar', '/some-longer-tag'],
×
150
            [
×
151
                '(/foo|/bar)($|\,)',
×
152
                '(/some\-longer\-tag)($|\,)',
×
153
            ],
×
154
        ];
×
155

156
        yield 'iris to have five headers' => [
×
157
            25,
×
158
            ['/foo/1', '/foo/2', '/foo/3', '/foo/4', '/foo/5', '/foo/6', '/foo/7', '/foo/8', '/foo/9', '/foo/10'],
×
159
            [
×
160
                '(/foo/1|/foo/2)($|\,)',
×
161
                '(/foo/3|/foo/4)($|\,)',
×
162
                '(/foo/5|/foo/6)($|\,)',
×
163
                '(/foo/7|/foo/8)($|\,)',
×
164
                '(/foo/9|/foo/10)($|\,)',
×
165
            ],
×
166
        ];
×
167

168
        yield 'with varnish default limit' => [
×
169
            8000,
×
170
            array_fill(0, 3000, '/foo'),
×
171
            [
×
NEW
172
                \sprintf('(%s)($|\,)', implode('|', array_fill(0, 1598, '/foo'))),
×
NEW
173
                \sprintf('(%s)($|\,)', implode('|', array_fill(0, 1402, '/foo'))),
×
174
            ],
×
175
        ];
×
176
    }
177

178
    public function testConstructor(): void
179
    {
180
        $clientProphecy = $this->prophesize(HttpClientInterface::class);
×
181
        $clientProphecy->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/foo)($|\,)']])->shouldBeCalled();
×
182
        $purger = new VarnishPurger(new RewindableGenerator(static function () use ($clientProphecy) {
×
183
            yield $clientProphecy->reveal();
×
184
        }, 1));
×
185

186
        $purger->purge(['/foo']);
×
187
    }
188

189
    public function testGetResponseHeader(): void
190
    {
191
        $clientProphecy = $this->prophesize(HttpClientInterface::class);
×
192

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