From 8ccaf4d728232a7537ab964885c526175cb20e09 Mon Sep 17 00:00:00 2001 From: gggeek Date: Tue, 15 Dec 2020 23:20:22 +0000 Subject: [PATCH] fix test coverage calculations --- .travis.yml | 7 ++-- demo/client/_prepend.php | 1 + demo/server/_prepend.php | 1 + phpunit.xml | 4 +- tests/3LocalhostTest.php | 5 +++ tests/4LocalhostMultiTest.php | 2 +- tests/7ExtraTest.php | 12 ++++++ tests/LocalFileTestCase.php | 8 +++- tests/ci/setup/setup_code_coverage.sh | 57 +++++++++++++++++++++++++++ tests/ci/setup/setup_php.sh | 11 +++--- tests/ci/setup/setup_php_travis.sh | 10 ++--- 11 files changed, 99 insertions(+), 19 deletions(-) create mode 100755 tests/ci/setup/setup_code_coverage.sh diff --git a/.travis.yml b/.travis.yml index 9d1f3c0..12d85d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,13 +46,12 @@ install: before_script: # Output what version of phpunit we got going - - ./vendor/bin/phpunit --version + #- ./vendor/bin/phpunit --version # Enable code coverage for a single version of php - - if [ "$TRAVIS_PHP_VERSION" = "7.4" ]; then export PHP_OPTS="-d zend_extension=xdebug.so -d xdebug.mode=coverage" && export PHPUNIT_OPTS="--coverage-clover=coverage.clover"; fi - - echo "php $PHP_OPTS ./vendor/phpunit/phpunit/phpunit $PHPUNIT_OPTS tests" + - if [ "$TRAVIS_PHP_VERSION" = "7.4" ]; then ./tests/ci/setup/setup_code_coverage.sh enable && export PHPUNIT_OPTS="--coverage-clover=coverage.clover"; fi script: - php $PHP_OPTS ./vendor/phpunit/phpunit/phpunit $PHPUNIT_OPTS tests + ./vendor/bin/phpunit -v $PHPUNIT_OPTS tests after_failure: # Troubleshoot test env build problems diff --git a/demo/client/_prepend.php b/demo/client/_prepend.php index e2a480f..0f65cd9 100644 --- a/demo/client/_prepend.php +++ b/demo/client/_prepend.php @@ -30,6 +30,7 @@ if (isset($_COOKIE['PHPUNIT_SELENIUM_TEST_ID']) && extension_loaded('xdebug')) { $GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] = '/tmp/phpxmlrpc_coverage'; if (!is_dir($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'])) { mkdir($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY']); + chmod($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'], 0777); } include_once __DIR__ . "/../../vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumCommon/prepend.php"; diff --git a/demo/server/_prepend.php b/demo/server/_prepend.php index 3963a32..8dbf6cf 100644 --- a/demo/server/_prepend.php +++ b/demo/server/_prepend.php @@ -23,6 +23,7 @@ if (isset($_COOKIE['PHPUNIT_SELENIUM_TEST_ID']) && extension_loaded('xdebug')) { $GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] = '/tmp/phpxmlrpc_coverage'; if (!is_dir($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'])) { mkdir($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY']); + chmod($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'], 0777); } include_once __DIR__ . "/../../vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumCommon/prepend.php"; diff --git a/phpunit.xml b/phpunit.xml index cdd5e2b..a4e4a19 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,9 @@ ./ - ./Resources + ./build + ./doc + ./extras ./tests ./vendor ./pakefile.php diff --git a/tests/3LocalhostTest.php b/tests/3LocalhostTest.php index 439b635..d513b09 100644 --- a/tests/3LocalhostTest.php +++ b/tests/3LocalhostTest.php @@ -30,6 +30,9 @@ class LocalhostTest extends PhpXmlRpc_PolyfillTestCase protected $collectCodeCoverageInformation; protected $coverageScriptUrl; + /** + * @todo instead of overriding run via _fail, implement PHPUnit_Framework_TestListener - IFF there is such an API portable across PHPUnit 5 to 9... + */ public static function _fail($message = '') { // save in a static var that this particular test has failed @@ -54,6 +57,8 @@ class LocalhostTest extends PhpXmlRpc_PolyfillTestCase * @param TestResult $result * @return TestResult * @throws Exception + * + * @todo instead of overriding run via _run, subclass PHPUnit_Extensions_TestDecorator - IFF there is such an API portable across PHPUnit 5 to 9... */ public function _run($result = NULL) { diff --git a/tests/4LocalhostMultiTest.php b/tests/4LocalhostMultiTest.php index 317e517..619e3ae 100644 --- a/tests/4LocalhostMultiTest.php +++ b/tests/4LocalhostMultiTest.php @@ -16,7 +16,7 @@ class LocalhostMultiTest extends LocalhostTest /** * Returns all test methods from the base class, except the ones which failed already * - * @todo reintroduce skipping of tests which failed when executed individually if test runs happen as separate processes + * @todo (re)introduce skipping of tests which failed when executed individually even if test runs happen as separate processes * @todo reintroduce skipping of tests within the loop */ public function getSingleHttpTestMethods() diff --git a/tests/7ExtraTest.php b/tests/7ExtraTest.php index 5770eca..48acf6b 100644 --- a/tests/7ExtraTest.php +++ b/tests/7ExtraTest.php @@ -4,6 +4,7 @@ include_once __DIR__ . '/LocalFileTestCase.php'; /** * Tests for php files in the 'extras' directory + * */ class ExtraTest extends PhpXmlRpc_LocalFileTestCase { @@ -16,6 +17,17 @@ class ExtraTest extends PhpXmlRpc_LocalFileTestCase $this->coverageScriptUrl = 'http://' . $this->args['HTTPSERVER'] . '/' . str_replace( '/demo/server/server.php', 'tests/phpunit_coverage.php', $this->args['HTTPURI'] ); } + /** + * @todo collect code coverage for this... + */ + public function testBenchmark() + { + $page = $this->request('benchmark.php'); + } + + /** + * @todo collect code coverage for this... + */ public function testVerifyCompat() { $page = $this->request('verify_compat.php'); diff --git a/tests/LocalFileTestCase.php b/tests/LocalFileTestCase.php index 13160c7..9cc660f 100644 --- a/tests/LocalFileTestCase.php +++ b/tests/LocalFileTestCase.php @@ -15,6 +15,12 @@ abstract class PhpXmlRpc_LocalFileTestCase extends PhpXmlRpc_PolyfillTestCase protected $collectCodeCoverageInformation; protected $coverageScriptUrl; + /** + * Reimplemented to allow us to collect code coverage info for the target php files executed via an http request. + * Code taken from PHPUnit_Extensions_Selenium2TestCase + * + * @todo instead of overriding run via _run, subclass PHPUnit_Extensions_TestDecorator - IFF there is such an API portable across PHPUnit 5 to 9... + */ public function _run($result = NULL) { $this->testId = get_class($this) . '__' . $this->getName(); @@ -61,7 +67,7 @@ abstract class PhpXmlRpc_LocalFileTestCase extends PhpXmlRpc_PolyfillTestCase } if ($this->collectCodeCoverageInformation) { - curl_setopt($ch, CURLOPT_COOKIE, 'PHPUNIT_SELENIUM_TEST_ID=true'); + curl_setopt($ch, CURLOPT_COOKIE, 'PHPUNIT_SELENIUM_TEST_ID='.$this->testId); } if ($this->args['DEBUG'] > 0) { curl_setopt($ch, CURLOPT_VERBOSE, 1); diff --git a/tests/ci/setup/setup_code_coverage.sh b/tests/ci/setup/setup_code_coverage.sh new file mode 100755 index 0000000..145de3d --- /dev/null +++ b/tests/ci/setup/setup_code_coverage.sh @@ -0,0 +1,57 @@ +#!/bin/sh + +# @todo add 'query' action +# @todo avoid reloading php-fpm if config did not change + +# Note: we have php set up either via phpenv (TRAVIS=true), Ubuntu packages (PHP_VERSION=default) or Sury packages. +# xdebug comes either at version 2 or 3 + +set -e + +if [ "$TRAVIS" != true ]; then + PHPCONFDIR_CLI=$(php -i | grep 'Scan this dir for additional .ini files' | sed 's|Scan this dir for additional .ini files => ||') + PHPCONFDIR_FPM=$(echo "$PHPCONFDIR_CLI" | sed 's|/cli/|/fpm/|') +fi + +enable_cc() { + if [ "$TRAVIS" = true ]; then + phpenv config-add tests/ci/config/codecoverage_xdebug.ini + + pkill php-fpm + ~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm + else + if [ -L "${PHPCONFDIR_CLI}/99-codecoverage_xdebug.ini" ]; then sudo rm "${PHPCONFDIR_CLI}/99-codecoverage_xdebug.ini"; fi + sudo ln -s $(realpath tests/ci/config/codecoverage_xdebug.ini) "${PHPCONFDIR_CLI}/99-codecoverage_xdebug.ini" + if [ -L "${PHPCONFDIR_FPM}/99-codecoverage_xdebug.ini" ]; then sudo rm "${PHPCONFDIR_FPM}/99-codecoverage_xdebug.ini"; fi + sudo ln -s $(realpath tests/ci/config/codecoverage_xdebug.ini) "${PHPCONFDIR_FPM}/99-codecoverage_xdebug.ini" + + sudo service php-fpm restart + fi +} + +disable_cc() { + if [ "$TRAVIS" = true ]; then + phpenv config-rm tests/ci/config/codecoverage_xdebug.ini + + pkill php-fpm + ~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm + else + if [ -L "${PHPCONFDIR_CLI}/99-codecoverage_xdebug.ini" ]; then sudo rm "${PHPCONFDIR_CLI}/99-codecoverage_xdebug.ini"; fi + if [ -L "${PHPCONFDIR_FPM}/99-codecoverage_xdebug.ini" ]; then sudo rm "${PHPCONFDIR_FPM}/99-codecoverage_xdebug.ini"; fi + + sudo service php-fpm restart + fi +} + +case "$1" in + enable | on) + enable_cc + ;; + disable | off) + disable_cc + ;; + *) + echo "ERROR: unknown action '${1}', please use 'enable' or 'disable'" >&2 + exit 1 + ;; +esac diff --git a/tests/ci/setup/setup_php.sh b/tests/ci/setup/setup_php.sh index e45765c..ad16d3d 100755 --- a/tests/ci/setup/setup_php.sh +++ b/tests/ci/setup/setup_php.sh @@ -4,18 +4,17 @@ # To be kept in sync with setup_php_travis.sh +# @todo make it optional to disable xdebug ? + set -e configure_php_ini() { + # note: these settings are not required for cli config echo "cgi.fix_pathinfo = 1" >> "${1}" echo "always_populate_raw_post_data = -1" >> "${1}" - # @todo this only disables xdebug for CLI. To do the same for the FPM config as well, should we use instead `phpdismod` ? - XDEBUG_INI=$(php -i | grep xdebug.ini | grep -v '=>' | head -1) - if [ "$XDEBUG_INI" != "" ]; then - XDEBUG_INI="$(echo "$XDEBUG_INI" | tr -d ',')" - mv "$XDEBUG_INI" "$XDEBUG_INI.bak"; - fi + # we disable xdebug for speed for both cli and web mode + phpdismod xdebug } # install php diff --git a/tests/ci/setup/setup_php_travis.sh b/tests/ci/setup/setup_php_travis.sh index 8428b86..4cc194e 100755 --- a/tests/ci/setup/setup_php_travis.sh +++ b/tests/ci/setup/setup_php_travis.sh @@ -2,6 +2,8 @@ # To be kept in sync with setup_php.sh +# @todo make it optional to disable xdebug ? + set -e SCRIPT_DIR="$(dirname -- "$(readlink -f "$0")")" @@ -10,12 +12,8 @@ configure_php_ini() { echo "cgi.fix_pathinfo = 1" >> "${1}" echo "always_populate_raw_post_data = -1" >> "${1}" - # @todo this only disables xdebug for CLI. To do the same for the FPM config as well, should we use instead `phpdismod` ? - XDEBUG_INI=$(php -i | grep xdebug.ini | grep -v '=>' | head -1) - if [ "$XDEBUG_INI" != "" ]; then - XDEBUG_INI="$(echo "$XDEBUG_INI" | tr -d ',')" - mv "$XDEBUG_INI" "$XDEBUG_INI.bak"; - fi + # we disable xdebug for speed for both cli and web mode + phpenv config-rm xdebug.ini } PHPVER=$(phpenv version-name) -- 2.43.0