phpunit-selenium icon indicating copy to clipboard operation
phpunit-selenium copied to clipboard

PHPUnit/Selenium Code Coverage Report with PHPUnit_Extensions_Selenium2TestCase

Open rooseveltrp opened this issue 11 years ago • 1 comments

Hi,

I am having some difficulty with generating Code Coverage Report for a project that is using the following technologies:

  • Selenium 2.39 Standalone Driver/Server
  • PHP 5.5.0
  • PHP_CodeCoverage 1.2.13
  • PHPUnit 3.7.29

The main issue is.. it is not collecting code coverage report for the actual file that is loaded by Firefox. In this case /index.php. I am not so sure where it is going wrong or if I missed a step.

Here is my setup:

php.ini file*

auto_prepend_file = "PHPUnit/Extensions/SeleniumCommon/prepend.php"
auto_append_file = "PHPUnit/Extensions/SeleniumCommon/append.php"

my project structure

/www/phpunittester/index.php /www/phpunittester/phpunit_coverage.php /www/phpunittester/coverage/ /www/phpunittester/tests/ /www/phpunittester/tests/WebTest.php

the command I am using from the /www/phpuittester/ folder is:

phpunit --coverage-html ./coverage tests/WebTest.php

Generated Report

generatedcoveragereport

Source Codes

/www/phpunittester/index.php

<?php

echo "Hello";
echo " World!";
echo " You are awesome!";

?>

/www/phpunittester/tests/WebTest.php

<?php
class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{
    protected $coverageScriptUrl = 'http://localhost/phpunittester/phpunit_coverage.php';

    protected function setUp()
    {
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost/phpunittester/');
    }

    public function testTitle()
    {
        $this->url('http://localhost/phpunittester/');
        $this->assertEquals('Example WWW Page', $this->title());
    }

}
?>

/www/phpunittester/phpunit_coverage.php

<?php

require_once 'File/Iterator/Autoload.php';
require_once 'PHP/CodeCoverage/Autoload.php';

// Set this to the directory that contains the code coverage files.
// It defaults to getcwd(). If you have configured a different directory
// in prepend.php, you need to configure the same directory here.
$GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] = getcwd();

if (isset($_GET['PHPUNIT_SELENIUM_TEST_ID'])) {
    $facade = new File_Iterator_Facade;
    $files  = $facade->getFilesAsArray(
      $GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'],
      $_GET['PHPUNIT_SELENIUM_TEST_ID']
    );

    $coverage = array();

    foreach ($files as $file) {
        $data = unserialize(file_get_contents($file));
        unlink($file);
        unset($file);
        $filter = new PHP_CodeCoverage_Filter();

        foreach ($data as $file => $lines) {
            if ($filter->isFile($file)) {
                if (!isset($coverage[$file])) {
                    $coverage[$file] = array(
                      'md5' => md5_file($file), 'coverage' => $lines
                    );
                } else {
                    foreach ($lines as $line => $flag) {
                        if (!isset($coverage[$file]['coverage'][$line]) ||
                            $flag > $coverage[$file]['coverage'][$line]) {
                            $coverage[$file]['coverage'][$line] = $flag;
                        }
                    }
                }
            }
        }
    }

    print serialize($coverage);
}

rooseveltrp avatar Jan 24 '14 20:01 rooseveltrp

Did you add your code folders to Whitelist as required by PHPUnit?

okainov avatar Jun 13 '20 09:06 okainov