Home » Php » php – PHPunit Code Coverage error

php – PHPunit Code Coverage error

Posted by: admin July 12, 2020 Leave a comment

Questions:

PHPunit is working, but I get this code coverage error and don’t get the code coverage report.

Does anyone know how to fix this?

Thanks,

Demian.

[email protected]:/var/www/z2d2/tests$ phpunit
PHPUnit 3.5.15 by Sebastian Bergmann.

...

Time: 1 second, Memory: 13.00Mb

OK (3 tests, 4 assertions)

Generating code coverage report, this may take a moment.PHP Fatal error:  Class 'PHP_Token_Stream' not found in /usr/share/php/PHP/Token/Stream/CachingFactory.php on line 68
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:49
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:188
PHP   5. PHP_CodeCoverage_Report_HTML->process() /usr/share/php/PHPUnit/TextUI/TestRunner.php:363
PHP   6. PHP_CodeCoverage_Report_HTML->addItems() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:135
PHP   7. PHP_CodeCoverage_Report_HTML_Node_Directory->addFile() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:214
PHP   8. PHP_CodeCoverage_Report_HTML_Node_File->__construct() /usr/share/php/PHP/CodeCoverage/Report/HTML/Node/Directory.php:156
PHP   9. PHP_CodeCoverage_Util::getLinesToBeIgnored() /usr/share/php/PHP/CodeCoverage/Report/HTML/Node/File.php:169
PHP  10. PHP_Token_Stream_CachingFactory::get() /usr/share/php/PHP/CodeCoverage/Util.php:271

Fatal error: Class 'PHP_Token_Stream' not found in /usr/share/php/PHP/Token/Stream/CachingFactory.php on line 68

Call Stack:
    0.0002     326940   1. {main}() /usr/bin/phpunit:0
    0.0434     666604   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:49
    0.0435     667084   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
    0.0943    4312004   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:188
    1.1150   13272196   5. PHP_CodeCoverage_Report_HTML->process() /usr/share/php/PHPUnit/TextUI/TestRunner.php:363
    1.1521   14100768   6. PHP_CodeCoverage_Report_HTML->addItems() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:135
    1.1521   14101320   7. PHP_CodeCoverage_Report_HTML_Node_Directory->addFile() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:214
    1.1521   14103132   8. PHP_CodeCoverage_Report_HTML_Node_File->__construct() /usr/share/php/PHP/CodeCoverage/Report/HTML/Node/Directory.php:156
    1.1595   14122724   9. PHP_CodeCoverage_Util::getLinesToBeIgnored() /usr/share/php/PHP/CodeCoverage/Report/HTML/Node/File.php:169
    1.1595   14123076  10. PHP_Token_Stream_CachingFactory::get() /usr/share/php/PHP/CodeCoverage/Util.php:271
How to&Answers:

You are running PHPUnit 3.5.x so the Token stream version you need can be installed using:

sudo pear install --force --alldeps phpunit/PHP_CodeCoverage-1.0.2

If you want to update to the current version of phpunit something alone the lines of

sudo pear install --force --alldeps phpunit/phpunit

will do the job but if i remember correctly you are trying to use the Zend Framework testing environment and that only works with PHPUnit 3.5.15


If both options fail or are not suitable go for

sudo pear install --force phpunit/PHP_TokenStream

What will definitely get you a working PHPUnit 3.5.15

http://dustyreagan.com/downgrade-phpunit-3-6-to-3-5-15/

Important note: if you install PHPUnit via PEAR. This installation method is no longer
supported and http://pear.phpunit.de/ will be shut down no later than
December, 31 2014.

Please read http://phpunit.de/manual/current/en/installation.html and
learn how to use PHPUnit from a PHAR or install it via Composer.

Answer:

If all efforts to downgrade phpunit 3.6 to 3.5 not success.
You can fix this problem by add bellow code to /usr/share/php/PHP/Token/Stream/CachingFactory.php to fix “Class ‘PHP_Token_Stream’ not found…”

require_once('PHP/Token/Stream/Autoload.php');

Answer:

Try to fix phpunit by re-installing:

$ pear upgrade -f phpunit

Answer:

Installing phpunit with pear did not work for me at all.

What I finally did is (in some directory for example /home/USERNAME/phpunit):

wget http://pear.phpunit.de/get/phpunit.phar
chmod +x phpunit.phar

And in .bash.rc add the end

export PATH=${PATH}:PATH_WHERE_YOU_PUT_phpunit.phar

In my example PATH_WHERE_YOU_PUT_phpunit.phar will be /home/USERNAME/phpunit

After that, phpunit be can executed with command phpunit.phar in whatever directory

Answer:

An option for some linux distros might be to use yum or apt-get to install phpunit.

I am using Fedora 17 and installed phpunit with yum, everything worked fine at first try. Not sure how well Ubuntu/Mint packages work.

In Windows I used pear and it worked fine there but since I found phpunit in yum I thought I’d give that a try first.