fix test coverage calculations
authorgggeek <giunta.gaetano@gmail.com>
Tue, 15 Dec 2020 23:20:22 +0000 (23:20 +0000)
committergggeek <giunta.gaetano@gmail.com>
Tue, 15 Dec 2020 23:20:22 +0000 (23:20 +0000)
.travis.yml
demo/client/_prepend.php
demo/server/_prepend.php
phpunit.xml
tests/3LocalhostTest.php
tests/4LocalhostMultiTest.php
tests/7ExtraTest.php
tests/LocalFileTestCase.php
tests/ci/setup/setup_code_coverage.sh [new file with mode: 0755]
tests/ci/setup/setup_php.sh
tests/ci/setup/setup_php_travis.sh

index 9d1f3c0..12d85d6 100644 (file)
@@ -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
index e2a480f..0f65cd9 100644 (file)
@@ -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";
index 3963a32..8dbf6cf 100644 (file)
@@ -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";
index cdd5e2b..a4e4a19 100644 (file)
@@ -7,7 +7,9 @@
         <whitelist>
             <directory suffix=".php">./</directory>
             <exclude>
-                <directory>./Resources</directory>
+                <directory>./build</directory>
+                <directory>./doc</directory>
+                <directory>./extras</directory>
                 <directory>./tests</directory>
                 <directory>./vendor</directory>
                 <file>./pakefile.php</file>
index 439b635..d513b09 100644 (file)
@@ -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)
     {
index 317e517..619e3ae 100644 (file)
@@ -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()
index 5770eca..48acf6b 100644 (file)
@@ -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');
index 13160c7..9cc660f 100644 (file)
@@ -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 (executable)
index 0000000..145de3d
--- /dev/null
@@ -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
index e45765c..ad16d3d 100755 (executable)
@@ -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
index 8428b86..4cc194e 100755 (executable)
@@ -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)