How to Fix PHPUnit AutoLoad errors in /usr/share/pear/File/Iterator/Autoload.php

I had to fix a rather frustrating issue with PHP Unit today.  I am documenting this for two reasons.

  1. Is that I am sure others out there will run into the same issue and hopefully this will help.
  2. I am sure that I will run into this again sometime in the future — and I want to remember it!  🙂

The issue is that when running PHP 5.2 and installing a fresh copy of PHP Unit… you are broken.  Sucks right?  Here is the error that you will most likely see:

PHP Parse error:  syntax error, unexpected T_FUNCTION, expecting ')' in /usr/share/pear/File/Iterator/Autoload.php on line 45

It’s unfortunate that you get a syntax error, expecting a function with a fresh copy of PHPUnit… but let’s move on.  The background is that there are certain Pear Packages that PHPUnit depends on.  Those packages have been updated for newer versions of PHP.  Afterall – PHP 5.2 has been deprecated for 2+ years (as of this writing).  But what about those projects (such as one of mine) that are still on PHP 5.2 and can’t upgrade at the moment?

How to Fix PHPUnit – Summary

Well – here is the way to hack around this issue and fix it.  In short – you will need to uninstall all of the updated packages and then re-install specific versions of them.  Let’s look at this.

You will need to uninstall these versions:

  • phpunit/File_Iterator-1.3.3
  • phpunit/Text_Template-1.1.3
  • phpunit/PHP_Timer-1.0.4

You will need to re-install these versions:

  • phpunit/File_Iterator-1.3.2
  • phpunit/Text_Template-1.1.2
  • phpunit/PHP_Timer-1.0.3

How to Fix PHPUnit AutoLoad errors – Step by Step

So let’s walk through this.  Let’s assume I just installed PHP Unit.

[ben@zdc-dev-01 tests]$ sudo pear install phpunit/PHPUnit-3.6.12
downloading PHPUnit-3.6.12.tgz ...
Starting to download PHPUnit-3.6.12.tgz (120,240 bytes)
..........................done: 120,240 bytes
install ok: channel://pear.phpunit.de/PHPUnit-3.6.12

I am now going to run a test

[ben@zdc-dev-01 tests]$ phpunit --version
PHP Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /usr/share/pear/File/Iterator/Autoload.php on line 45
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. require() /usr/bin/phpunit:43

Well that sucks… OK – let’s uninstall some stuff.  We first have to uninstall PHP Unit.  PHP Unit has several other packages listed as dependencies.  We need to remove PHP Unit so that these other packages can be removed.

[ben@zdc-dev-01 tests]$ sudo pear uninstall phpunit/phpunit
uninstall ok: channel://pear.phpunit.de/PHPUnit-3.6.12

Now that PHP Unit is uninstalled we can uninstall the three packages we mentioned above.  let’s start with File_Iterator.  File_Iterator is a dependency for PHP_CodeCoverage so let’s remove that first.

[ben@zdc-dev-01 tests]$ sudo pear uninstall phpunit/PHP_CodeCoverage
uninstall ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.1.4

Now let’s remove File_Iterator.

[ben@zdc-dev-01 tests]$ sudo pear uninstall phpunit/File_Iterator
uninstall ok: channel://pear.phpunit.de/File_Iterator-1.3.3

Now let’s move on to Text_Template. Text_Template is a dependency for PHPUnit_MockObject so let’s remove that first.

[ben@zdc-dev-01 tests]$ sudo pear uninstall phpunit/PHPUnit_MockObject
uninstall ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.1.1

Now let’s remove Text_Template.

[ben@zdc-dev-01 tests]$ sudo pear uninstall phpunit/Text_Template
uninstall ok: channel://pear.phpunit.de/Text_Template-1.1.3

The last of the three that need to be downgraded is PHP_Timer.  PHP_Timer is a dependency for PHP_Invoker (and PHP_CodeSniffer) so let’s remove PHP_Invoker.

[ben@zdc-dev-01 tests]$ sudo pear uninstall phpunit/PHP_Invoker
uninstall ok: channel://pear.phpunit.de/PHP_Invoker-1.1.2

