1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @filesource RequestTest.php |
5
|
|
|
* @created 13.02.2016 |
6
|
|
|
* @package chillerlan\TinyCurlTest |
7
|
|
|
* @author Smiley <[email protected]> |
8
|
|
|
* @copyright 2016 Smiley |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace chillerlan\TinyCurlTest; |
13
|
|
|
|
14
|
|
|
use chillerlan\TinyCurl\Request; |
15
|
|
|
use chillerlan\TinyCurl\RequestOptions; |
16
|
|
|
use chillerlan\TinyCurl\Response\Response; |
17
|
|
|
use chillerlan\TinyCurl\URL; |
18
|
|
|
use stdClass; |
19
|
|
|
|
20
|
|
|
class RequestTest extends \PHPUnit_Framework_TestCase{ |
21
|
|
|
|
22
|
|
|
const GW2_APIKEY = '39519066-20B0-5545-9AE2-71109410A2CAF66348DA-50F7-4ACE-9363-B38FD8EE1881'; |
23
|
|
|
const GW2_ACC_ID = 'A9EAD53E-4157-E111-BBF3-78E7D1936222'; |
24
|
|
|
const GW2_GUILD = '75FD83CF-0C45-4834-BC4C-097F93A487AF'; |
25
|
|
|
const GW2_CHARS = 'Skin Receiver'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \chillerlan\TinyCurl\Request |
29
|
|
|
*/ |
30
|
|
|
protected $requestWithCA; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \chillerlan\TinyCurl\Request |
34
|
|
|
*/ |
35
|
|
|
protected $requestNoCA; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \chillerlan\TinyCurl\Response\ResponseInterface |
39
|
|
|
*/ |
40
|
|
|
protected $response; |
41
|
|
|
|
42
|
|
View Code Duplication |
protected function setUp(){ |
|
|
|
|
43
|
|
|
|
44
|
|
|
$co = [ |
45
|
|
|
CURLOPT_HTTPHEADER => ['Authorization: Bearer '.self::GW2_APIKEY] |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$o1 = new RequestOptions; |
49
|
|
|
$o1->curl_options = $co; |
50
|
|
|
$o1->ca_info = __DIR__.'/test-cacert.pem'; |
51
|
|
|
$this->requestWithCA = new Request($o1); |
52
|
|
|
|
53
|
|
|
$o2 = new RequestOptions; |
54
|
|
|
$o2->curl_options = $co; |
55
|
|
|
$this->requestNoCA = new Request($o2); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testInstanceWithoutArgsCoverage(){ |
59
|
|
|
$this->assertInstanceOf(Request::class, new Request); // HA HA. |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function fetchDataProvider(){ |
63
|
|
|
return [ |
64
|
|
|
['https://api.guildwars2.com/v2/account', []], |
65
|
|
|
['https://api.guildwars2.com/v2/account?lang=de', []], |
66
|
|
|
['https://api.guildwars2.com/v2/account?lang=de', ['lang' => 'fr']], |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @dataProvider fetchDataProvider |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function testFetchWithCA($url, array $params){ |
|
|
|
|
74
|
|
|
$this->response = $this->requestWithCA->fetch(new URL($url, $params)); |
75
|
|
|
|
76
|
|
|
$this->assertEquals(0, $this->response->error->code); |
|
|
|
|
77
|
|
|
$this->assertEquals(200, $this->response->info->http_code); |
|
|
|
|
78
|
|
|
$this->assertEquals('*', $this->response->headers->{'access-control-allow-origin'}); |
|
|
|
|
79
|
|
|
$this->assertEquals(self::GW2_ACC_ID, $this->response->json->id); |
|
|
|
|
80
|
|
|
$this->assertEquals('application/json; charset=utf-8', $this->response->body->content_type); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @dataProvider fetchDataProvider |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function testFetchNoCA($url, array $params){ |
|
|
|
|
87
|
|
|
$this->response = $this->requestNoCA->fetch(new URL($url, $params)); |
88
|
|
|
|
89
|
|
|
$this->assertEquals(0, $this->response->error->code); |
|
|
|
|
90
|
|
|
$this->assertEquals(200, $this->response->info->http_code); |
|
|
|
|
91
|
|
|
$this->assertEquals('*', $this->response->headers->{'access-control-allow-origin'}); |
|
|
|
|
92
|
|
|
$this->assertEquals(self::GW2_ACC_ID, $this->response->json->id); |
|
|
|
|
93
|
|
|
$this->assertEquals('application/json; charset=utf-8', $this->response->body->content_type); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @expectedException \chillerlan\TinyCurl\RequestException |
98
|
|
|
* @expectedExceptionMessage $url |
99
|
|
|
*/ |
100
|
|
|
public function testFetchUrlSchemeException(){ |
101
|
|
|
$this->requestWithCA->fetch(new URL('htps://whatever.wat')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @expectedException \chillerlan\TinyCurl\Response\ResponseException |
106
|
|
|
* @expectedExceptionMessage $curl |
107
|
|
|
*/ |
108
|
|
|
public function testResponseNoCurlException(){ |
109
|
|
|
$this->response = new Response(null); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @expectedException \chillerlan\TinyCurl\Response\ResponseException |
114
|
|
|
* @expectedExceptionMessage !$property: foobar |
115
|
|
|
*/ |
116
|
|
|
public function testResponseGetMagicFieldException(){ |
117
|
|
|
var_dump($this->requestWithCA->fetch(new URL('https://api.guildwars2.com/v2/build'))->foobar); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.