Use random ports for Client test to reduce false errors on Travis

This commit is contained in:
PJ Dietz 2015-01-21 13:49:17 -05:00
parent e256610680
commit bcaa0ee7b7
2 changed files with 26 additions and 9 deletions

View File

@ -11,8 +11,13 @@
verbose="true" verbose="true"
> >
<php> <php>
<!-- To override, "export PORT=8081" --> <!-- Port for testing the Client class. Set this to a port that is not used on the system.
<env name="PORT" value="8080" /> Use a range (e.g., 2000-9000) to randomly pick a port within the boundaries.
To override, "export PORT=8081"
-->
<env name="PORT" value="2000-9000" />
<!-- Port for testing Client class failuers. Set this to a port that is not used on the system. -->
<env name="FAIL_PORT" value="9001-9100" />
</php> </php>
<testsuites> <testsuites>
<testsuite name="WellRESTed Test Suite"> <testsuite name="WellRESTed Test Suite">

View File

@ -15,7 +15,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
public function testSendHttpMethod($method) public function testSendHttpMethod($method)
{ {
$host = "localhost"; $host = "localhost";
$port = getenv("PORT"); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/method.php"); $script = realpath(__DIR__ . "/sham-routers/method.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
@ -60,7 +60,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
public function testSendHttpHeaders($headerKey, $headerValue) public function testSendHttpHeaders($headerKey, $headerValue)
{ {
$host = "localhost"; $host = "localhost";
$port = getenv("PORT"); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/headers.php"); $script = realpath(__DIR__ . "/sham-routers/headers.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
@ -102,7 +102,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
public function testSendBody($body) public function testSendBody($body)
{ {
$host = "localhost"; $host = "localhost";
$port = getenv("PORT"); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/body.php"); $script = realpath(__DIR__ . "/sham-routers/body.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
@ -145,7 +145,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
public function testSendForm($form) public function testSendForm($form)
{ {
$host = "localhost"; $host = "localhost";
$port = getenv("PORT"); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/formFields.php"); $script = realpath(__DIR__ . "/sham-routers/formFields.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
@ -178,7 +178,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
public function testSetCustomCurlOptionsOnInstantiation() public function testSetCustomCurlOptionsOnInstantiation()
{ {
$host = "localhost"; $host = "localhost";
$port = getenv("PORT"); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/headers.php"); $script = realpath(__DIR__ . "/sham-routers/headers.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
@ -208,7 +208,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
public function testSetCustomCurlOptionsOnRequest() public function testSetCustomCurlOptionsOnRequest()
{ {
$host = "localhost"; $host = "localhost";
$port = getenv("PORT"); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/headers.php"); $script = realpath(__DIR__ . "/sham-routers/headers.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
@ -261,12 +261,24 @@ class ClientTest extends \PHPUnit_Framework_TestCase
public function curlErrorProvider() public function curlErrorProvider()
{ {
$port = $this->getRandomNumberInRange(getenv("FAIL_PORT"));
return [ return [
["http://localhost:9991", [ ["http://localhost:{$port}", [
CURLOPT_FAILONERROR, true, CURLOPT_FAILONERROR, true,
CURLOPT_TIMEOUT_MS, 10 CURLOPT_TIMEOUT_MS, 10
]], ]],
]; ];
} }
private function getRandomNumberInRange($range)
{
static $pattern = '/(\d+)\-(\d+)/';
if (preg_match($pattern, $range, $matches)) {
$lower = $matches[1];
$upper = $matches[2];
return rand($lower, $upper);
} else {
return $range;
}
}
} }