CodeView

PHPUnit test harness for Komodo

DebugOutputTools.php

<?php
 
    /**
     * @author  Michael Heim
     * @link    http://www.zeilenwechsel.de/
     * @version 1.2.0
     * @license http://www.zeilenwechsel.de/it/code/browse/komodo-phpunit-harness/prod/license.txt
     */
 
    /**
     * Provides tools for comparing and standardizing raw PHPUnit test harness output.
     */
    class DebugOutputTools {
 
        /**
         * Removes random differences from the output. Replaces time and memory usage
         * by placeholders and removes empty lines.
         * 
         * @param  string $content
         * @return string
         */
        public static function normalize_output ( $content ) {
 
            $content = self::remove_phpunit_from_stacktrace( $content );
            $content = self::use_placeholders_for_runtime_info( $content );
            $content = self::normalize_phpunit_variable_notation( $content );
            $content = self::remove_phpunit_xmlconfig_notice( $content );
            $content = self::remove_empty_lines( $content );
 
            return $content;
 
        }
 
        /**
         * @param  string $content
         * @return string
         */
        public static function remove_empty_lines ( $content ) {
 
            return preg_replace( "%\n+%", "\n", $content );
 
        }
 
        /**
         * Removes the PHPUnit invokation from each stack trace.
         * 
         * Required for PHPUnit 3.6, which doesn't remove itself from the stack trace
         * if the tests are run from the command line.
         *
         * NB: The removal will leave empty lines behind. Call remove_empty_lines()
         * afterwards to get rid of them.
         * 
         * @param  string $content
         * @return string
         */
        public static function remove_phpunit_from_stacktrace ( $content ) {
 
            return preg_replace( '%^.*?[/\\\\]phpunit:\d+$%im', '', $content );
 
        }
 
        /**
         * Normalizes the way variables are printed by PHPUnit.
         * 
         * Required for PHPUnit 3.6. Before, variables were printed including the type
         * (e.g. "Failed asserting that <boolean:false> is true"). In PHPUnit 3.6, the
         * type information is dropped ("Failed asserting that false is true").
         *
         * Output will be normalized with the new notation, ie without data types.
         *
         * IMPORTANT: So far, normalization is implemented for boolean variables only.
         * 
         * @param  string $content
         * @return string
         */
        public static function normalize_phpunit_variable_notation ( $content ) {
 
            return preg_replace( '%<boolean:(true|false)>%', "$1", $content );
 
        }
 
        /**
         * Replaces version, memory usage and time statistics by placeholders ('[VERSION]', '[OPTIONAL MEMORY INFO]',
         * '[TIME]'). Absolute paths to the integration test resource directory are replaced by '[TEST RESOURCE DIR]',
         * and directory separators normalized to forward slashes.
         *
         * The stack trace also contains paths of the helper files for running the integration test. They are removed
         * from the stack trace.
         *
         * NB: Memory info is not generated by all PHPUnit versions. In standardized output, the [OPTIONAL MEMORY INFO]
         * placeholder is present regardless, so as to allow output comparison across versions.
         * 
         * @param  string $content
         * @return string
         */
        public static function use_placeholders_for_runtime_info ( $content ) {
 
            $content = preg_replace( '%(PHPUnit *)[\d.]+( +by Sebastian Bergmann)%', "$1[VERSION]$2", $content );
            $content = preg_replace( '%(Using PHP ).*(\.$)%m', "$1[VERSION]$2", $content );
 
            $content = preg_replace( '%(Time:\s*)[\d.:]+%', "$1[TIME]", $content );
            $content = preg_replace( '%(@suite_finished@:[^;]+;\s*)([\d.:]+)%', "$1[TIME]", $content );
            $content = preg_replace( '%(\[TIME\]) +seconds?%i', "$1", $content );
 
            $content = preg_replace( '%Time:\s*\[TIME\]( *$|,\s+Memory:\s*[\d.]+(k|m)b)%im', "[TIME] [OPTIONAL MEMORY INFO]", $content );
 
            $test_resource_dir = preg_quote( INTEGRATION_TEST_RESOURCE_DIR, '%' );
            $content = preg_replace( "%{$test_resource_dir}[\\\\/]?%", '[TEST RESOURCE DIR]', $content );
 
            $integration_test_dir = preg_quote( INTEGRATION_TEST_DIR, '%' );
            $content = preg_replace( "%^$integration_test_dir.+?:\d+\n%m", '', $content );
 
            $content = str_replace( '\\', '/', $content );
 
            return $content;
 
        }
 
        /**
         * Removes the PHPUnit status line for XML configurations. Required for
         * comparisons across PHPUnit versions.
         *
         * In PHPUnit 3.6, a notice is generated if an XML configuration is used:
         * "Configuration read from [path to XML config file].xml". Earlier versions
         * lack this info.
         * 
         * @param  string $content
         * @return string
         */
        public static function remove_phpunit_xmlconfig_notice ( $content ) {
 
            return preg_replace( '%^Configuration read from .+\.xml(.dist)?$%im', '', $content );
 
        }
 
    }
 
?>