1
|
|
|
<?php |
2
|
|
|
namespace Mezon\CustomClient\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Mezon\CustomClient\CurlWrapper; |
6
|
|
|
|
7
|
|
|
class CurlWrapperUnitTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* URL for testing purposes |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $url = 'http://google.com'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Testing GET requests |
19
|
|
|
*/ |
20
|
|
|
public function testGetRequest() |
21
|
|
|
{ |
22
|
|
|
// test body |
23
|
|
|
list ($body, $code) = CurlWrapper::sendRequest($this->url, [], 'GET'); |
24
|
|
|
|
25
|
|
|
// assertions |
26
|
|
|
$this->assertStringContainsString('', $body, 'Invalid HTML was returned'); |
|
|
|
|
27
|
|
|
$this->assertEquals(301, $code, 'Invalid HTTP code'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Testing POST requests |
32
|
|
|
*/ |
33
|
|
|
public function testPostRequest() |
34
|
|
|
{ |
35
|
|
|
// test body |
36
|
|
|
list ($body, $code) = CurlWrapper::sendRequest($this->url, [], 'POST', [ |
37
|
|
|
'data' => 1 |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
// assertions |
41
|
|
|
$this->assertStringContainsString('', $body, 'Invalid HTML was returned'); |
|
|
|
|
42
|
|
|
$this->assertEquals(405, $code, 'Invalid HTTP code'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Testing JSON request |
47
|
|
|
*/ |
48
|
|
|
public function testJsonRequest(): void |
49
|
|
|
{ |
50
|
|
|
// test body |
51
|
|
|
list ($body, $code) = CurlWrapper::sendRequest($this->url, [ |
52
|
|
|
'Content-type: application/json' |
53
|
|
|
], 'POST', [ |
54
|
|
|
'data' => 1 |
55
|
|
|
]); |
56
|
|
|
$body = json_decode($body, false); |
|
|
|
|
57
|
|
|
|
58
|
|
|
// assertions |
59
|
|
|
$this->assertEquals(405, $code); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|