Now we can remove PHP_Timer.

[ben@zdc-dev-01 tests]$ sudo pear uninstall phpunit/PHP_Timer
uninstall ok: channel://pear.phpunit.de/PHP_Timer-1.0.4

OK – Now we have all of the bad versions removed.  Now let’s re-install some stuff.  In the next three steps make sure that you include the specific version number.

First let’s install File_Iterator.

[ben@zdc-dev-01 tests]$ sudo pear install phpunit/File_Iterator-1.3.2
downloading File_Iterator-1.3.2.tgz ...
Starting to download File_Iterator-1.3.2.tgz (5,181 bytes)
.....done: 5,181 bytes
install ok: channel://pear.phpunit.de/File_Iterator-1.3.2

Now let’s install Text_Template.

[ben@zdc-dev-01 tests]$ sudo pear install phpunit/Text_Template-1.1.2
downloading Text_Template-1.1.2.tgz ...
Starting to download Text_Template-1.1.2.tgz (3,633 bytes)
....done: 3,633 bytes
install ok: channel://pear.phpunit.de/Text_Template-1.1.2

Now let’s install PHP_Timer.

[ben@zdc-dev-01 tests]$ sudo pear install phpunit/PHP_Timer-1.0.3
downloading PHP_Timer-1.0.3.tgz ...
Starting to download PHP_Timer-1.0.3.tgz (3,743 bytes)
....done: 3,743 bytes
install ok: channel://pear.phpunit.de/PHP_Timer-1.0.3

Now we can re-install PHP Unit.  Also make sure to grab the right version of PHP Unit as well.  You will also notice that when we install PHP Unit we will see a couple extra packages that we just removed get re-added.  These are PHP_CodeCoverage and PHPUnit_MockObject.  PHP_Invoker is not automatically included unless you include the flag “–alldeps”.  We will see a message about this below.

Let’s install PHP Unit

[ben@zdc-dev-01 tests]$ sudo pear install phpunit/PHPUnit-3.6.12
Did not download optional dependencies: phpunit/PHP_Invoker, use --alldeps to download automatically
phpunit/PHPUnit can optionally use package "phpunit/PHP_Invoker" (version >= 1.1.0)
downloading PHPUnit-3.6.12.tgz ...
Starting to download PHPUnit-3.6.12.tgz (120,240 bytes)
..........................done: 120,240 bytes
downloading PHP_CodeCoverage-1.1.4.tgz ...
Starting to download PHP_CodeCoverage-1.1.4.tgz (132,781 bytes)
...done: 132,781 bytes
downloading PHPUnit_MockObject-1.1.1.tgz ...
Starting to download PHPUnit_MockObject-1.1.1.tgz (19,910 bytes)
...done: 19,910 bytes
install ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.1.4
install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.1.1
install ok: channel://pear.phpunit.de/PHPUnit-3.6.12

Now let’s test our installation.

[ben@zdc-dev-01 tests]$ phpunit --version
PHPUnit 3.6.12 by Sebastian Bergmann.

Looks pretty good!

How to Fix PHPUnit AutoLoad errors – Conclusion

Hopefully this saved you some time and did not force you to upgrade your environment to a version of PHP that you may not be ready to start using.

In short we we took this error:

PHP Parse error:  syntax error, unexpected T_FUNCTION, expecting ')' in /usr/share/pear/File/Iterator/Autoload.php on line 45

Learned that is was due to php changes in underlying packages so we removed these pear packages:

  • phpunit/File_Iterator-1.3.3
  • phpunit/Text_Template-1.1.3
  • phpunit/PHP_Timer-1.0.4

And replaced them with these versions:

  • phpunit/File_Iterator-1.3.2
  • phpunit/Text_Template-1.1.2
  • phpunit/PHP_Timer-1.0.3

In the end…

[ben@zdc-dev-01 tests]$ phpunit --version
PHPUnit 3.6.12 by Sebastian Bergmann.

We had a successful PHPUnit Load with the AutoLoader fixed while still running on PHP 5.2

 

Comments

comments

Posted in , and tagged , , , .