|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: jaredchu |
|
6
|
|
|
* Date: 31/05/2017 |
|
7
|
|
|
* Time: 10:14 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
use JC\JCRequest; |
|
11
|
|
|
|
|
12
|
|
|
class JCRequestTest extends PHPUnit_Framework_TestCase |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
private $baseUrl = 'https://httpbin.org'; |
|
15
|
|
|
private $params; |
|
16
|
|
|
private $headers; |
|
17
|
|
|
|
|
18
|
2 |
|
public function setUp() |
|
19
|
|
|
{ |
|
20
|
2 |
|
$this->params = [ |
|
21
|
2 |
|
'b' => 2, |
|
22
|
|
|
'c' => '3' |
|
23
|
2 |
|
]; |
|
24
|
2 |
|
$this->headers = [ |
|
25
|
2 |
|
'User-Agent' => 'Jared Chu', |
|
26
|
|
|
'Accept' => 'application/json' |
|
27
|
2 |
|
]; |
|
28
|
2 |
|
} |
|
29
|
|
|
|
|
30
|
1 |
View Code Duplication |
public function testGet() |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
1 |
|
$url = $this->baseUrl . '/get?a=1'; |
|
33
|
|
|
|
|
34
|
1 |
|
$response = JCRequest::get($url, $this->params, $this->headers); |
|
35
|
1 |
|
$this->assertEquals(200, $response->status()); |
|
36
|
|
|
|
|
37
|
1 |
|
$responseData = json_decode($response->body()); |
|
38
|
1 |
|
$this->assertEquals('https://httpbin.org/get?a=1&b=2&c=3', $responseData->url); |
|
39
|
1 |
|
$this->assertEquals(1, $responseData->args->a); |
|
40
|
1 |
|
$this->assertEquals(2, $responseData->args->b); |
|
41
|
1 |
|
$this->assertEquals(3, $responseData->args->c); |
|
42
|
|
|
|
|
43
|
1 |
|
$this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'}); |
|
44
|
1 |
|
$this->assertEquals('application/json', $responseData->headers->{'Accept'}); |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
1 |
View Code Duplication |
public function testPost() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
1 |
|
$url = $this->baseUrl . '/post?a=1'; |
|
50
|
|
|
|
|
51
|
1 |
|
$response = JCRequest::post($url, $this->params, $this->headers); |
|
52
|
1 |
|
$this->assertEquals(200, $response->status()); |
|
53
|
|
|
|
|
54
|
1 |
|
$responseData = json_decode($response->body()); |
|
55
|
1 |
|
$this->assertEquals('https://httpbin.org/post?a=1', $responseData->url); |
|
56
|
1 |
|
$this->assertEquals(1, $responseData->args->a); |
|
57
|
1 |
|
$this->assertEquals(2, $responseData->form->b); |
|
58
|
1 |
|
$this->assertEquals(3, $responseData->form->c); |
|
59
|
|
|
|
|
60
|
1 |
|
$this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'}); |
|
61
|
1 |
|
$this->assertEquals('application/json', $responseData->headers->{'Accept'}); |
|
62
|
|
|
} |
|
63
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.