-
Notifications
You must be signed in to change notification settings - Fork 37
/
CurlRequestTest.php
180 lines (154 loc) · 5.51 KB
/
CurlRequestTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* gomoob/php-pushwoosh
*
* @copyright Copyright (c) 2017, GOMOOB SARL (http://gomoob.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE.md file)
*/
namespace Gomoob\Pushwoosh\Curl;
use PHPUnit\Framework\TestCase;
/**
* Test case used to test the `CurlRequest` class.
*
* @author Baptiste GAILLARD (baptiste.gaillard@gomoob.com)
* @group CurlRequestTest
*/
class CurlRequestTest extends TestCase
{
/**
* Test method for the `__construct($url = null)` function.
*
* @group CurlRequestTest.testConstruct
*/
public function testConstruct()
{
// Test with no URL provided
$curlRequest = new CurlRequest();
$this->assertNotNull($curlRequest);
// Test with a valid URL provided
$curlRequest = new CurlRequest('https://www.gomoob.com');
$this->assertNotNull($curlRequest);
// Test with a bad URL provided
try {
new CurlRequest('bad');
$this->fail('Must have thrown an Exception !');
} catch (\Exception $ex) {
$this->assertSame('Invalid URL provided \'bad\' !', $ex->getMessage());
}
}
/**
* Test method for the `close()` function.
*
* @group CurlRequestTest.testClose
*/
public function testClose()
{
// Test with a CURL request without handle
$curlRequest = new CurlRequest();
try {
$curlRequest->close();
$this->fail('Must have thrown an Exception !');
} catch (\Exception $ex) {
$this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage());
}
// Test with a CURL request having a handle
$curlRequest = new CurlRequest('https://www.gomoob.com');
$curlRequest->close();
}
/**
* Test method for the `error()` function.
*
* @group CurlRequestTest.testError
*/
public function testError()
{
// Test with a CURL request without handle
$curlRequest = new CurlRequest();
try {
$curlRequest->error();
$this->fail('Must have thrown an Exception !');
} catch (\Exception $ex) {
$this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage());
}
// Test with a CURL request having a handle
$curlRequest = new CurlRequest('https://www.gomoob.com');
$error = $curlRequest->error();
$this->assertSame('', $error);
}
/**
* Test method for the `exec()` function.
*
* @group CurlRequestTest.testExec
*/
public function testExec()
{
// Test with a CURL request without handle
$curlRequest = new CurlRequest();
try {
$curlRequest->exec();
$this->fail('Must have thrown an Exception !');
} catch (\Exception $ex) {
$this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage());
}
// Test with a CURL request having a handle
$curlRequest = new CurlRequest('https://www.gomoob.com');
// see: http://curl.haxx.se/docs/sslcerts.html
$curlRequest->setOpt(CURLOPT_RETURNTRANSFER, true);
$curlRequest->setOpt(CURLOPT_CAINFO, __DIR__ . '/../../../../../main/resources/cacert.pem');
$curlRequest->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
$curlRequest->setOpt(CURLOPT_SSL_VERIFYPEER, true);
$result = $curlRequest->exec();
$this->assertRegexp('/GoMoob/', $result);
}
/**
* Test method for the `init($url = null)` function.
*
* @group CurlRequestTest.testInit
*/
public function testInit()
{
// Test with no URL provided and no CURL handle initilized after construct
$curlRequest = new CurlRequest();
$curlRequest->init();
// Test with no URL provided and a CURL handle initilized after construct
$curlRequest = new CurlRequest('https://www.gomoob.com');
$curlRequest->init();
// Test with a bad URL provided
try {
$curlRequest->init('bad');
$this->fail('Must have thrown an Exception !');
} catch (\Exception $ex) {
$this->assertSame('Invalid URL provided \'bad\' !', $ex->getMessage());
}
// Test with a good URL provided
$curlRequest->init('https://www.gomoob.com');
}
/**
* Test method for the `getInfo()` function.
*
* @group CurlRequestTest.testGetInfo
*/
public function testGetInfo()
{
// Test with a CURL request without handle
$curlRequest = new CurlRequest();
try {
$curlRequest->getInfo();
$this->fail('Must have thrown an Exception !');
} catch (\Exception $ex) {
$this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage());
}
// Test with no parameter provided
$curlRequest = new CurlRequest('https://www.gomoob.com');
// see: http://curl.haxx.se/docs/sslcerts.html
$curlRequest->setOpt(CURLOPT_RETURNTRANSFER, true);
$curlRequest->setOpt(CURLOPT_CAINFO, __DIR__ . '/../../../../../main/resources/cacert.pem');
$curlRequest->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
$curlRequest->setOpt(CURLOPT_SSL_VERIFYPEER, true);
$result = $curlRequest->getInfo();
$this->assertSame('https://www.gomoob.com', $result);
// Test with a parameter
$result = $curlRequest->getInfo(CURLINFO_LOCAL_PORT);
$this->assertSame(0, $result);
}
}