Phpunit: Code coverage problem: Fatal error: Class 'PHP_Token_Stream' not found

Created on 26 Sep 2011  路  22Comments  路  Source: sebastianbergmann/phpunit

I am running PHPUnit 3.5.15 on a freshly installed Ubuntu 11.04 64bit. Tests are running fine, however, when trying to run code coverage as well, an error is thrown:

$ phpunit --coverage-html build/coverage/ Tests/SomeTest.php 
PHPUnit 3.5.15 by Sebastian Bergmann.

...

Time: 1 second, Memory: 6.00Mb

OK (3 tests, 30 assertions)

Generating code coverage report, this may take a moment.
Fatal error: Class 'PHP_Token_Stream' not found in /usr/share/php/PHP/Token/Stream/CachingFactory.php on line 68

Call Stack:
    0.0002     639560   1. {main}() /usr/bin/phpunit:0
    0.0132    1151288   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:49
    0.0132    1152160   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
    0.0300    4052112   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:188
    1.5055    5682376   5. PHP_CodeCoverage_Report_HTML->process() /usr/share/php/PHPUnit/TextUI/TestRunner.php:363
    1.5059    5733328   6. PHP_CodeCoverage_Report_HTML->addItems() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:135
    1.5059    5734064   7. PHP_CodeCoverage_Report_HTML_Node_Directory->addFile() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:213
    1.5059    5737416   8. PHP_CodeCoverage_Report_HTML_Node_File->__construct() /usr/share/php/PHP/CodeCoverage/Report/HTML/Node/Directory.php:156
    1.5062    5801152   9. PHP_CodeCoverage_Util::getLinesToBeIgnored() /usr/share/php/PHP/CodeCoverage/Report/HTML/Node/File.php:169
    1.5063    5801792  10. PHP_Token_Stream_CachingFactory::get() /usr/share/php/PHP/CodeCoverage/Util.php:271

I have manually included missing file in /usr/share/php/PHP/Token/Stream/CachingFactory.php for debugging purposes, the diff is

<             include_once '/usr/share/php/PHP/Token/Stream.php';

Running the same test again results in following error:

Generating code coverage report, this may take a moment.
Fatal error: Class 'PHP_Token_OPEN_TAG' not found in /usr/share/php/PHP/Token/Stream.php on line 205

Call Stack:
    0.0002     639560   1. {main}() /usr/bin/phpunit:0
    0.0132    1151288   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:49
    0.0132    1152160   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
    0.0302    4052608   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:188
    1.5091    5682912   5. PHP_CodeCoverage_Report_HTML->process() /usr/share/php/PHPUnit/TextUI/TestRunner.php:363
    1.5095    5733840   6. PHP_CodeCoverage_Report_HTML->addItems() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:135
    1.5095    5734576   7. PHP_CodeCoverage_Report_HTML_Node_Directory->addFile() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:213
    1.5095    5737928   8. PHP_CodeCoverage_Report_HTML_Node_File->__construct() /usr/share/php/PHP/CodeCoverage/Report/HTML/Node/Directory.php:156
    1.5099    5801688   9. PHP_CodeCoverage_Util::getLinesToBeIgnored() /usr/share/php/PHP/CodeCoverage/Report/HTML/Node/File.php:169
    1.5099    5802328  10. PHP_Token_Stream_CachingFactory::get() /usr/share/php/PHP/CodeCoverage/Util.php:271
    1.5106    5953392  11. PHP_Token_Stream->__construct() /usr/share/php/PHP/Token/Stream/CachingFactory.php:69
    1.5106    5957656  12. PHP_Token_Stream->scan() /usr/share/php/PHP/Token/Stream.php:147

Has anyone experienced this behaviour? I'll try looking into it more closely in the upcoming days.

Most helpful comment

Found the solution. Adding the composer autoload.php to PHPUnit xml config file, as is:

<phpunit colors="true" bootstrap="vendor/autoload.php">

All 22 comments

I cannot reproduce this.

I have just come up against this exact bug under OSX Lion

Can you post your php include path? Is /usr/share/php/ in it?

That path actually doesn't exist on my system ...
My current include path is: '.:/usr/local/Cellar/php/5.3.8/lib/php'

As is clear from this path, I used homebrew to install php

Okay, then where have you installed PHPUnit and where is your PHP/Token/Stream directory located? Make sure all these are under the php include path(adjust that path in php.ini if necessary) so that autoloader can find them.

If class name is PHP_Token_Stream autoloader will try do include("PHP/Token/Stream.php");

