Passed
Pull Request — master (#100)
by
unknown
23:41 queued 21:10
created

ApiTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 57
c 3
b 0
f 0
dl 0
loc 120
rs 10
wmc 9
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 <[email protected]>
10
 *
11
 * This source file is subject to the MIT license. For the full copyright,
12
 * license information, and credits/acknowledgements, please view the LICENSE
13
 * and README files that were distributed with this source code.
14
 */
15
16
namespace Numverify\Tests;
17
18
use GuzzleHttp\Client;
19
use GuzzleHttp\ClientInterface;
20
use Iterator;
21
use Numverify\Api;
22
use PHPUnit\Framework\Attributes\CoversClass;
23
use PHPUnit\Framework\Attributes\DataProvider;
24
use PHPUnit\Framework\Attributes\TestDox;
25
use PHPUnit\Framework\TestCase;
26
use ReflectionClass;
27
28
use function array_shift;
29
use function sys_get_temp_dir;
30
31
use const DIRECTORY_SEPARATOR;
32
33
/**
34
 * @internal
35
 */
36
#[CoversClass(Api::class)]
37
class ApiTest extends TestCase
38
{
39
    /**
40
     * Current version of the Numverify package.
41
     *
42
     * @var string
43
     */
44
    public const LIBRARY_VERSION = '3.0.1';
45
46
    private const ACCESS_KEY = 'SomeAccessKey';
47
48
    #[TestDox('Verifies the Api::LIBRARY_VERSION constant returns the correct version string.')]
49
    public function testApiReturnsCurrentLibraryVersion(): void
50
    {
51
        self::assertEquals(self::LIBRARY_VERSION, Api::LIBRARY_VERSION);
52
    }
53
54
    #[DataProvider('dataProviderForHttp')]
55
    #[TestDox('Construction with default Guzzle client, with cache path and $useHttps.')]
56
    public function testConstructionWithCachePathOption(bool $useHttps): void
57
    {
58
        $api = new Api(self::ACCESS_KEY, $useHttps, null, ['cachePath' => sys_get_temp_dir()]);
59
        self::assertObjectHasProperty('client', $api);
60
        self::assertFileExists(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'numverify');
61
62
        $reflectionClass    = new ReflectionClass($api);
63
        $reflectionProperty = $reflectionClass->getProperty('client');
64
        self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));
65
    }
66
67
    #[DataProvider('dataProviderForHttp')]
68
    #[TestDox('Construction with custom Guzzle client with $useHttps.')]
69
    public function testConstructionWithCustomClient(bool $useHttps): void
70
    {
71
        $api = new Api(self::ACCESS_KEY, $useHttps, new Client(['base_uri' => 'http://apilayer.net/api']));
72
        self::assertObjectHasProperty('client', $api);
73
74
        $reflectionClass    = new ReflectionClass($api);
75
        $reflectionProperty = $reflectionClass->getProperty('client');
76
        self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));
77
78
        /** @var Client $client */
79
        $client = $reflectionProperty->getValue($api);
80
81
        $client = self::parseGuzzleConfig($client);
82
83
        $expected = 'http://apilayer.net/api';
84
        $actual   = $client['base_uri'];
85
        self::assertSame($expected, $actual);
86
    }
87
88
    #[DataProvider('dataProviderForHttp')]
89
    #[TestDox('Construction with default Guzzle client with $useHttps.')]
90
    public function testConstructionWithDefaultClient(bool $useHttps): void
91
    {
92
        $api = new Api(self::ACCESS_KEY, $useHttps);
93
        self::assertObjectHasProperty('client', $api);
94
95
        $reflectionClass    = new ReflectionClass($api);
96
        $reflectionProperty = $reflectionClass->getProperty('client');
97
        self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));
98
99
        /** @var Client $client */
100
        $client = $reflectionProperty->getValue($api);
101
102
        $client = self::parseGuzzleConfig($client);
103
104
        $expected = ($useHttps ? 'https' : 'http') . '://apilayer.net/api';
105
        $actual   = $client['base_uri'];
106
        self::assertSame($expected, $actual);
107
    }
108
109
    #[DataProvider('dataProviderForHttp')]
110
    #[TestDox('Construction with default Guzzle client, extra options with $useHttps.')]
111
    public function testConstructionWithDefaultClientExtraOptions(bool $useHttps): void
112
    {
113
        $api = new Api(self::ACCESS_KEY, $useHttps, null, ['timeout' => 10]);
114
        self::assertObjectHasProperty('client', $api);
115
116
        $reflectionClass    = new ReflectionClass($api);
117
        $reflectionProperty = $reflectionClass->getProperty('client');
118
        self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));
119
120
        /** @var Client $client */
121
        $client = $reflectionProperty->getValue($api);
122
123
        $client = self::parseGuzzleConfig($client);
124
125
        $expected = ($useHttps ? 'https' : 'http') . '://apilayer.net/api';
126
        $actual   = $client['base_uri'];
127
        self::assertSame($expected, $actual);
128
        self::assertSame(10, $client['timeout']);
129
    }
130
131
    /**
132
     * @psalm-suppress PossiblyUnusedMethod
133
     */
134
    public static function dataProviderForHttp(): Iterator
135
    {
136
        yield [true];
137
        yield [false];
138
    }
139
140
    /**
141
     * @return array<string, int|string>
142
     */
143
    private static function parseGuzzleConfig(Client $client): array
144
    {
145
        $client = (array) $client;
146
147
        /** @var array<array-key, mixed> $config */
148
        $config = array_shift($client);
149
150
        /** @var array<string, int|string> $config */
151
        $config = [
152
            'base_uri' => (string) $config['base_uri'], // @phpstan-ignore-line
153
            'timeout'  => $config['timeout'] ?? 0,
154
        ];
155
        return $config;
156
    }
157
}
158