Skip to content

Commit 80f7218

Browse files
committed
Increase code coverage.
1 parent 6850c9d commit 80f7218

File tree

4 files changed

+216
-35
lines changed

4 files changed

+216
-35
lines changed

src/main/php/Gomoob/Pushwoosh/Curl/CurlRequest.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
*/
1818
class CurlRequest implements ICurlRequest
1919
{
20-
// @codingStandardsIgnoreStart
21-
/**
22-
* A regular expression used to validate URLs.
23-
*
24-
* @see https://mathiasbynens.be/demo/url-regex
25-
* @see https://gist.github.com/dperini/729294
26-
*/
27-
const URL_REGEX = '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(localhost)|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/\S*)?$_iuS';
28-
// @codingStandardsIgnoreEnd
20+
// @codingStandardsIgnoreStart
21+
/**
22+
* A regular expression used to validate URLs.
23+
*
24+
* @see https://mathiasbynens.be/demo/url-regex
25+
* @see https://gist.github.com/dperini/729294
26+
*/
27+
const URL_REGEX = '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(localhost)|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/\S*)?$_iuS';
28+
// @codingStandardsIgnoreEnd
2929

3030
/**
3131
* The current cURL handle.
@@ -43,15 +43,7 @@ class CurlRequest implements ICurlRequest
4343
*/
4444
public function __construct($url = null)
4545
{
46-
// If a URL is provided
47-
if (isset($url)) {
48-
// The provided URL must be valid
49-
if (!$this->isUrlValid($url)) {
50-
throw new \Exception('Invalid URL provided \'' . $url . '\' !', -1, null);
51-
}
52-
53-
$this->init($url);
54-
}
46+
$this->init($url);
5547
}
5648

5749
/**
@@ -90,24 +82,33 @@ public function init($url = null)
9082
$this->close();
9183
}
9284

93-
$this->ch = curl_init($url);
85+
// If a URL is provided
86+
if (isset($url)) {
87+
// The provided URL must be valid
88+
if (!$this->isUrlValid($url)) {
89+
throw new \Exception('Invalid URL provided \'' . $url . '\' !', -1, null);
90+
}
91+
92+
$this->ch = curl_init($url);
93+
}
94+
9495
return $this->ch;
9596
}
9697

9798
/**
9899
* {@inheritdoc}
99100
*/
100-
public function getInfo($opt = 0)
101+
public function getInfo($opt = CURLINFO_EFFECTIVE_URL)
101102
{
102-
return curl_getinfo($this->ch, $opt);
103+
return curl_getinfo($this->getCh(), $opt);
103104
}
104105

105106
/**
106107
* {@inheritdoc}
107108
*/
108109
public function setOpt($option, $value)
109110
{
110-
return curl_setopt($this->ch, $option, $value);
111+
return curl_setopt($this->getCh(), $option, $value);
111112
}
112113

113114
/**

src/main/php/Gomoob/Pushwoosh/Curl/ICurlRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function exec();
111111
* Note that private data is not included in the associative array and must be retrieved individually with
112112
* the CURLINFO_PRIVATE option.
113113
*/
114-
public function getInfo($opt = 0);
114+
public function getInfo($opt = CURLINFO_EFFECTIVE_URL0);
115115

116116
/**
117117
* Set an option for a cURL transfer.

src/test/php/Gomoob/Pushwoosh/Client/PushwooshMockTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* gomoob/php-pushwoosh
55
*
6-
* @copyright Copyright (c) 2014, GOMOOB SARL (http://gomoob.com)
6+
* @copyright Copyright (c) 2017, GOMOOB SARL (http://gomoob.com)
77
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE.md file)
88
*/
99
namespace Gomoob\Pushwoosh\Client;
@@ -32,7 +32,7 @@ class PushwooshMockTest extends TestCase
3232
/**
3333
* Test method for the `clear()` function.
3434
*
35-
* @group PushwooshTest.clear
35+
* @group PushwooshMockTest.testClear
3636
*/
3737
public function testClear()
3838
{
@@ -59,7 +59,7 @@ public function testClear()
5959
/**
6060
* Test method for the `createMessage(CreateMessageRequest $createMessageRequest)` function.
6161
*
62-
* @group PushwooshTest.createMessage
62+
* @group PushwooshMockTest.testCreateMessage
6363
*/
6464
public function testCreateMessage()
6565
{
@@ -89,7 +89,7 @@ public function testCreateMessage()
8989
/**
9090
* Test method for the `createTargetedMessage(CreateTargetedMessageRequest $createTargetedMessageRequest)` function.
9191
*
92-
* @group PushwooshTest.createTargetedMessage
92+
* @group PushwooshMockTest.testCreateTargetedMessage
9393
*/
9494
public function testCreateTargetedMessage()
9595
{
@@ -113,7 +113,7 @@ public function testCreateTargetedMessage()
113113
/**
114114
* Test method for the `deleteMessage(DeleteMessageRequest $deleteMessageRequest)` function.
115115
*
116-
* @group PushwooshTest.deleteMessage
116+
* @group PushwooshMockTest.testDeleteMessage
117117
*/
118118
public function testDeleteMessage()
119119
{
@@ -139,7 +139,7 @@ public function testDeleteMessage()
139139
/**
140140
* Test method for the `getNearestZone(GetNearestZoneRequest $getNearestZoneRequest)` function.
141141
*
142-
* @group PushwooshTest.getNearestZone
142+
* @group PushwooshMockTest.testGetNearestZone
143143
*/
144144
public function testGetNearestZone()
145145
{
@@ -207,7 +207,7 @@ public function testGetSetAuth()
207207
/**
208208
* Test method for the `getTags(GetTagsRequest $getTagsRequest)` function.
209209
*
210-
* @group PushwooshTest.getTags
210+
* @group PushwooshMockTest.testGetTags
211211
*/
212212
public function testGetTags()
213213
{
@@ -239,7 +239,7 @@ public function testGetTags()
239239
/**
240240
* Test method for the `pushStat(PushStatRequest $pushStatRequest)` function.
241241
*
242-
* @group PushwooshTest.pushStat
242+
* @group PushwooshMockTest.testPushStat
243243
*/
244244
public function testPushStat()
245245
{
@@ -265,7 +265,7 @@ public function testPushStat()
265265
/**
266266
* Test method for the `registerDevice(RegisterDeviceRequest $registerDeviceRequest)` function.
267267
*
268-
* @group PushwooshTest.registerDevice
268+
* @group PushwooshMockTest.testRegisterDevice
269269
*/
270270
public function testRegisterDevice()
271271
{
@@ -291,7 +291,7 @@ public function testRegisterDevice()
291291
/**
292292
* Test method for the `setBadge(SetBadgeRequest $setBadgeRequest)` function.
293293
*
294-
* @group PushwooshTest.setBadge
294+
* @group PushwooshMockTest.testSetBadge
295295
*/
296296
public function testSetBadge()
297297
{
@@ -317,7 +317,7 @@ public function testSetBadge()
317317
/**
318318
* Test method for the `setTags(SetTagsRequest $setTagsRequest)` function.
319319
*
320-
* @group PushwooshTest.setTags
320+
* @group PushwooshMockTest.testSetTags
321321
*/
322322
public function testSetTags()
323323
{
@@ -343,7 +343,7 @@ public function testSetTags()
343343
/**
344344
* Test method for the `unregisterDevice(UnregisterDeviceRequest $unregisterDeviceRequest)` function.
345345
*
346-
* @group PushwooshTest.unregisterDevice
346+
* @group PushwooshMockTest.testUnregisterDevice
347347
*/
348348
public function testUnregisterDevice()
349349
{
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?php
2+
3+
/**
4+
* gomoob/php-pushwoosh
5+
*
6+
* @copyright Copyright (c) 2017, GOMOOB SARL (http://gomoob.com)
7+
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE.md file)
8+
*/
9+
namespace Gomoob\Pushwoosh\Curl;
10+
11+
use PHPUnit\Framework\TestCase;
12+
13+
/**
14+
* Test case used to test the `CurlRequest` class.
15+
*
16+
* @author Baptiste GAILLARD (baptiste.gaillard@gomoob.com)
17+
* @group CurlRequestTest
18+
*/
19+
class CurlRequestTest extends TestCase
20+
{
21+
/**
22+
* Test method for the `__construct($url = null)` function.
23+
*
24+
* @group CurlRequestTest.testConstruct
25+
*/
26+
public function testConstruct()
27+
{
28+
// Test with no URL provided
29+
$curlRequest = new CurlRequest();
30+
$this->assertNotNull($curlRequest);
31+
32+
// Test with a valid URL provided
33+
$curlRequest = new CurlRequest('https://www.gomoob.com');
34+
$this->assertNotNull($curlRequest);
35+
36+
// Test with a bad URL provided
37+
try {
38+
new CurlRequest('bad');
39+
$this->fail('Must have thrown an Exception !');
40+
} catch (\Exception $ex) {
41+
$this->assertSame('Invalid URL provided \'bad\' !', $ex->getMessage());
42+
}
43+
}
44+
45+
/**
46+
* Test method for the `close()` function.
47+
*
48+
* @group CurlRequestTest.testClose
49+
*/
50+
public function testClose()
51+
{
52+
// Test with a CURL request without handle
53+
$curlRequest = new CurlRequest();
54+
55+
try {
56+
$curlRequest->close();
57+
$this->fail('Must have thrown an Exception !');
58+
} catch (\Exception $ex) {
59+
$this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage());
60+
}
61+
62+
// Test with a CURL request having a handle
63+
$curlRequest = new CurlRequest('https://www.gomoob.com');
64+
$curlRequest->close();
65+
}
66+
67+
/**
68+
* Test method for the `error()` function.
69+
*
70+
* @group CurlRequestTest.testError
71+
*/
72+
public function testError()
73+
{
74+
// Test with a CURL request without handle
75+
$curlRequest = new CurlRequest();
76+
77+
try {
78+
$curlRequest->error();
79+
$this->fail('Must have thrown an Exception !');
80+
} catch (\Exception $ex) {
81+
$this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage());
82+
}
83+
84+
// Test with a CURL request having a handle
85+
$curlRequest = new CurlRequest('https://www.gomoob.com');
86+
$error = $curlRequest->error();
87+
$this->assertSame('', $error);
88+
}
89+
90+
/**
91+
* Test method for the `exec()` function.
92+
*
93+
* @group CurlRequestTest.testExec
94+
*/
95+
public function testExec()
96+
{
97+
// Test with a CURL request without handle
98+
$curlRequest = new CurlRequest();
99+
100+
try {
101+
$curlRequest->exec();
102+
$this->fail('Must have thrown an Exception !');
103+
} catch (\Exception $ex) {
104+
$this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage());
105+
}
106+
107+
// Test with a CURL request having a handle
108+
$curlRequest = new CurlRequest('https://www.gomoob.com');
109+
110+
// see: http://curl.haxx.se/docs/sslcerts.html
111+
$curlRequest->setOpt(CURLOPT_RETURNTRANSFER, true);
112+
$curlRequest->setOpt(CURLOPT_CAINFO, __DIR__ . '/../../../../../main/resources/cacert.pem');
113+
$curlRequest->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
114+
$curlRequest->setOpt(CURLOPT_SSL_VERIFYPEER, true);
115+
116+
$result = $curlRequest->exec();
117+
$this->assertRegexp('/GoMoob/', $result);
118+
}
119+
120+
/**
121+
* Test method for the `init($url = null)` function.
122+
*
123+
* @group CurlRequestTest.testInit
124+
*/
125+
public function testInit()
126+
{
127+
// Test with no URL provided and no CURL handle initilized after construct
128+
$curlRequest = new CurlRequest();
129+
$curlRequest->init();
130+
131+
// Test with no URL provided and a CURL handle initilized after construct
132+
$curlRequest = new CurlRequest('https://www.gomoob.com');
133+
$curlRequest->init();
134+
135+
// Test with a bad URL provided
136+
try {
137+
$curlRequest->init('bad');
138+
$this->fail('Must have thrown an Exception !');
139+
} catch (\Exception $ex) {
140+
$this->assertSame('Invalid URL provided \'bad\' !', $ex->getMessage());
141+
}
142+
143+
// Test with a good URL provided
144+
$curlRequest->init('https://www.gomoob.com');
145+
}
146+
147+
/**
148+
* Test method for the `getInfo()` function.
149+
*
150+
* @group CurlRequestTest.testGetInfo
151+
*/
152+
public function testGetInfo()
153+
{
154+
// Test with a CURL request without handle
155+
$curlRequest = new CurlRequest();
156+
157+
try {
158+
$curlRequest->getInfo();
159+
$this->fail('Must have thrown an Exception !');
160+
} catch (\Exception $ex) {
161+
$this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage());
162+
}
163+
164+
// Test with no parameter provided
165+
$curlRequest = new CurlRequest('https://www.gomoob.com');
166+
167+
// see: http://curl.haxx.se/docs/sslcerts.html
168+
$curlRequest->setOpt(CURLOPT_RETURNTRANSFER, true);
169+
$curlRequest->setOpt(CURLOPT_CAINFO, __DIR__ . '/../../../../../main/resources/cacert.pem');
170+
$curlRequest->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
171+
$curlRequest->setOpt(CURLOPT_SSL_VERIFYPEER, true);
172+
173+
$result = $curlRequest->getInfo();
174+
$this->assertSame('https://www.gomoob.com', $result);
175+
176+
// Test with a parameter
177+
$result = $curlRequest->getInfo(CURLINFO_LOCAL_PORT);
178+
$this->assertSame(0, $result);
179+
}
180+
}

0 commit comments

Comments
 (0)