1 | <?php |
||
19 | */ |
||
20 | class CurlTest extends \PHPUnit_Framework_TestCase |
||
21 | { |
||
22 | /** |
||
23 | * @expectedException \Jgut\Spiral\Exception\OptionException |
||
24 | */ |
||
25 | public function testGetterSetter() |
||
26 | { |
||
27 | $transport = new Curl; |
||
28 | |||
29 | static::assertFalse($transport->hasOption('fake_option')); |
||
30 | |||
31 | $options = [CURLOPT_VERBOSE => false]; |
||
32 | |||
33 | $transport->setOptions($options); |
||
34 | static::assertEquals(1, count($transport->getOptions())); |
||
35 | static::assertTrue($transport->hasOption(CURLOPT_VERBOSE)); |
||
36 | static::assertTrue($transport->hasOption(OptionFactory::build(CURLOPT_VERBOSE, false))); |
||
37 | static::assertEquals( |
||
38 | $options, |
||
39 | [$transport->getOptions()[0]->getOption() => $transport->getOptions()[0]->getValue()] |
||
40 | ); |
||
41 | |||
42 | $transport->removeOption('fake_option'); |
||
43 | $transport->removeOption(CURLOPT_VERBOSE); |
||
44 | $transport->removeOption(OptionFactory::build(CURLOPT_VERBOSE, false)); |
||
45 | static::assertFalse($transport->hasOption(CURLOPT_VERBOSE)); |
||
46 | |||
47 | $transport->setOption('fake_option'); |
||
48 | } |
||
49 | |||
50 | public function testDefaultCreation() |
||
51 | { |
||
52 | $transport = Curl::createFromDefaults(); |
||
53 | |||
54 | static::assertTrue($transport->hasOption(CURLOPT_VERBOSE)); |
||
55 | static::assertTrue($transport->hasOption('timeout')); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @expectedException \Jgut\Spiral\Exception\TransportException |
||
60 | * @expectedExceptionMessageRegExp /^Could( not|n't) resolve host/ |
||
61 | */ |
||
62 | public function testErrorRequest() |
||
63 | { |
||
64 | $transport = Curl::createFromDefaults(); |
||
65 | |||
66 | $transport->request(Transport::METHOD_HEAD, 'http://fake_made_up_web.com'); |
||
67 | } |
||
68 | |||
69 | public function testForgeAndInfo() |
||
70 | { |
||
71 | $transport = Curl::createFromDefaults(); |
||
72 | $transport->setOption('user_password', 'user:pass'); |
||
73 | |||
74 | static::assertNull($transport->responseInfo()); |
||
75 | |||
76 | $transport->request(Transport::METHOD_GET, 'http://www.php.net', ['Accept-Charset' => 'utf-8']); |
||
77 | |||
78 | static::assertInternalType('array', $transport->responseInfo()); |
||
79 | static::assertEquals(200, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
||
80 | |||
81 | $transport->close(); |
||
82 | } |
||
83 | |||
84 | public function testRequestWithVars() |
||
85 | { |
||
86 | $transport = Curl::createFromDefaults(); |
||
87 | |||
88 | $transport->request(Transport::METHOD_GET, 'http://www.php.net', [], ['var' => 'value']); |
||
89 | static::assertEquals(200, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
||
90 | |||
91 | $transport->request(Transport::METHOD_POST, 'http://www.php.net', [], ['var' => 'value']); |
||
92 | static::assertEquals(200, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
||
93 | |||
94 | $transport->close(); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param string $method |
||
99 | * @param string $shorthand |
||
100 | * @param int $expectedCode |
||
101 | * |
||
102 | * @dataProvider methodProvider |
||
103 | */ |
||
104 | public function testRequestMethods($method, $shorthand, $expectedCode) |
||
105 | { |
||
106 | $transport = Curl::createFromDefaults(); |
||
107 | |||
108 | $transport->request($method, 'http://www.php.net'); |
||
109 | static::assertEquals($expectedCode, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
||
110 | |||
111 | call_user_func([$transport, $shorthand], 'http://www.php.net'); |
||
112 | static::assertEquals($expectedCode, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
||
113 | |||
114 | $transport->close(); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Provide HTTP methods. |
||
119 | * |
||
120 | * @return array[] |
||
121 | */ |
||
122 | public function methodProvider() |
||
123 | { |
||
124 | return [ |
||
125 | [Transport::METHOD_OPTIONS, 'options', 200], |
||
126 | [Transport::METHOD_HEAD, 'head', 200], |
||
127 | [Transport::METHOD_GET, 'get', 200], |
||
128 | [Transport::METHOD_POST, 'post', 200], |
||
129 | [Transport::METHOD_PUT, 'put', 200], |
||
130 | [Transport::METHOD_DELETE, 'delete', 200], |
||
131 | [Transport::METHOD_PATCH, 'patch', 200], |
||
132 | ]; |
||
133 | } |
||
134 | } |
||
135 |