-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed tests which require a webserver
- Loading branch information
1 parent
82d4268
commit 7b613a7
Showing
6 changed files
with
344 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the BeSimpleSoapClient. | ||
* | ||
* (c) Christian Kerl <[email protected]> | ||
* (c) Francis Besset <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace BeSimple\SoapClient\Tests; | ||
|
||
use Symfony\Component\Process\PhpExecutableFinder; | ||
use Symfony\Component\Process\ProcessBuilder; | ||
|
||
/** | ||
* @author [email protected] <[email protected]> | ||
*/ | ||
abstract class AbstractWebServerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ProcessBuilder | ||
*/ | ||
static protected $webserver; | ||
static protected $websererPortLength; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
if (version_compare(PHP_VERSION, '5.4.0', '<')) { | ||
self::markTestSkipped('PHP Webserver is available from PHP 5.4'); | ||
} | ||
|
||
self::$webserver = ProcessBuilder::create(array( | ||
'exec', // used exec binary (https://github.com/symfony/symfony/issues/5759) | ||
(new PhpExecutableFinder())->find(), | ||
'-S', | ||
sprintf('localhost:%d', WEBSERVER_PORT), | ||
'-t', | ||
__DIR__.DIRECTORY_SEPARATOR.'Fixtures', | ||
))->getProcess(); | ||
|
||
self::$webserver->start(); | ||
usleep(100000); | ||
|
||
self::$websererPortLength = strlen(WEBSERVER_PORT); | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
self::$webserver->stop(0); | ||
usleep(100000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,167 +17,91 @@ | |
/** | ||
* @author Andreas Schamberger <[email protected]> | ||
*/ | ||
class CurlTest extends \PHPUnit_Framework_TestCase | ||
class CurlTest extends AbstractWebserverTest | ||
{ | ||
protected $webserverProcessId; | ||
|
||
protected function startPhpWebserver() | ||
{ | ||
$this->markTestSkipped('Start a local webserver is not the best practice'); | ||
|
||
$dir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'; | ||
if ('Windows' == substr(php_uname('s'), 0, 7)) { | ||
$powershellCommand = "\$app = start-process php.exe -ArgumentList '-S localhost:8000 -t ".$dir."' -WindowStyle 'Hidden' -passthru; Echo \$app.Id;"; | ||
$shellCommand = 'powershell -command "& {'.$powershellCommand.'}"'; | ||
} else { | ||
$shellCommand = "nohup php -S localhost:8000 -t ".$dir." &"; | ||
} | ||
$output = array(); | ||
exec($shellCommand, $output); | ||
$this->webserverProcessId = $output[0]; // pid is in first element | ||
} | ||
|
||
protected function stopPhpWebserver() | ||
{ | ||
if (!is_null($this->webserverProcessId)) { | ||
if ('Windows' == substr(php_uname('s'), 0, 7 )) { | ||
exec('TASKKILL /F /PID ' . $this->webserverProcessId); | ||
} else { | ||
exec('kill ' . $this->webserverProcessId); | ||
} | ||
$this->webserverProcessId = null; | ||
} | ||
} | ||
|
||
public function testExec() | ||
{ | ||
$this->startPhpWebserver(); | ||
|
||
$curl = new Curl(); | ||
|
||
$this->assertTrue($curl->exec('http://localhost:8000/curl.txt')); | ||
$this->assertTrue($curl->exec('http://localhost:8000/404.txt')); | ||
|
||
$this->stopPhpWebserver(); | ||
$this->assertTrue($curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT))); | ||
$this->assertTrue($curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT))); | ||
} | ||
|
||
public function testGetErrorMessage() | ||
{ | ||
$this->startPhpWebserver(); | ||
|
||
$curl = new Curl(); | ||
|
||
$curl->exec('http://unknown/curl.txt'); | ||
$this->assertEquals('Could not connect to host', $curl->getErrorMessage()); | ||
|
||
$curl->exec('xyz://localhost:8000/@404.txt'); | ||
$curl->exec(sprintf('xyz://localhost:%d/@404.txt', WEBSERVER_PORT)); | ||
$this->assertEquals('Unknown protocol. Only http and https are allowed.', $curl->getErrorMessage()); | ||
|
||
$curl->exec(''); | ||
$this->assertEquals('Unable to parse URL', $curl->getErrorMessage()); | ||
|
||
$this->stopPhpWebserver(); | ||
} | ||
|
||
public function testGetRequestHeaders() | ||
{ | ||
$this->startPhpWebserver(); | ||
|
||
$curl = new Curl(); | ||
|
||
$curl->exec('http://localhost:8000/curl.txt'); | ||
$this->assertEquals(136, strlen($curl->getRequestHeaders())); | ||
$curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT)); | ||
$this->assertEquals(132 + self::$websererPortLength, strlen($curl->getRequestHeaders())); | ||
|
||
$curl->exec('http://localhost:8000/404.txt'); | ||
$this->assertEquals(135, strlen($curl->getRequestHeaders())); | ||
|
||
$this->stopPhpWebserver(); | ||
$curl->exec(sprintf('http://localhost:%s/404.txt', WEBSERVER_PORT)); | ||
$this->assertEquals(131 + self::$websererPortLength, strlen($curl->getRequestHeaders())); | ||
} | ||
|
||
public function testGetResponse() | ||
{ | ||
$this->startPhpWebserver(); | ||
|
||
$curl = new Curl(); | ||
|
||
$curl->exec('http://localhost:8000/curl.txt'); | ||
$this->assertEquals(150, strlen($curl->getResponse())); | ||
$curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT)); | ||
$this->assertSame('OK', $curl->getResponseStatusMessage()); | ||
$this->assertEquals(145 + self::$websererPortLength, strlen($curl->getResponse())); | ||
|
||
$curl->exec('http://localhost:8000/404.txt'); | ||
$this->assertEquals(1282, strlen($curl->getResponse())); | ||
|
||
$this->stopPhpWebserver(); | ||
$curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT)); | ||
$this->assertSame('Not Found', $curl->getResponseStatusMessage()); | ||
} | ||
|
||
public function testGetResponseBody() | ||
{ | ||
$this->startPhpWebserver(); | ||
|
||
$curl = new Curl(); | ||
|
||
$curl->exec('http://localhost:8000/curl.txt'); | ||
$curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT)); | ||
$this->assertEquals('This is a testfile for cURL.', $curl->getResponseBody()); | ||
|
||
$this->stopPhpWebserver(); | ||
} | ||
|
||
public function testGetResponseContentType() | ||
{ | ||
$this->startPhpWebserver(); | ||
|
||
$curl = new Curl(); | ||
|
||
$curl->exec('http://localhost:8000/curl.txt'); | ||
$curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT)); | ||
$this->assertEquals('text/plain; charset=UTF-8', $curl->getResponseContentType()); | ||
|
||
$curl->exec('http://localhost:8000/404.txt'); | ||
$curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT)); | ||
$this->assertEquals('text/html; charset=UTF-8', $curl->getResponseContentType()); | ||
|
||
$this->stopPhpWebserver(); | ||
} | ||
|
||
public function testGetResponseHeaders() | ||
{ | ||
$this->startPhpWebserver(); | ||
|
||
$curl = new Curl(); | ||
|
||
$curl->exec('http://localhost:8000/curl.txt'); | ||
$this->assertEquals(122, strlen($curl->getResponseHeaders())); | ||
$curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT)); | ||
$this->assertEquals(117 + self::$websererPortLength, strlen($curl->getResponseHeaders())); | ||
|
||
$curl->exec('http://localhost:8000/404.txt'); | ||
$this->assertEquals(130, strlen($curl->getResponseHeaders())); | ||
|
||
$this->stopPhpWebserver(); | ||
$curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT)); | ||
$this->assertEquals(124 + self::$websererPortLength, strlen($curl->getResponseHeaders())); | ||
} | ||
|
||
public function testGetResponseStatusCode() | ||
{ | ||
$this->startPhpWebserver(); | ||
|
||
$curl = new Curl(); | ||
|
||
$curl->exec('http://localhost:8000/curl.txt'); | ||
$curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT)); | ||
$this->assertEquals(200, $curl->getResponseStatusCode()); | ||
|
||
$curl->exec('http://localhost:8000/404.txt'); | ||
$curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT)); | ||
$this->assertEquals(404, $curl->getResponseStatusCode()); | ||
|
||
$this->stopPhpWebserver(); | ||
} | ||
|
||
public function testGetResponseStatusMessage() | ||
{ | ||
$this->startPhpWebserver(); | ||
|
||
$curl = new Curl(); | ||
|
||
$curl->exec('http://localhost:8000/curl.txt'); | ||
$this->assertEquals('OK', $curl->getResponseStatusMessage()); | ||
|
||
$curl->exec('http://localhost:8000/404.txt'); | ||
$this->assertEquals('Not Found', $curl->getResponseStatusMessage()); | ||
|
||
$this->stopPhpWebserver(); | ||
} | ||
} |
Oops, something went wrong.