From 02e2bf73cdc127dbb079b71ae8d27d4499c831c4 Mon Sep 17 00:00:00 2001 From: gggeek Date: Thu, 26 Jan 2023 16:33:26 +0000 Subject: [PATCH] refactor tests to reduce duplication and clean up output --- tests/01CharsetTest.php | 29 +++++--------- tests/02ValueTest.php | 32 +-------------- tests/03MessagesTest.php | 33 +-------------- tests/04ParsingTest.php | 33 +-------------- tests/06EncoderTest.php | 33 +-------------- tests/07ClientTest.php | 29 ++------------ tests/08ServerTest.php | 75 ++--------------------------------- tests/09HTTPTest.php | 5 --- tests/10DemofilesTest.php | 13 +----- tests/11DebuggerTest.php | 9 ----- tests/12ExtraFilesTest.php | 9 ----- tests/LogAwareTestCase.php | 60 ++++++++++++++++++++++++++++ tests/ServerAwareTestCase.php | 65 ++++++++++++++++++++++++++++++ tests/WebTestCase.php | 63 ++++++----------------------- 14 files changed, 164 insertions(+), 324 deletions(-) create mode 100644 tests/LogAwareTestCase.php create mode 100644 tests/ServerAwareTestCase.php diff --git a/tests/01CharsetTest.php b/tests/01CharsetTest.php index a1768440..70acb8f5 100644 --- a/tests/01CharsetTest.php +++ b/tests/01CharsetTest.php @@ -21,27 +21,12 @@ use PhpXmlRpc\Helper\Charset; */ class CharsetTest extends PhpXmlRpc_PolyfillTestCase { - // Consolas font should render these properly + // Consolas font should render these properly. Of course this file is encoded in UTF-8 protected $runes = "ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ"; protected $greek = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ"; protected $russian = "Река неслася; бедный чёлн"; protected $chinese = "我能吞下玻璃而不伤身体。"; - protected $latinString; - - /// @todo move to usage of a dataProvider and create the latinString there - protected function set_up() - { - // construct a latin string with all chars (except control ones) - $this->latinString = "\n\r\t"; - for($i = 32; $i < 127; $i++) { - $this->latinString .= chr($i); - } - for($i = 160; $i < 256; $i++) { - $this->latinString .= chr($i); - } - } - protected function utf8ToLatin1($data) { return Charset::instance()->encodeEntities( @@ -62,10 +47,18 @@ class CharsetTest extends PhpXmlRpc_PolyfillTestCase public function testUtf8ToLatin1All() { + $latinString = "\n\r\t"; + for($i = 32; $i < 127; $i++) { + $latinString .= chr($i); + } + for($i = 160; $i < 256; $i++) { + $latinString .= chr($i); + } + // the warning suppression is due to utf8_encode being deprecated in php 8.2 - $string = @utf8_encode($this->latinString); + $string = @utf8_encode($latinString); $encoded = $this->utf8ToLatin1($string); - $this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $this->latinString), $encoded); + $this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $latinString), $encoded); } public function testUtf8ToLatin1EuroSymbol() diff --git a/tests/02ValueTest.php b/tests/02ValueTest.php index ffd8b1d6..73ba4b76 100644 --- a/tests/02ValueTest.php +++ b/tests/02ValueTest.php @@ -1,41 +1,13 @@ args = argParser::getArgs(); - if ($this->args['DEBUG'] == 1) - ob_start(); - } - - protected function tear_down() - { - if ($this->args['DEBUG'] != 1) - return; - $out = ob_get_clean(); - $status = $this->getStatus(); - if ($status == BaseTestRunner::STATUS_ERROR - || $status == BaseTestRunner::STATUS_FAILURE) { - echo $out; - } - } - public function testMinusOneString() { $v = new xmlrpcval('-1'); diff --git a/tests/03MessagesTest.php b/tests/03MessagesTest.php index b6c4a3fa..cb4a70bb 100644 --- a/tests/03MessagesTest.php +++ b/tests/03MessagesTest.php @@ -1,41 +1,12 @@ args = argParser::getArgs(); - // hide parsing errors unless in debug mode - if ($this->args['DEBUG'] == 1) - ob_start(); - } - - protected function tear_down() - { - if ($this->args['DEBUG'] != 1) - return; - $out = ob_get_clean(); - $status = $this->getStatus(); - if ($status == BaseTestRunner::STATUS_ERROR - || $status == BaseTestRunner::STATUS_FAILURE) { - echo $out; - } - } - public function testSerializePHPValResponse() { $r = new \PhpXmlRpc\Response(array('hello' => 'world'), 0, '', 'phpvals'); diff --git a/tests/04ParsingTest.php b/tests/04ParsingTest.php index 2a5831af..50b2fe4e 100644 --- a/tests/04ParsingTest.php +++ b/tests/04ParsingTest.php @@ -1,43 +1,14 @@ args = argParser::getArgs(); - // hide parsing errors unless in debug mode - if ($this->args['DEBUG'] < 1) - ob_start(); - } - - protected function tear_down() - { - if ($this->args['DEBUG'] >= 1) - return; - $out = ob_get_clean(); - $status = $this->getStatus(); - if ($status == BaseTestRunner::STATUS_ERROR - || $status == BaseTestRunner::STATUS_FAILURE) { - echo $out; - } - } - protected function newRequest($methodName, $params = array()) { $msg = new xmlrpcmsg($methodName, $params); diff --git a/tests/06EncoderTest.php b/tests/06EncoderTest.php index 4323ed48..568256b1 100644 --- a/tests/06EncoderTest.php +++ b/tests/06EncoderTest.php @@ -1,13 +1,6 @@ args = argParser::getArgs(); - // hide parsing errors unless in debug mode - if ($this->args['DEBUG'] == 1) - ob_start(); - } - - protected function tear_down() - { - if ($this->args['DEBUG'] != 1) - return; - $out = ob_get_clean(); - $status = $this->getStatus(); - if ($status == BaseTestRunner::STATUS_ERROR - || $status == BaseTestRunner::STATUS_FAILURE) { - echo $out; - } - } - public function testEncodeArray() { $v = php_xmlrpc_encode(array()); diff --git a/tests/07ClientTest.php b/tests/07ClientTest.php index 34905030..6faac336 100644 --- a/tests/07ClientTest.php +++ b/tests/07ClientTest.php @@ -1,44 +1,21 @@ args = argParser::getArgs(); + parent::set_up(); $this->client = new xmlrpc_client('/NOTEXIST.php', $this->args['HTTPSERVER'], 80); $this->client->setDebug($this->args['DEBUG']); - - // in debug mode, the client will be very verbose. Avoid showing its output unless there are errors - if ($this->args['DEBUG'] >= 1) - ob_start(); - } - - protected function tear_down() - { - if ($this->args['DEBUG'] < 1) - return; - $out = ob_get_clean(); - $status = $this->getStatus(); - if ($status == BaseTestRunner::STATUS_ERROR - || $status == BaseTestRunner::STATUS_FAILURE) { - echo $out; - } } public function test404() diff --git a/tests/08ServerTest.php b/tests/08ServerTest.php index 40252d78..6f9f9765 100644 --- a/tests/08ServerTest.php +++ b/tests/08ServerTest.php @@ -1,22 +1,15 @@ testId = get_class($this) . '__' . $this->getName(); - - if ($result === NULL) { - $result = $this->createResult(); - } - - $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation(); - - parent::_run($result); - - if ($this->collectCodeCoverageInformation) { - $coverage = new 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 set_up() { - $this->args = argParser::getArgs(); + parent::set_up(); $server = explode(':', $this->args['HTTPSERVER']); if (count($server) > 1) { @@ -105,24 +54,6 @@ class ServerTest extends PhpXmlRpc_PolyfillTestCase $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['HTTPSERVER'] . preg_replace('|/tests/index\.php(\?.*)?|', '/tests/phpunit_coverage.php', $this->args['HTTPURI']); - - // in debug mode, the client will be very verbose. Avoid showing its output unless there are errors - if ($this->args['DEBUG'] >= 1) - ob_start(); - } - - protected function tear_down() - { - if ($this->args['DEBUG'] < 1) - return; - $out = ob_get_clean(); - $status = $this->getStatus(); - if ($status == BaseTestRunner::STATUS_ERROR - || $status == BaseTestRunner::STATUS_FAILURE) { - echo $out; - } } /** diff --git a/tests/09HTTPTest.php b/tests/09HTTPTest.php index 58a0b52a..ed4348ea 100644 --- a/tests/09HTTPTest.php +++ b/tests/09HTTPTest.php @@ -1,10 +1,5 @@ args = argParser::getArgs(); - - // assumes HTTPURI to be in the form /tests/index.php?etc... - $this->baseUrl = 'http://' . $this->args['HTTPSERVER'] . preg_replace('|\?.+|', '', $this->args['HTTPURI']); - $this->coverageScriptUrl = 'http://' . $this->args['HTTPSERVER'] . preg_replace('|/tests/index\.php(\?.*)?|', '/tests/phpunit_coverage.php', $this->args['HTTPURI']); - } - public function testVardemo() { $page = $this->request('?demo=vardemo.php'); @@ -84,7 +75,7 @@ class DemoFilesTest extends PhpXmlRpc_WebTestCase $this->assertStringContainsString('faultCode', $page); $this->assertRegexp('#10(5|3)#', $page); - $c = $this->getClient('?demo=server/codegen.php'); + $c = $this->newClient('?demo=server/codegen.php'); $r = $c->send(new \PhpXmlRpc\Request('CommentManager.getComments', array( new \PhpXmlRpc\Value('aCommentId') ))); @@ -97,7 +88,7 @@ class DemoFilesTest extends PhpXmlRpc_WebTestCase $this->assertStringContainsString('faultCode', $page); $this->assertRegexp('#10(5|3)#', $page); - $c = $this->getClient('?demo=server/discuss.php'); + $c = $this->newClient('?demo=server/discuss.php'); $r = $c->send(new \PhpXmlRpc\Request('discuss.addComment', array( new \PhpXmlRpc\Value('aCommentId'), diff --git a/tests/11DebuggerTest.php b/tests/11DebuggerTest.php index c5310a09..3844ccf3 100644 --- a/tests/11DebuggerTest.php +++ b/tests/11DebuggerTest.php @@ -7,15 +7,6 @@ include_once __DIR__ . '/WebTestCase.php'; */ class DebuggerTest extends PhpXmlRpc_WebTestCase { - public function set_up() - { - $this->args = argParser::getArgs(); - - // assumes HTTPURI to be in the form /tests/index.php?etc... - $this->baseUrl = 'http://' . $this->args['HTTPSERVER'] . preg_replace('|\?.+|', '', $this->args['HTTPURI']); - $this->coverageScriptUrl = 'http://' . $this->args['HTTPSERVER'] . preg_replace('|/tests/index\.php(\?.*)?|', '/tests/phpunit_coverage.php', $this->args['HTTPURI']); - } - public function testIndex() { $page = $this->request('?debugger=index.php'); diff --git a/tests/12ExtraFilesTest.php b/tests/12ExtraFilesTest.php index 034d3cea..a8228848 100644 --- a/tests/12ExtraFilesTest.php +++ b/tests/12ExtraFilesTest.php @@ -7,15 +7,6 @@ include_once __DIR__ . '/WebTestCase.php'; */ class ExtraFilesTest extends PhpXmlRpc_WebTestCase { - public function set_up() - { - $this->args = argParser::getArgs(); - - // assumes HTTPURI to be in the form /tests/index.php?etc... - $this->baseUrl = 'http://' . $this->args['HTTPSERVER'] . preg_replace('|\?.+|', '', $this->args['HTTPURI']); - $this->coverageScriptUrl = 'http://' . $this->args['HTTPSERVER'] . preg_replace('|/tests/index\.php(\?.*)?|', '/tests/phpunit_coverage.php', $this->args['HTTPURI']); - } - public function testBenchmark() { $page = $this->request('?extras=benchmark.php'); diff --git a/tests/LogAwareTestCase.php b/tests/LogAwareTestCase.php new file mode 100644 index 00000000..d0dec6c1 --- /dev/null +++ b/tests/LogAwareTestCase.php @@ -0,0 +1,60 @@ +args = argParser::getArgs(); + + if ($this->args['DEBUG'] == 0) { + $this->debugBuffer = ''; + $this->errorBuffer = ''; + \PhpXmlRpc\PhpXmlRpc::setLogger($this); + } + } + + protected function tear_down() + { + if ($this->args['DEBUG'] > 0) { + return; + } + + // reset the logger to the default + \PhpXmlRpc\PhpXmlRpc::setLogger(\PhpXmlRpc\Helper\Logger::instance()); + + $status = $this->getStatus(); + if ($status == BaseTestRunner::STATUS_ERROR + || $status == BaseTestRunner::STATUS_FAILURE) { + echo $this->buffer; + } + } + + // logger API + + public function debug($message, $context = array()) + { + $this->buffer .= $message . "\n"; + } + + public function error($message, $context = array()) + { + $this->buffer .= $message . "\n"; + } +} diff --git a/tests/ServerAwareTestCase.php b/tests/ServerAwareTestCase.php new file mode 100644 index 00000000..02bbb267 --- /dev/null +++ b/tests/ServerAwareTestCase.php @@ -0,0 +1,65 @@ +testId = get_class($this) . '__' . $this->getName(); + + if ($result === NULL) { + $result = $this->createResult(); + } + + $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation(); + + parent::_run($result); + + if ($this->collectCodeCoverageInformation) { + $coverage = new 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 set_up() + { + parent::set_up(); + + // assumes HTTPURI to be in the form /tests/index.php?etc... + $this->baseUrl = 'http://' . $this->args['HTTPSERVER'] . preg_replace('|\?.+|', '', $this->args['HTTPURI']); + $this->coverageScriptUrl = 'http://' . $this->args['HTTPSERVER'] . preg_replace('|/tests/index\.php(\?.*)?|', '/tests/phpunit_coverage.php', $this->args['HTTPURI']); + } +} diff --git a/tests/WebTestCase.php b/tests/WebTestCase.php index bf721b19..f2c9c414 100644 --- a/tests/WebTestCase.php +++ b/tests/WebTestCase.php @@ -1,57 +1,12 @@ testId = get_class($this) . '__' . $this->getName(); - - if ($result === NULL) { - $result = $this->createResult(); - } - - $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation(); - - parent::_run($result); - - if ($this->collectCodeCoverageInformation) { - $coverage = new 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; - } - - /** * @param string $path * @param string $method * @param string $payload @@ -94,13 +49,19 @@ abstract class PhpXmlRpc_WebTestCase extends PhpXmlRpc_PolyfillTestCase return $page; } - protected function getClient($path) + /** + * Build an xml-rpc client, tweaked if needed to collect code-coverage information of the server + * + * @param string $path + * @return \PhpXmlRpc\Client + */ + protected function newClient($path) { - $client = new xmlrpc_client($this->baseUrl . $path); + $client = new \PhpXmlRpc\Client($this->baseUrl . $path); if ($this->collectCodeCoverageInformation) { $client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId); } - // let's just assume that the client works fine for these tests, and avoid polluting output + // let's just assume that the client works fine for these tests, and avoid polluting output, even in debug mode //$client->setAcceptedCompression(false); //$client->setDebug($this->args['DEBUG']); return $client; -- 2.47.0