Skip to content

Commit

Permalink
Fixed tests which require a webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
francisbesset committed Jul 24, 2013
1 parent 82d4268 commit 7b613a7
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 361 deletions.
55 changes: 55 additions & 0 deletions Tests/AbstractWebserverTest.php
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 <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* 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 francis.besset@gmail.com <francis.besset@gmail.com>
*/
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);
}
}
120 changes: 22 additions & 98 deletions Tests/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,167 +17,91 @@
/**
* @author Andreas Schamberger <mail@andreass.net>
*/
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();
}
}
Loading

0 comments on commit 7b613a7

Please sign in to comment.