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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 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
    /**
72
     * @dataProvider provideChunkHeaderCases
73
     */
74
    public function testItChunksHeaderToAvoidHittingVarnishLimit(int $maxHeaderLength, array $iris, array $regexesToSend): void
75
    {
76
        /** @var HttpClientInterface $client */
77
        $client = new class() implements ClientInterface {
×
78
            public array $sentRegexes = [];
79

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

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

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

94
                return new Response();
×
95
            }
96

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc