• 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/SouinPurgerTest.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\SouinPurger;
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\HttpFoundation\Request;
25
use Symfony\Contracts\HttpClient\HttpClientInterface;
26

27
/**
28
 * @author Sylvain Combraque <darkweak@protonmail.com>
29
 */
30
class SouinPurgerTest extends TestCase
31
{
32
    use ProphecyTrait;
33

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

40
        $clientProphecy2 = $this->prophesize(HttpClientInterface::class);
×
41
        $clientProphecy2->request('PURGE', '', ['headers' => ['Surrogate-Key' => '/foo']])->shouldBeCalled();
×
42
        $clientProphecy2->request('PURGE', '', ['headers' => ['Surrogate-Key' => '/foo, /bar']])->shouldBeCalled();
×
43

44
        $purger = new SouinPurger([$clientProphecy1->reveal(), $clientProphecy2->reveal()]);
×
45
        $purger->purge(['/foo']);
×
46
        $purger->purge(['/foo' => '/foo', '/bar' => '/bar']);
×
47
    }
48

49
    private function generateXResourcesTags(int $number, int $minimum = 0): array
50
    {
51
        $stack = [];
×
52

53
        for ($i = $minimum; $i < $number; ++$i) {
×
NEW
54
            $stack[] = \sprintf('/tags/%d', $i);
×
55
        }
56

57
        return $stack;
×
58
    }
59

60
    public function testMultiChunkedTags(): void
61
    {
62
        /** @var HttpClientInterface $client */
NEW
63
        $client = new class implements ClientInterface {
×
64
            public array $sentRegexes = [];
65

66
            public function send(RequestInterface $request, array $options = []): ResponseInterface
67
            {
68
                throw new \LogicException('Not implemented');
×
69
            }
70

71
            public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface
72
            {
73
                throw new \LogicException('Not implemented');
×
74
            }
75

76
            public function request($method, $uri, array $options = []): ResponseInterface
77
            {
78
                $this->sentRegexes[] = $options['headers']['Surrogate-Key'];
×
79

80
                return new Response();
×
81
            }
82

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

88
            public function getConfig($option = null): void
89
            {
90
                throw new \LogicException('Not implemented');
×
91
            }
92
        };
×
93
        $purger = new SouinPurger([$client]);
×
94
        $purger->purge($this->generateXResourcesTags(200));
×
95

96
        self::assertSame([
×
97
            implode(', ', $this->generateXResourcesTags(146)),
×
98
            implode(', ', $this->generateXResourcesTags(200, 146)),
×
99
        ], $client->sentRegexes); // @phpstan-ignore-line
×
100
    }
101

102
    public function testPurgeWithMultipleClients(): void
103
    {
104
        /** @var HttpClientInterface $client1 */
NEW
105
        $client1 = new class implements ClientInterface {
×
106
            public $requests = [];
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->requests[] = [$method, 'http://dummy_host/dummy_api_path/souin_api', $options];
×
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
        /** @var HttpClientInterface $client2 */
NEW
136
        $client2 = new class implements ClientInterface {
×
137
            public $requests = [];
138

139
            public function send(RequestInterface $request, array $options = []): ResponseInterface
140
            {
141
                throw new \LogicException('Not implemented');
×
142
            }
143

144
            public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface
145
            {
146
                throw new \LogicException('Not implemented');
×
147
            }
148

149
            public function request($method, $uri, array $options = []): ResponseInterface
150
            {
151
                $this->requests[] = [$method, 'http://dummy_host/dummy_api_path/souin_api', $options];
×
152

153
                return new Response();
×
154
            }
155

156
            public function requestAsync($method, $uri, array $options = []): PromiseInterface
157
            {
158
                throw new \LogicException('Not implemented');
×
159
            }
160

161
            public function getConfig($option = null): void
162
            {
163
                throw new \LogicException('Not implemented');
×
164
            }
165
        };
×
166

167
        $purger = new SouinPurger([$client1, $client2]);
×
168
        $purger->purge(['/foo']);
×
169
        self::assertSame([
×
170
            Request::METHOD_PURGE,
×
171
            'http://dummy_host/dummy_api_path/souin_api',
×
172
            ['headers' => ['Surrogate-Key' => '/foo']],
×
173
        ], $client1->requests[0]); // @phpstan-ignore-line
×
174
        self::assertSame([
×
175
            Request::METHOD_PURGE,
×
176
            'http://dummy_host/dummy_api_path/souin_api',
×
177
            ['headers' => ['Surrogate-Key' => '/foo']],
×
178
        ], $client2->requests[0]); // @phpstan-ignore-line
×
179
    }
180

181
    public function testGetResponseHeaders(): void
182
    {
183
        $purger = new SouinPurger([]);
×
184
        self::assertSame(['Surrogate-Key' => ''], $purger->getResponseHeaders([]));
×
185
        self::assertSame(['Surrogate-Key' => 'first-value/, second'], $purger->getResponseHeaders(['first-value/', 'second']));
×
186
        self::assertSame(['Surrogate-Key' => 'C0mplex_Value/, The value with spaces'], $purger->getResponseHeaders(['C0mplex_Value/', 'The value with spaces']));
×
187
    }
188
}
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