Skip to content

Commit

Permalink
Increase code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaillard committed Jun 19, 2017
1 parent 6850c9d commit 80f7218
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 35 deletions.
45 changes: 23 additions & 22 deletions src/main/php/Gomoob/Pushwoosh/Curl/CurlRequest.php
Expand Up @@ -17,15 +17,15 @@
*/
class CurlRequest implements ICurlRequest
{
// @codingStandardsIgnoreStart
/**
* A regular expression used to validate URLs.
*
* @see https://mathiasbynens.be/demo/url-regex
* @see https://gist.github.com/dperini/729294
*/
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';
// @codingStandardsIgnoreEnd
// @codingStandardsIgnoreStart
/**
* A regular expression used to validate URLs.
*
* @see https://mathiasbynens.be/demo/url-regex
* @see https://gist.github.com/dperini/729294
*/
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';
// @codingStandardsIgnoreEnd

/**
* The current cURL handle.
Expand All @@ -43,15 +43,7 @@ class CurlRequest implements ICurlRequest
*/
public function __construct($url = null)
{
// If a URL is provided
if (isset($url)) {
// The provided URL must be valid
if (!$this->isUrlValid($url)) {
throw new \Exception('Invalid URL provided \'' . $url . '\' !', -1, null);
}

$this->init($url);
}
$this->init($url);
}

/**
Expand Down Expand Up @@ -90,24 +82,33 @@ public function init($url = null)
$this->close();
}

$this->ch = curl_init($url);
// If a URL is provided
if (isset($url)) {
// The provided URL must be valid
if (!$this->isUrlValid($url)) {
throw new \Exception('Invalid URL provided \'' . $url . '\' !', -1, null);
}

$this->ch = curl_init($url);
}

return $this->ch;
}

/**
* {@inheritdoc}
*/
public function getInfo($opt = 0)
public function getInfo($opt = CURLINFO_EFFECTIVE_URL)
{
return curl_getinfo($this->ch, $opt);
return curl_getinfo($this->getCh(), $opt);
}

/**
* {@inheritdoc}
*/
public function setOpt($option, $value)
{
return curl_setopt($this->ch, $option, $value);
return curl_setopt($this->getCh(), $option, $value);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/Gomoob/Pushwoosh/Curl/ICurlRequest.php
Expand Up @@ -111,7 +111,7 @@ public function exec();
* Note that private data is not included in the associative array and must be retrieved individually with
* the CURLINFO_PRIVATE option.
*/
public function getInfo($opt = 0);
public function getInfo($opt = CURLINFO_EFFECTIVE_URL0);

/**
* Set an option for a cURL transfer.
Expand Down
24 changes: 12 additions & 12 deletions src/test/php/Gomoob/Pushwoosh/Client/PushwooshMockTest.php
Expand Up @@ -3,7 +3,7 @@
/**
* gomoob/php-pushwoosh
*
* @copyright Copyright (c) 2014, GOMOOB SARL (http://gomoob.com)
* @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\Client;
Expand Down Expand Up @@ -32,7 +32,7 @@ class PushwooshMockTest extends TestCase
/**
* Test method for the `clear()` function.
*
* @group PushwooshTest.clear
* @group PushwooshMockTest.testClear
*/
public function testClear()
{
Expand All @@ -59,7 +59,7 @@ public function testClear()
/**
* Test method for the `createMessage(CreateMessageRequest $createMessageRequest)` function.
*
* @group PushwooshTest.createMessage
* @group PushwooshMockTest.testCreateMessage
*/
public function testCreateMessage()
{
Expand Down Expand Up @@ -89,7 +89,7 @@ public function testCreateMessage()
/**
* Test method for the `createTargetedMessage(CreateTargetedMessageRequest $createTargetedMessageRequest)` function.
*
* @group PushwooshTest.createTargetedMessage
* @group PushwooshMockTest.testCreateTargetedMessage
*/
public function testCreateTargetedMessage()
{
Expand All @@ -113,7 +113,7 @@ public function testCreateTargetedMessage()
/**
* Test method for the `deleteMessage(DeleteMessageRequest $deleteMessageRequest)` function.
*
* @group PushwooshTest.deleteMessage
* @group PushwooshMockTest.testDeleteMessage
*/
public function testDeleteMessage()
{
Expand All @@ -139,7 +139,7 @@ public function testDeleteMessage()
/**
* Test method for the `getNearestZone(GetNearestZoneRequest $getNearestZoneRequest)` function.
*
* @group PushwooshTest.getNearestZone
* @group PushwooshMockTest.testGetNearestZone
*/
public function testGetNearestZone()
{
Expand Down Expand Up @@ -207,7 +207,7 @@ public function testGetSetAuth()
/**
* Test method for the `getTags(GetTagsRequest $getTagsRequest)` function.
*
* @group PushwooshTest.getTags
* @group PushwooshMockTest.testGetTags
*/
public function testGetTags()
{
Expand Down Expand Up @@ -239,7 +239,7 @@ public function testGetTags()
/**
* Test method for the `pushStat(PushStatRequest $pushStatRequest)` function.
*
* @group PushwooshTest.pushStat
* @group PushwooshMockTest.testPushStat
*/
public function testPushStat()
{
Expand All @@ -265,7 +265,7 @@ public function testPushStat()
/**
* Test method for the `registerDevice(RegisterDeviceRequest $registerDeviceRequest)` function.
*
* @group PushwooshTest.registerDevice
* @group PushwooshMockTest.testRegisterDevice
*/
public function testRegisterDevice()
{
Expand All @@ -291,7 +291,7 @@ public function testRegisterDevice()
/**
* Test method for the `setBadge(SetBadgeRequest $setBadgeRequest)` function.
*
* @group PushwooshTest.setBadge
* @group PushwooshMockTest.testSetBadge
*/
public function testSetBadge()
{
Expand All @@ -317,7 +317,7 @@ public function testSetBadge()
/**
* Test method for the `setTags(SetTagsRequest $setTagsRequest)` function.
*
* @group PushwooshTest.setTags
* @group PushwooshMockTest.testSetTags
*/
public function testSetTags()
{
Expand All @@ -343,7 +343,7 @@ public function testSetTags()
/**
* Test method for the `unregisterDevice(UnregisterDeviceRequest $unregisterDeviceRequest)` function.
*
* @group PushwooshTest.unregisterDevice
* @group PushwooshMockTest.testUnregisterDevice
*/
public function testUnregisterDevice()
{
Expand Down
180 changes: 180 additions & 0 deletions src/test/php/Gomoob/Pushwoosh/Curl/CurlRequestTest.php
@@ -0,0 +1,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);
}
}

0 comments on commit 80f7218

Please sign in to comment.