My response to your first question actually got me thinking - it didn't make sense that my include_path was missing the normal paths, so I went digging - it looks like I'm having problems with autoloading class files.
I'll need to look into this a bit more I think

Thanks for the responses though

@gpkamp figured the problem is resolved by manually including Token/Stream/Autoload.php, can you try it to see if it works for you as well?

My problem was gone by re-installing everything, I blame PEAR for original issue :)

I've tried reinstalling alright, but it made no difference.
Note that this was working perfectly for me until this morning when I did something. God knows what that was though :/

Anyway, after figuring out I needed this line: '''require 'PHPUnit/Autoload.php';''' (but I didn't need it before this morning ...), I now have another error:

Generating code coverage report, this may take a moment.PHP Fatal error: Class 'PHP_Token_OPEN_TAG' not found in /usr/local/share/pear/PHP/Token/Stream.php on line 205

Things are getting _awfully_ strange here...

Just to note - adding:
require_once('PHP/Token/Stream/Autoload.php');
solved my issues.

Hey Daniel, same problem here.
PHPUnit 3.5.15 on OSX + XAMPP.
PEAR Version: 1.9.4
PHP Version: 5.3.1
Zend Engine Version: 2.3.0

What's your PEAR and PHP version? And yes, adding your require_once to the top of PHP/Token/Stream/CachingFactory.php hacks that into submission.

PHPUnit 3.5.15 on OSX + nginx/php-fpm/mysql/mongo ;)
PEAR Version: 1.9.4
PHP Version: 5.3.8
Zend Engine Version: 2.3.0

A colleague, who is having no problems at all, reports this:
PHPUnit 3.5.5 on Ubuntu 11.04 + nginx/php-fpm/mysql/mongo
PEAR Version: 1.9.1
PHP Version: 5.3.5-1ubuntu7.3
Zend Engine Version: 2.3.0

@danielhunt Can confirm, adding require_once('PHP/Token/Stream/Autoload.php'); solved the issue for me, too (thanks!).

Nothing from @sebastianbergmann on this? :)

I cannot reproduce this, sorry. And PHPUnit 3.5 is no longer supported, also sorry.

... that's _so_ odd :/
I'll try reinstalling the whole lot and see what happens

(cheers for the quick response)

Used the steps at http://dustyreagan.com/downgrade-phpunit-3-6-to-3-5-15/ to get the correct versions and dependencies for installing PHPUnit 3.5.15

Adding the snippet of code also worked for me.

I managed to reproduce your error in parts.
The ideal is that PHPUnit is installed inside the PEAR folder, but gives problem.

I corrected the problem by deleting the directories and PHP and PHPUnit downloading again through the pear command.

Downgraded the version of PHP_TokenStream to 1.0.1 and worked for me.
sudo pear install --force phpunit/PHP_TokenStream-1.0.1

Thanks a lot @amitchhajer! Work for me! ;)

5 years later and I am running accross the very same problem. The Class does exist!
PHPUnit version 5.3.4:

Generating code coverage report in HTML format ...PHP Fatal error: Class 'AcmeControllersBaseController' not found in /var/www/html/src/Controllers/
AdminController.php on line 11
PHP Stack trace:
PHP 1. {main}() /usr/local/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:525
PHP 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:110
PHP 4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:159
PHP 5. PHP_CodeCoverage_Report_HTML->process() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:501
PHP 6. PHP_CodeCoverage->getReport() phar:///usr/local/bin/phpunit/php-code-coverage/CodeCoverage/Report/HTML.php:68
PHP 7. PHP_CodeCoverage_Report_Factory->create() phar:///usr/local/bin/phpunit/php-code-coverage/CodeCoverage.php:146
PHP 8. PHP_CodeCoverage->getData() phar:///usr/local/bin/phpunit/php-code-coverage/CodeCoverage/Report/Factory.php:25
PHP 9. PHP_CodeCoverage->addUncoveredFilesFromWhitelist() phar:///usr/local/bin/phpunit/php-code-coverage/CodeCoverage.php:182
PHP 10. PHP_CodeCoverage->processUncoveredFileFromWhitelist() phar:///usr/local/bin/phpunit/php-code-coverage/CodeCoverage.php:708
PHP 11. include_once() phar:///usr/local/bin/phpunit/php-code-coverage/CodeCoverage.php:735

Found the solution. Adding the composer autoload.php to PHPUnit xml config file, as is:

<phpunit colors="true" bootstrap="vendor/autoload.php">

Was this page helpful?
0 / 5 - 0 ratings