1 | <?php |
||
18 | class MultiRequestTest extends \PHPUnit_Framework_TestCase{ |
||
19 | |||
20 | protected function getURLs(){ |
||
21 | |||
22 | $ids = [ |
||
23 | [1,2,6,11,15,23,24,56,57,58,59,60,61,62,63,64,68,69,70,71,72,73,74,75,76], |
||
24 | [77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101], |
||
25 | ]; |
||
26 | |||
27 | $urls = []; |
||
28 | |||
29 | foreach($ids as $chunk){ |
||
30 | foreach(['de', 'en', 'es', 'fr', 'zh'] as $lang){ |
||
31 | $urls[] = 'lang='.$lang.'&ids='.implode(',', $chunk); |
||
32 | } |
||
33 | } |
||
34 | |||
35 | return $urls; |
||
36 | } |
||
37 | |||
38 | public function testInstance(){ |
||
39 | $options = new MultiRequestOptions; |
||
40 | $options->handler = MultiResponseHandlerTest::class; |
||
41 | $options->ca_info = __DIR__.'/test-cacert.pem'; |
||
42 | $options->base_url = 'https://api.guildwars2.com/v2/items?'; |
||
43 | $options->window_size = 3; |
||
44 | |||
45 | $request = new MultiRequest($options); |
||
46 | $request->fetch($this->getURLs()); |
||
47 | |||
48 | foreach($request->getResponseData() as $response){ |
||
49 | |||
50 | $this->assertEquals(0, $response->errorcode); |
||
51 | $this->assertEquals(200, $response->statuscode); |
||
52 | $this->assertEquals($response->content_length_header, $response->content_length_body); |
||
53 | $this->assertEquals('application/json; charset=utf-8', $response->content_type); |
||
54 | # var_dump($response->ids); |
||
55 | } |
||
56 | |||
57 | } |
||
58 | |||
59 | public function testInstanceWindowSize(){ |
||
60 | $options = new MultiRequestOptions; |
||
61 | $options->handler = MultiResponseHandlerTest::class; |
||
62 | $options->ca_info = __DIR__.'/test-cacert.pem'; |
||
63 | $options->base_url = 'https://api.guildwars2.com/v2/items?'; |
||
64 | $options->window_size = 30; |
||
65 | |||
66 | $request = new MultiRequest($options); |
||
67 | $request->fetch($this->getURLs()); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @expectedException \chillerlan\TinyCurl\RequestException |
||
72 | * @expectedExceptionMessage empty($urls) |
||
73 | */ |
||
74 | public function testFetchUrlEmptyException(){ |
||
75 | (new MultiRequest)->fetch([]); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @expectedException \chillerlan\TinyCurl\RequestException |
||
80 | * @expectedExceptionMessage !$this->options->handler |
||
81 | */ |
||
82 | public function testSetHandlerExistsException(){ |
||
83 | $options = new MultiRequestOptions; |
||
84 | $options->handler = 'foobar'; |
||
85 | |||
86 | new MultiRequest($options); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @expectedException \chillerlan\TinyCurl\RequestException |
||
91 | * @expectedExceptionMessage !is_a($handler) |
||
92 | */ |
||
93 | public function testSetHandlerImplementsException(){ |
||
94 | $options = new MultiRequestOptions; |
||
95 | $options->handler = stdClass::class; |
||
96 | |||
97 | new MultiRequest($options); |
||
98 | } |
||
99 | |||
100 | } |
||
101 |