From bcaa0ee7b7a0f125b7802415108fd2483ec0fbac Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Wed, 21 Jan 2015 13:49:17 -0500 Subject: [PATCH] Use random ports for Client test to reduce false errors on Travis --- phpunit.xml.dist | 9 +++++++-- test/ClientTest.php | 26 +++++++++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7993f0b..017916a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,8 +11,13 @@ verbose="true" > - - + + + + diff --git a/test/ClientTest.php b/test/ClientTest.php index c3369f0..c215356 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -15,7 +15,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function testSendHttpMethod($method) { $host = "localhost"; - $port = getenv("PORT"); + $port = $this->getRandomNumberInRange(getenv("PORT")); $script = realpath(__DIR__ . "/sham-routers/method.php"); $server = new ShamServer($host, $port, $script); @@ -60,7 +60,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function testSendHttpHeaders($headerKey, $headerValue) { $host = "localhost"; - $port = getenv("PORT"); + $port = $this->getRandomNumberInRange(getenv("PORT")); $script = realpath(__DIR__ . "/sham-routers/headers.php"); $server = new ShamServer($host, $port, $script); @@ -102,7 +102,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function testSendBody($body) { $host = "localhost"; - $port = getenv("PORT"); + $port = $this->getRandomNumberInRange(getenv("PORT")); $script = realpath(__DIR__ . "/sham-routers/body.php"); $server = new ShamServer($host, $port, $script); @@ -145,7 +145,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function testSendForm($form) { $host = "localhost"; - $port = getenv("PORT"); + $port = $this->getRandomNumberInRange(getenv("PORT")); $script = realpath(__DIR__ . "/sham-routers/formFields.php"); $server = new ShamServer($host, $port, $script); @@ -178,7 +178,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function testSetCustomCurlOptionsOnInstantiation() { $host = "localhost"; - $port = getenv("PORT"); + $port = $this->getRandomNumberInRange(getenv("PORT")); $script = realpath(__DIR__ . "/sham-routers/headers.php"); $server = new ShamServer($host, $port, $script); @@ -208,7 +208,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function testSetCustomCurlOptionsOnRequest() { $host = "localhost"; - $port = getenv("PORT"); + $port = $this->getRandomNumberInRange(getenv("PORT")); $script = realpath(__DIR__ . "/sham-routers/headers.php"); $server = new ShamServer($host, $port, $script); @@ -261,12 +261,24 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function curlErrorProvider() { + $port = $this->getRandomNumberInRange(getenv("FAIL_PORT")); return [ - ["http://localhost:9991", [ + ["http://localhost:{$port}", [ CURLOPT_FAILONERROR, true, 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; + } + } }