X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=tests%2F3LocalhostTest.php;h=e73bf61f07619bb9f237602d665ff69e177f7d12;hb=ee97067e0c7de2fc25a0834672f27dcad4468631;hp=562dbb511ced20796e064c880a268ba96e59765c;hpb=048358caae836a0e0d6a28956acf80c84e480aa3;p=plcapi.git diff --git a/tests/3LocalhostTest.php b/tests/3LocalhostTest.php index 562dbb5..e73bf61 100644 --- a/tests/3LocalhostTest.php +++ b/tests/3LocalhostTest.php @@ -8,22 +8,26 @@ include_once __DIR__ . '/parse_args.php'; class LocalhostTest extends PHPUnit_Framework_TestCase { /** @var xmlrpc_client $client */ - public $client = null; - public $method = 'http'; - public $timeout = 10; - public $request_compression = null; - public $accepted_compression = ''; - public $args = array(); + protected $client = null; + protected $method = 'http'; + protected $timeout = 10; + protected $request_compression = null; + protected $accepted_compression = ''; + protected $args = array(); protected static $failed_tests = array(); + protected $testId; + /** @var boolean $collectCodeCoverageInformation */ + protected $collectCodeCoverageInformation; + protected $coverageScriptUrl; + public static function fail($message = '') { - // save in global var that this particular test has failed + // save in a static var that this particular test has failed // (but only if not called from subclass objects / multitests) if (function_exists('debug_backtrace') && strtolower(get_called_class()) == 'localhosttests') { - global $failed_tests; - $trace = debug_backtrace(); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); for ($i = 0; $i < count($trace); $i++) { if (strpos($trace[$i]['function'], 'test') === 0) { self::$failed_tests[$trace[$i]['function']] = true; @@ -36,8 +40,41 @@ class LocalhostTest extends PHPUnit_Framework_TestCase } /** - * @todo be smarter with setup, do not use global variables anymore + * Reimplemented to allow us to collect code coverage info from the target server. + * Code taken from PHPUnit_Extensions_Selenium2TestCase + * + * @param PHPUnit_Framework_TestResult $result + * @return PHPUnit_Framework_TestResult + * @throws Exception */ + public function run(PHPUnit_Framework_TestResult $result = NULL) + { + $this->testId = get_class($this) . '__' . $this->getName(); + + if ($result === NULL) { + $result = $this->createResult(); + } + + $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation(); + + parent::run($result); + + if ($this->collectCodeCoverageInformation) { + $coverage = new PHPUnit_Extensions_SeleniumCommon_RemoteCoverage( + $this->coverageScriptUrl, + $this->testId + ); + $result->getCodeCoverage()->append( + $coverage->get(), $this + ); + } + + // do not call this before to give the time to the Listeners to run + //$this->getStrategy()->endOfTest($this->session); + + return $result; + } + public function setUp() { $this->args = argParser::getArgs(); @@ -48,15 +85,35 @@ class LocalhostTest extends PHPUnit_Framework_TestCase } else { $this->client = new xmlrpc_client($this->args['URI'], $this->args['LOCALSERVER']); } - if ($this->args['DEBUG']) { - $this->client->setDebug($this->args['DEBUG']); - } + + $this->client->setDebug($this->args['DEBUG']); $this->client->request_compression = $this->request_compression; $this->client->accepted_compression = $this->accepted_compression; + + $this->coverageScriptUrl = 'http://' . $this->args['LOCALSERVER'] . '/' . str_replace( '/demo/server/server.php', 'tests/phpunit_coverage.php', $this->args['URI'] ); + + if ($this->args['DEBUG'] == 1) + ob_start(); } - public function send($msg, $errrorcode = 0, $return_response = false) + protected function tearDown() { + if ($this->args['DEBUG'] != 1) + return; + $out = ob_get_clean(); + $status = $this->getStatus(); + if ($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR + || $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) { + echo $out; + } + } + + protected function send($msg, $errrorcode = 0, $return_response = false) + { + if ($this->collectCodeCoverageInformation) { + $this->client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId); + } + $r = $this->client->send($msg, $this->timeout, $this->method); // for multicall, return directly array of responses if (is_array($r)) { @@ -64,8 +121,7 @@ class LocalhostTest extends PHPUnit_Framework_TestCase } if (is_array($errrorcode)) { $this->assertContains($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString()); - } - else { + } else { $this->assertEquals($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString()); } if (!$r->faultCode()) { @@ -107,6 +163,19 @@ class LocalhostTest extends PHPUnit_Framework_TestCase } } + public function testLatin1String() + { + $sendstring = + "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne"; + $f = 'examples.stringecho'. + $sendstring. + ''; + $v = $this->send($f); + if ($v) { + $this->assertEquals($sendstring, $v->scalarval()); + } + } + public function testAddingDoubles() { // note that rounding errors mean we @@ -574,6 +643,12 @@ And turned it into nylon'; if (!$r->faultCode()) { $v = $r->value(); $v = php_xmlrpc_decode($v); + + // take care for the extra cookie used for coverage collection + if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) { + unset($v['PHPUNIT_SELENIUM_TEST_ID']); + } + // on IIS and Apache getallheaders returns something slightly different... $this->assertEquals($v, $cookies); }