|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\Http\Client\{ClientFactory, CurlClient, PhpClient}; |
|
6
|
|
|
use Koded\Http\Interfaces\{ClientType, HttpMethod}; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @group internet |
|
11
|
|
|
*/ |
|
12
|
|
|
class ClientFactoryTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
const URI = 'https://example.com'; |
|
15
|
|
|
|
|
16
|
|
|
public function test_php_factory() |
|
17
|
|
|
{ |
|
18
|
|
|
$instance = (new ClientFactory(ClientType::PHP))->get(self::URI); |
|
19
|
|
|
$this->assertInstanceOf(PhpClient::class, $instance); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function test_curl_factory() |
|
23
|
|
|
{ |
|
24
|
|
|
$instance = (new ClientFactory)->get(self::URI); |
|
25
|
|
|
$this->assertInstanceOf(CurlClient::class, $instance, 'CurlClient is the default'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// public function test_factory_should_throw_exception_for_unknown_client() |
|
29
|
|
|
// { |
|
30
|
|
|
// $this->expectException(InvalidArgumentException::class); |
|
31
|
|
|
// $this->expectExceptionMessage('4 is not a valid HTTP client'); |
|
32
|
|
|
// (new ClientFactory(4))->get('localhost'); |
|
33
|
|
|
// } |
|
34
|
|
|
|
|
35
|
|
|
public function test_get() |
|
36
|
|
|
{ |
|
37
|
|
|
$client = (new ClientFactory)->get(self::URI, []); |
|
38
|
|
|
// $this->assertSame(Request::GET, $client->getMethod()); |
|
39
|
|
|
$this->assertSame(HttpMethod::GET->value, $client->getMethod()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function test_post() |
|
43
|
|
|
{ |
|
44
|
|
|
$client = (new ClientFactory)->post(self::URI, []); |
|
45
|
|
|
// $this->assertSame(Request::POST, $client->getMethod()); |
|
46
|
|
|
$this->assertSame(HttpMethod::POST->value, $client->getMethod()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function test_put() |
|
50
|
|
|
{ |
|
51
|
|
|
$client = (new ClientFactory)->put(self::URI, []); |
|
52
|
|
|
// $this->assertSame(Request::PUT, $client->getMethod()); |
|
53
|
|
|
$this->assertSame(HttpMethod::PUT->value, $client->getMethod()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function test_head() |
|
57
|
|
|
{ |
|
58
|
|
|
$client = (new ClientFactory)->head(self::URI, []); |
|
59
|
|
|
// $this->assertSame(Request::HEAD, $client->getMethod()); |
|
60
|
|
|
$this->assertSame(HttpMethod::HEAD->value, $client->getMethod()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function test_patch() |
|
64
|
|
|
{ |
|
65
|
|
|
$client = (new ClientFactory)->patch(self::URI, []); |
|
66
|
|
|
// $this->assertSame(Request::PATCH, $client->getMethod()); |
|
67
|
|
|
$this->assertSame(HttpMethod::PATCH->value, $client->getMethod()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function test_delete() |
|
71
|
|
|
{ |
|
72
|
|
|
$client = (new ClientFactory)->delete(self::URI, []); |
|
73
|
|
|
// $this->assertSame(Request::DELETE, $client->getMethod()); |
|
74
|
|
|
$this->assertSame(HttpMethod::DELETE->value, $client->getMethod()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function test_psr18_client_create() |
|
78
|
|
|
{ |
|
79
|
|
|
$client = (new ClientFactory)->client(); |
|
80
|
|
|
$this->assertSame('HEAD', $client->getMethod()); |
|
81
|
|
|
$this->assertEmpty((string)$client->getUri()); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|