Passed
Push — master ( 564a9a...f4255a )
by Eric
02:08
created

ApiTest::parseGuzzleConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of the Numverify API Client for PHP.
7
 *
8
 * (c) 2024 Eric Sizemore <[email protected]>
9
 * (c) 2018-2021 Mark Rogoyski
10
 *
11
 * @license The MIT License
12
 *
13
 * For the full copyright and license information, please view the LICENSE.md
14
 * file that was distributed with this source code.
15
 */
16
17
namespace Numverify\Tests;
18
19
use GuzzleHttp\{
20
    ClientInterface,
21
    Client
22
};
23
use Iterator;
24
use Numverify\Api;
25
use PHPUnit\Framework\{
26
    Attributes\CoversClass,
27
    Attributes\DataProvider,
28
    TestCase
29
};
30
use ReflectionClass;
31
32
use function sys_get_temp_dir;
33
34
use const DIRECTORY_SEPARATOR;
35
36
/**
37
 * @internal
38
 */
39
#[CoversClass(Api::class)]
40
class ApiTest extends TestCase
41
{
42
    private const ACCESS_KEY = 'SomeAccessKey';
43
44
    /**
45
     * @testCase Construction with default Guzzle client.
46
     * @psalm-suppress DeprecatedMethod
47
     */
48
    #[DataProvider('dataProviderForHttp')]
49
    public function testConstructionWithDefaultClient(bool $useHttps): void
50
    {
51
        $api = new Api(self::ACCESS_KEY, $useHttps);
52
        self::assertObjectHasProperty('client', $api); // @phpstan-ignore-line
53
54
        $reflectionClass    = new ReflectionClass($api);
55
        $reflectionProperty = $reflectionClass->getProperty('client');
56
        self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));
57
58
        /** @var Client $client */
59
        $client = $reflectionProperty->getValue($api);
60
61
        $client = self::parseGuzzleConfig($client);
62
63
        $expected = ($useHttps ? 'https' : 'http') . '://apilayer.net/api';
64
        $actual   = (string) $client['base_uri']; // @phpstan-ignore-line
65
        self::assertSame($expected, $actual);
66
    }
67
68
    /**
69
     * @testCase Construction with custom Guzzle client.
70
     */
71
    #[DataProvider('dataProviderForHttp')]
72
    public function testConstructionWithCustomClient(bool $useHttps): void
73
    {
74
        $api = new Api(self::ACCESS_KEY, $useHttps, new Client(['base_uri' => 'http://apilayer.net/api']));
75
        self::assertObjectHasProperty('client', $api); // @phpstan-ignore-line
76
77
        $reflectionClass    = new ReflectionClass($api);
78
        $reflectionProperty = $reflectionClass->getProperty('client');
79
        self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));
80
81
        /** @var Client $client */
82
        $client = $reflectionProperty->getValue($api);
83
84
        $client = self::parseGuzzleConfig($client);
85
86
        $expected = 'http://apilayer.net/api';
87
        $actual   = (string) $client['base_uri']; // @phpstan-ignore-line
88
        self::assertSame($expected, $actual);
89
    }
90
91
    /**
92
     * @testCase Construction with default Guzzle client, extra options.
93
     */
94
    #[DataProvider('dataProviderForHttp')]
95
    public function testConstructionWithDefaultClientExtraOptions(bool $useHttps): void
96
    {
97
        $api = new Api(self::ACCESS_KEY, $useHttps, null, ['timeout' => 10]);
98
        self::assertObjectHasProperty('client', $api); // @phpstan-ignore-line
99
100
        $reflectionClass    = new ReflectionClass($api);
101
        $reflectionProperty = $reflectionClass->getProperty('client');
102
        self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));
103
104
        /** @var Client $client */
105
        $client = $reflectionProperty->getValue($api);
106
107
        $client = self::parseGuzzleConfig($client);
108
109
        $expected = ($useHttps ? 'https' : 'http') . '://apilayer.net/api';
110
        $actual   = (string) $client['base_uri']; // @phpstan-ignore-line
111
        self::assertSame($expected, $actual);
112
        self::assertSame(10, $client['timeout']);
113
    }
114
115
    /**
116
     * @return array<array-key, mixed>
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
117
     */
118
    private static function parseGuzzleConfig(Client $client): array
119
    {
120
        $client = (array) $client;
121
122
        /** @var array<array-key, mixed> $config */
123
        $config = \array_shift($client);
124
125
        return $config;
126
    }
127
128
    /**
129
     * @testCase Construction with default Guzzle client, with cache path.
130
     */
131
    #[DataProvider('dataProviderForHttp')]
132
    public function testConstructionWithCachePathOption(bool $useHttps): void
133
    {
134
        $api = new Api(self::ACCESS_KEY, $useHttps, null, ['cachePath' => sys_get_temp_dir()]);
135
        self::assertObjectHasProperty('client', $api); // @phpstan-ignore-line
136
        self::assertFileExists(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'numverify');
137
138
        $reflectionClass    = new ReflectionClass($api);
139
        $reflectionProperty = $reflectionClass->getProperty('client');
140
        self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));
141
    }
142
143
    /**
144
     * @psalm-suppress PossiblyUnusedMethod
145
     */
146
    public static function dataProviderForHttp(): Iterator
147
    {
148
        yield [true];
149
        yield [false];
150
    }
151
}
152