File: AwsRetryStrategyTest.php

package info (click to toggle)
php-async-aws-core 1.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 988 kB
  • sloc: php: 6,837; makefile: 32
file content (50 lines) | stat: -rw-r--r-- 1,634 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

namespace AsyncAws\Core\Tests\HttpClient;

use AsyncAws\Core\HttpClient\AwsRetryStrategy;
use AsyncAws\Core\Test\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\AsyncContext;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;

class AwsRetryStrategyTest extends TestCase
{
    /**
     * @dataProvider provideRetries
     */
    #[DataProvider('provideRetries')]
    public function testShoudRetry(?bool $expected, int $statusCode, ?string $response): void
    {
        if (!class_exists(GenericRetryStrategy::class)) {
            self::markTestSkipped('AwsRetryStrategy requires symfony/http-client 5.2');
        }
        $strategy = new AwsRetryStrategy();
        $context = $this->getContext($statusCode);

        self::assertSame($expected, $strategy->shouldRetry($context, $response, null));
    }

    public static function provideRetries(): iterable
    {
        yield [false, 200, null];
        yield [true, 429, null];
        yield [null, 400, null];
        yield [false, 400, 'this is invalid'];
        yield [false, 400, '{"__type": "RandomError"}'];
        yield [true, 400, '{"__type": "RequestLimitExceeded"}'];
    }

    private function getContext(int $statusCode): AsyncContext
    {
        $passthru = null;
        $info = [
            'http_code' => $statusCode,
        ];
        $response = new MockResponse('', $info);

        return new AsyncContext($passthru, new MockHttpClient(), $response, $info, null, 0);
    }
}