|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author : Jakiboy |
| 4 | + * @package : FloatPHP |
| 5 | + * @subpackage : Classes Http Component Tests |
| 6 | + * @version : 1.5.x |
| 7 | + * @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com> |
| 8 | + * @link : https://floatphp.com |
| 9 | + * @license : MIT |
| 10 | + * |
| 11 | + * This file if a part of FloatPHP Framework. |
| 12 | + */ |
| 13 | + |
| 14 | +declare(strict_types=1); |
| 15 | + |
| 16 | +namespace FloatPHP\Tests\Classes\Http; |
| 17 | + |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use FloatPHP\Classes\Http\Client; |
| 20 | + |
| 21 | +/** |
| 22 | + * Client class tests. |
| 23 | + */ |
| 24 | +final class ClientTest extends TestCase |
| 25 | +{ |
| 26 | + private Client $client; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->client = new Client('https://httpbin.org'); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Test client initialization. |
| 35 | + */ |
| 36 | + public function testClientInitialization(): void |
| 37 | + { |
| 38 | + $client = new Client('https://example.com'); |
| 39 | + $this->assertInstanceOf(Client::class, $client); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Test client initialization with params. |
| 44 | + */ |
| 45 | + public function testClientInitializationWithParams(): void |
| 46 | + { |
| 47 | + $params = ['timeout' => 30, 'redirect' => 5]; |
| 48 | + $client = new Client('https://example.com', $params); |
| 49 | + $this->assertInstanceOf(Client::class, $client); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test HTTP methods constants. |
| 54 | + */ |
| 55 | + public function testHttpMethodConstants(): void |
| 56 | + { |
| 57 | + $this->assertEquals('GET', Client::GET); |
| 58 | + $this->assertEquals('POST', Client::POST); |
| 59 | + $this->assertEquals('HEAD', Client::HEAD); |
| 60 | + $this->assertEquals('PUT', Client::PUT); |
| 61 | + $this->assertEquals('PATCH', Client::PATCH); |
| 62 | + $this->assertEquals('OPTIONS', Client::OPTIONS); |
| 63 | + $this->assertEquals('DELETE', Client::DELETE); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Test default timeout constant. |
| 68 | + */ |
| 69 | + public function testTimeoutConstant(): void |
| 70 | + { |
| 71 | + $this->assertEquals(10, Client::TIMEOUT); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test default redirect constant. |
| 76 | + */ |
| 77 | + public function testRedirectConstant(): void |
| 78 | + { |
| 79 | + $this->assertEquals(3, Client::REDIRECT); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test user agent constant. |
| 84 | + */ |
| 85 | + public function testUserAgentConstant(): void |
| 86 | + { |
| 87 | + $this->assertIsArray(Client::USERAGENT); |
| 88 | + $this->assertNotEmpty(Client::USERAGENT); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Test GET request method. |
| 93 | + */ |
| 94 | + public function testGetRequest(): void |
| 95 | + { |
| 96 | + $response = $this->client->get('/get'); |
| 97 | + $this->assertInstanceOf(Client::class, $response); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test POST request method. |
| 102 | + */ |
| 103 | + public function testPostRequest(): void |
| 104 | + { |
| 105 | + $response = $this->client->post('/post', ['key' => 'value']); |
| 106 | + $this->assertInstanceOf(Client::class, $response); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test HEAD request method. |
| 111 | + */ |
| 112 | + public function testHeadRequest(): void |
| 113 | + { |
| 114 | + $response = $this->client->head('/get'); |
| 115 | + $this->assertInstanceOf(Client::class, $response); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Test PUT request method. |
| 120 | + */ |
| 121 | + public function testPutRequest(): void |
| 122 | + { |
| 123 | + $response = $this->client->put('/put', ['key' => 'value']); |
| 124 | + $this->assertInstanceOf(Client::class, $response); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Test PATCH request method. |
| 129 | + */ |
| 130 | + public function testPatchRequest(): void |
| 131 | + { |
| 132 | + $response = $this->client->patch('/patch', ['key' => 'value']); |
| 133 | + $this->assertInstanceOf(Client::class, $response); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Test OPTIONS request method. |
| 138 | + */ |
| 139 | + public function testOptionsRequest(): void |
| 140 | + { |
| 141 | + $response = $this->client->options('/get'); |
| 142 | + $this->assertInstanceOf(Client::class, $response); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Test DELETE request method. |
| 147 | + */ |
| 148 | + public function testDeleteRequest(): void |
| 149 | + { |
| 150 | + $response = $this->client->delete('/delete'); |
| 151 | + $this->assertInstanceOf(Client::class, $response); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Test method chaining. |
| 156 | + */ |
| 157 | + public function testMethodChaining(): void |
| 158 | + { |
| 159 | + $response = $this->client |
| 160 | + ->setTimeout(15) |
| 161 | + ->setUserAgent('Custom-Agent') |
| 162 | + ->follow() |
| 163 | + ->return() |
| 164 | + ->get('/get'); |
| 165 | + |
| 166 | + $this->assertInstanceOf(Client::class, $response); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Test set timeout. |
| 171 | + */ |
| 172 | + public function testSetTimeout(): void |
| 173 | + { |
| 174 | + $result = $this->client->setTimeout(30); |
| 175 | + $this->assertInstanceOf(Client::class, $result); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Test set redirect. |
| 180 | + */ |
| 181 | + public function testSetRedirect(): void |
| 182 | + { |
| 183 | + $result = $this->client->setRedirect(5); |
| 184 | + $this->assertInstanceOf(Client::class, $result); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Test set encoding. |
| 189 | + */ |
| 190 | + public function testSetEncoding(): void |
| 191 | + { |
| 192 | + $result = $this->client->setEncoding('gzip'); |
| 193 | + $this->assertInstanceOf(Client::class, $result); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Test set user agent. |
| 198 | + */ |
| 199 | + public function testSetUserAgent(): void |
| 200 | + { |
| 201 | + $result = $this->client->setUserAgent('Test-Agent'); |
| 202 | + $this->assertInstanceOf(Client::class, $result); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Test return method. |
| 207 | + */ |
| 208 | + public function testReturn(): void |
| 209 | + { |
| 210 | + $result = $this->client->return(); |
| 211 | + $this->assertInstanceOf(Client::class, $result); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Test follow method. |
| 216 | + */ |
| 217 | + public function testFollow(): void |
| 218 | + { |
| 219 | + $result = $this->client->follow(); |
| 220 | + $this->assertInstanceOf(Client::class, $result); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Test header in method. |
| 225 | + */ |
| 226 | + public function testHeaderIn(): void |
| 227 | + { |
| 228 | + $result = $this->client->headerIn(); |
| 229 | + $this->assertInstanceOf(Client::class, $result); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Test get response. |
| 234 | + */ |
| 235 | + public function testGetResponse(): void |
| 236 | + { |
| 237 | + $this->client->get('/get'); |
| 238 | + $response = $this->client->getResponse(); |
| 239 | + $this->assertIsArray($response); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Test is gateway detection. |
| 244 | + */ |
| 245 | + public function testIsGateway(): void |
| 246 | + { |
| 247 | + $client = new Client(); |
| 248 | + $this->assertTrue($client->isCurl() || $client->isStream()); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Test gateway availability. |
| 253 | + */ |
| 254 | + public function testHasGateway(): void |
| 255 | + { |
| 256 | + $this->assertTrue(Client::hasCurl() || Client::hasStream()); |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Test get user agent. |
| 261 | + */ |
| 262 | + public function testGetUserAgent(): void |
| 263 | + { |
| 264 | + $ua = Client::getUserAgent(); |
| 265 | + $this->assertIsString($ua); |
| 266 | + $this->assertNotEmpty($ua); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Test get params. |
| 271 | + */ |
| 272 | + public function testGetParams(): void |
| 273 | + { |
| 274 | + $params = Client::getParams(); |
| 275 | + $this->assertIsArray($params); |
| 276 | + $this->assertArrayHasKey('timeout', $params); |
| 277 | + $this->assertArrayHasKey('redirect', $params); |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * Test format header. |
| 282 | + */ |
| 283 | + public function testFormatHeader(): void |
| 284 | + { |
| 285 | + $header = ['Content-Type' => 'application/json', 'Accept' => 'application/json']; |
| 286 | + $formatted = Client::formatHeader($header); |
| 287 | + $this->assertIsString($formatted); |
| 288 | + $this->assertStringContainsString('Content-Type: application/json', $formatted); |
| 289 | + } |
| 290 | + |
| 291 | + /** |
| 292 | + * Test pattern getters and setters. |
| 293 | + */ |
| 294 | + public function testPatternMethods(): void |
| 295 | + { |
| 296 | + Client::setPattern(['custom' => '/test/']); |
| 297 | + $pattern = Client::getPattern('status'); |
| 298 | + $this->assertIsString($pattern); |
| 299 | + } |
| 300 | +} |
0 commit comments