sebastianbergmann/phpunit-selenium — Issue #104: Selenium returns empty or invalid code coverage
Author: andriesss
Created: 2012-02-29 21:17:12+00:00
State: open

I tried to enable code coverage for Selenium, by following the steps below:

* I copied PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into my webserver's document root directory.
* In my php.ini configuration file, I configured PHPUnit/Extensions/SeleniumTestCase/prepend.php and PHPUnit/Extensions/SeleniumTestCase/append.php as the auto_prepend_file and auto_append_file, respectively.
* I made sure that xdebug was enabled
* I made sure that my document root was writeable, so the temporary files containing the coverage data could be created.
* In my test case class, i added protected $coverageScriptUrl = 'http://host/phpunit_coverage.php'; to configure the URL for the phpunit_coverage.php script.

```php
class fooTest extends \PHPUnit_Extensions_SeleniumTestCase
{
protected $coverageScriptUrl = 'http://localhost/phpunit_coverage.php';

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

public function testFoo()
{
$this->open('/');

$this->assertElementPresent('id=foo');
}
}
```
* started Selenium: java -jar selenium-server.jar
* ran my testsuite: phpunit --coverage-html c:\coverage fooTest.php

At this point, the following error is generated:

PHP Notice: unserialize(): Error at offset 0 of 2941 bytes in C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php on line 994

PHP Stack trace:
PHP 1. {main}() C:\PHP\phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() C:\PHP\phpunit:45
PHP 3. PHPUnit_TextUI_Command->run() C:\PHP\pear\PHPUnit\TextUI\Command.php:130
PHP 4. PHPUnit_TextUI_TestRunner->doRun() C:\PHP\pear\PHPUnit\TextUI\Command.php:192
PHP 5. PHPUnit_Framework_TestSuite->run() C:\PHP\pear\PHPUnit\TextUI\TestRunner.php:325
PHP 6. PHPUnit_Framework_TestSuite->runTest() C:\PHP\pear\PHPUnit\Framework\TestSuite.php:745
PHP 7. PHPUnit_Extensions_SeleniumTestCase->run() C:\PHP\pear\PHPUnit\Framework\TestSuite.php:772
PHP 8. PHPUnit_Extensions_SeleniumTestCase->getCodeCoverage() C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php:569
PHP 9. unserialize() C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php:994

Notice: unserialize(): Error at offset 0 of 2941 bytes in C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php on line 994

Call Stack:
0.0011 326376 1. {main}() C:\PHP\phpunit:0
0.0080 766192 2. PHPUnit_TextUI_Command::main() C:\PHP\phpunit:45
0.0080 766608 3. PHPUnit_TextUI_Command->run() C:\PHP\pear\PHPUnit\TextUI\Command.php:130
0.1542 3486160 4. PHPUnit_TextUI_TestRunner->doRun() C:\PHP\pear\PHPUnit\TextUI\Command.php:192
0.1635 3975552 5. PHPUnit_Framework_TestSuite->run() C:\PHP\pear\PHPUnit\TextUI\TestRunner.php:325
0.1636 3975880 6. PHPUnit_Framework_TestSuite->runTest() C:\PHP\pear\PHPUnit\Framework\TestSuite.php:745
0.1636 3975880 7. PHPUnit_Extensions_SeleniumTestCase->run() C:\PHP\pear\PHPUnit\Framework\TestSuite.php:772
2.2328 4238912 8. PHPUnit_Extensions_SeleniumTestCase->getCodeCoverage() C:\PHP\pear\PHPUnit\Extensions\Seleniu
mTestCase.php:569
2.3044 4243520 9. unserialize() C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php:994

PHP Fatal error: Uncaught exception 'Exception' with message 'Empty or invalid code coverage data received from url "ht
tp://localhost/test/phpunit_coverage.php?PHPUNIT_SELENIUM_TEST_ID=d71164a93eabb9c69949c89f760d2fc9"' in C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php:998

Stack trace:
#0 C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php(569): PHPUnit_Extensions_SeleniumTestCase->getCodeCoverage()
#1 C:\PHP\pear\PHPUnit\Framework\TestSuite.php(772): PHPUnit_Extensions_SeleniumTestCase->run(Object(PHPUnit_Framework_TestResult))
#2 C:\PHP\pear\PHPUnit\Framework\TestSuite.php(745): PHPUnit_Framework_TestSuite->runTest(Object(fooTest), Object(PHPUnit_Framework_TestResult))
#3 C:\PHP\pear\PHPUnit\TextUI\TestRunner.php(325): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#4 C:\PHP\pear\PHPUnit\TextUI\Command.php(192): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Extensions_SeleniumTestSuite), Array)
#5 C:\PHP\pear\PHPUnit\TextUI\Command.php(130): PHPUnit_TextUI_Command-> in C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php on line 998

Fatal error: Uncaught exception 'Exception' with message 'Empty or invalid code coverage data received from url "http://localhost/test/phpunit_coverage.php?PHPUNIT_SELENIUM_TEST_ID=d71164a93eabb9c69949c89f760d2fc9"' in C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php on line 998

Exception: Empty or invalid code coverage data received from url "http://localhost/test/phpunit_coverage.php?PHPUNIT_SELENIUM_TEST_ID=d71164a93eabb9c69949c89f760d2fc9" in C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php on line 998

Call Stack:
0.0011 326376 1. {main}() C:\PHP\phpunit:0
0.0080 766192 2. PHPUnit_TextUI_Command::main() C:\PHP\phpunit:45
0.0080 766608 3. PHPUnit_TextUI_Command->run() C:\PHP\pear\PHPUnit\TextUI\Command.php:130
0.1542 3486160 4. PHPUnit_TextUI_TestRunner->doRun() C:\PHP\pear\PHPUnit\TextUI\Command.php:192
0.1635 3975552 5. PHPUnit_Framework_TestSuite->run() C:\PHP\pear\PHPUnit\TextUI\TestRunner.php:325
0.1636 3975880 6. PHPUnit_Framework_TestSuite->runTest() C:\PHP\pear\PHPUnit\Framework\TestSuite.php:745
0.1636 3975880 7. PHPUnit_Extensions_SeleniumTestCase->run() C:\PHP\pear\PHPUnit\Framework\TestSuite.php:772
2.2328 4238912 8. PHPUnit_Extensions_SeleniumTestCase->getCodeCoverage() C:\PHP\pear\PHPUnit\Extensions\SeleniumTestCase.php:569

When I try to open that URL in the browser, I get the following error:

( ! ) Warning: Invalid argument supplied for foreach() in C:\PHP\pear\File\Iterator\Factory.php on line 73
Call Stack
#	Time	Memory	Function	Location
1	0.0005	344264	{main}( )	..\phpunit_coverage.php:0
2	0.0017	434504	File_Iterator_Facade->getFilesAsArray( )	..\phpunit_coverage.php:60
3	0.0019	450656	File_Iterator_Factory->getFileIterator( )	..\Facade.php:76

( ! ) Warning: Invalid argument supplied for foreach() in C:\PHP\pear\File\Iterator\Facade.php on line 88
Call Stack
#	Time	Memory	Function	Location
1	0.0005	344264	{main}( )	..\phpunit_coverage.php:0
2	0.0017	434504	File_Iterator_Facade->getFilesAsArray( )	..\phpunit_coverage.php:60
a:0:{}