6

I get the following error when I try to run phpunit . from within my project's tests folder:

PHP Fatal error:  Call to undefined method PHP_CodeCoverage_Filter::getInstance() in /usr/share/php/PHPUnit/Framework.php on line 46

I installed PHPUnit via these commands:

sudo pear channel-discover pear.symfony-project.com
sudo pear channel-discover components.ez.no
sudo pear install --alldeps phpunit/PHPUnit

As none of the other methods seem to work, including apt-get.

I think CodeCoverage changed their singleton pattern at some point in time and hence removed getInstance but I don't know how to fix this error. How can I either downgrade CodeCoverage or upgrade PHPUnit?

I tried manually installing the latest versions of everything via the following commands:

sudo apt-get install git
mkdir phpunit && cd phpunit
git clone git://github.com/sebastianbergmann/phpunit.git
git clone git://github.com/sebastianbergmann/dbunit.git
git clone git://github.com/sebastianbergmann/php-file-iterator.git
git clone git://github.com/sebastianbergmann/php-text-template.git
git clone git://github.com/sebastianbergmann/php-code-coverage.git
git clone git://github.com/sebastianbergmann/php-token-stream.git
git clone git://github.com/sebastianbergmann/php-timer.git
git clone git://github.com/sebastianbergmann/phpunit-mock-objects.git
git clone git://github.com/sebastianbergmann/phpunit-selenium.git
git clone git://github.com/sebastianbergmann/phpunit-story.git
git clone git://github.com/sebastianbergmann/php-invoker.git
sudo cp -r dbunit/PHPUnit /usr/share/php/
sudo cp -r php-code-coverage/PHP /usr/share/php/
sudo cp -r php-file-iterator/File /usr/share/php/
sudo cp -r php-invoker/PHP /usr/share/php/
sudo cp -r php-text-template/Text /usr/share/php/
sudo cp -r php-timer/PHP /usr/share/php/
sudo cp -r php-token-stream/PHP /usr/share/php/
sudo cp -r phpunit/PHPUnit /usr/share/php/
sudo cp -r phpunit-mock-objects/PHPUnit /usr/share/php/
sudo cp -r phpunit-selenium/PHPUnit /usr/share/php/
sudo cp -r phpunit-story/PHPUnit /usr/share/php/
sudo cp -r phpunit/phpunit.php /usr/share/php/

But that didn't help any. Now I just have a bunch of junk all over the place :\


Version info:

PEAR Version: 1.9.4
PHP Version: 5.3.6-13ubuntu3.3
Zend Engine Version: 2.3.0
PHPUnit 3.6.9 by Sebastian Bergmann.

Installed packages, channel pear.phpunit.de:
============================================
Package            Version State
File_Iterator      1.3.1   stable
PHPUnit            3.6.9   stable
PHPUnit_MockObject 1.1.1   stable
PHP_CodeCoverage   1.1.1   stable
PHP_Invoker        1.1.0   stable
PHP_Timer          1.0.2   stable
PHP_TokenStream    1.1.2   stable
Text_Template      1.1.1   stable
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • I haven't been able to get phpUnit to work for me either on Ubuntu 11.10. – Jeremy Harris Jan 24 '12 at 17:54
  • 2
    I have same problem and i already asked it with **50 points bounty** : http://stackoverflow.com/questions/8949963/phpunit-pear-upgrading-errors – Eray Jan 24 '12 at 18:06
  • Going on record as an opponent of PEAR, and the way PHPUnit is released in it's current state. – Mike Purcell Jan 24 '12 at 18:58
  • @MikePurcell: Well we don't have many alternatives for unit testing do we? That are easy to install and relatively easy to use? This project only has 2 unit tests so far...so we can still make the switch! – mpen Jan 24 '12 at 19:37
  • @Mark: PHPUnit works great for it's purpose, unit testing. I just disagree with the engineer's release strategies. I actually wrote my own custom autoloader, and bootstrapper, and got it to work without using PEAR. When I am finished with post I will link to you. – Mike Purcell Jan 24 '12 at 19:39
  • @MikePurcell: That would be great! You could save a lot of people some pain. – mpen Jan 24 '12 at 19:45
  • @Mark: Almost done with article, should be published tonight or tomorrow, when I do I will let you know here so you can check it out. You will be my beta tester :) – Mike Purcell Jan 25 '12 at 02:23
  • 1
    @Mark: Ok, done with v1.0 of the article, take a look at it and let me know if it works for you. Good luck! http://melikedev.com/2012/01/25/php-phpunit-use-phpunit-without-pear/ – Mike Purcell Jan 26 '12 at 02:08

5 Answers5

7

you can try this, I have done it on my Ubuntu 12.04

  1. sudo pear uninstall phpunit/PHPUnit
  2. sudo pear uninstall pear.phpunit.de/PHPUnit_MockObject
  3. sudo pear install pear.phpunit.de/PHPUnit_MockObject
  4. sudo pear install --alldeps pear.phpunit.de/PHPUnit

I have tried many other methods. This works when I get the error blow.

hegaofeng@ubuntu:~$ phpunit PHP Warning: require_once(PHPUnit/Framework/MockObject/Autoload.php): failed to open >?>stream: No such file or directory in /usr/share/php/PHPUnit/Autoload.php on line 48 PHP Fatal error: require_once(): Failed opening required >'PHPUnit/Framework/MockObject/Autoload.php' >(include_path='.:/usr/share/php:/usr/share/pear') in /usr/share/php/PHPUnit/Autoload.php on >line 48 hegaofeng@ubuntu:~$ cd /usr/share/php/

hgfeaon
  • 71
  • 1
  • 2
5

I've had issues when not using the actual pear.phpunit.de channel to install PHPUnit, especially when trying to use a package manager like apt-get or yum. Try removing all the manually installed stuff you pulled from the git repository and do a pear uninstall of PHPUnit and its optional dependencies. Then:

  • sudo pear upgrade PEAR
  • sudo pear config-set auto_discover 1
  • sudo pear install --alldeps pear.phpunit.de/PHPUnit

This has worked for me ... hope it helps.


UPDATE

It probably goes without saying, but you should also purge your previous apt-get attempts as well before following the steps above:

  • sudo apt-get purge php5-pear
  • http://stackoverflow.com/questions/8949963/phpunit-pear-upgrading-errors (**with 50 points bounty**) – Eray Jan 24 '12 at 18:05
  • Well that code me a different error: `PHP Fatal error: require_once(): Failed opening required 'PHPUnit/Framework/MockObject/Autoload.php'` – mpen Jan 24 '12 at 18:31
  • "MockObject" directory doesn't exist. Funny. You'd think "alldeps" would install all the deps. Apparently not. How do I get MockObjects back? – mpen Jan 24 '12 at 18:33
  • Almost forgot -- I had the same problem. I believe the correct command is: `pear install phpunit/PHPUnit_MockObject`. lemme know if that isn't correct. **OOPS** I think that should be `pear install pear.phpunit.de/PHPUnit_MockObject` –  Jan 24 '12 at 18:36
  • `pear install phpunit/PHPUnit_MockObject` fails with "already installed" message...but the folder isn't there..hrm. Ok... uninstalled and reinstalled again, mocked objects intalled now, but now I get `Failed opening required 'PHPUnit/Framework.php'` instead. There's a Framework folder, but no Framework.php – mpen Jan 24 '12 at 18:36
  • `No releases available for package "pear.phpunit.de/PHPUnit_Framework" `. The folder is already there though, just not `Framework.php`. I'm thinking it's a version mismatch and the files aren't in the same place. – mpen Jan 24 '12 at 18:45
  • So, the */usr/share/pear/PHPUnit/Framework* (or equivalent) directory is totally empty? Can you post the output of `pear list -c phpunit`? –  Jan 24 '12 at 18:49
  • @rdlowrey: No, there's plenty of files in it. Adding the output of that command to question. – mpen Jan 24 '12 at 19:47
  • @Mark When do you receive the `Failed opening required PHPUnit/Framework.php`? When you try to run the phpunit command? This is strange because on my working PHPUnit install with the same `pear list -c phpunit` output as yours I have no `Framework.php` file **anywhere** on my machine (as verified by `locate Framework.php`) –  Jan 24 '12 at 19:54
  • @rdlowrey: Yes...when running `phpunit .` in my tests folder. `which phpunit` gives me `/usr/bin/phpunit` and `tail /usr/bin/phpunit` gives me me some stuff about `Autoload.php`; no reference to Framework.php in there. Weird...would it be trying to autoload it from the wrong place? Or...everything is installed to `/usr/share/php/PHPUnit` not to `/usr/share/pear` -- `/usr/share/pear` doesn't exist though..so it shouldn't be looking there..? I don't know what it's trying to do. – mpen Jan 24 '12 at 21:19
2

Manual Installation for Ubuntu 11.10

mkdir phpunit && cd phpunit
git clone git://github.com/sebastianbergmann/phpunit.git
git clone git://github.com/sebastianbergmann/dbunit.git
git clone git://github.com/sebastianbergmann/php-file-iterator.git
git clone git://github.com/sebastianbergmann/php-text-template.git
git clone git://github.com/sebastianbergmann/php-code-coverage.git
git clone git://github.com/sebastianbergmann/php-token-stream.git
git clone git://github.com/sebastianbergmann/php-timer.git
git clone git://github.com/sebastianbergmann/phpunit-mock-objects.git
git clone git://github.com/sebastianbergmann/phpunit-selenium.git
git clone git://github.com/sebastianbergmann/phpunit-story.git
git clone git://github.com/sebastianbergmann/php-invoker.git
cd phpunit && git checkout 3.6 && cd ..
cd dbunit && git checkout 1.1 && cd ..
cd php-code-coverage && git checkout 1.1 && cd ..
cd phpunit-mock-objects && git checkout 1.1 && cd ..
cd phpunit-selenium && git checkout 1.1 && cd ..
sudo mv ./* /usr/share/php/
ln -s /usr/share/php/phpunit/phpunit.php ~/bin/phpunit
gksudo gedit /etc/php5/cli/php.ini

Add the following to your include_path:

/usr/share/php/dbunit:/usr/share/php/php-code-coverage:/usr/share/php/php-file-iterator:/usr/share/php/php-text-template:/usr/share/php/php-timer:/usr/share/php/php-token-stream:/usr/share/php/phpunit:/usr/share/php/phpunit-mock-objects:/usr/share/php/phpunit-selenium:/usr/share/php/phpunit-story:/usr/share/php/php-invoker
mpen
  • 272,448
  • 266
  • 850
  • 1,236
1

Open /usr/share/php/PHPUnit/Framework.php and you will see this:

trigger_error(
    'Please no longer include "PHPUnit/Framework.php".', E_USER_NOTICE
);

That's all! Please no longer include "PHPUnit/Framework.php"!!!

1

Before installing PHPUnit on your system, clear pear's cache

pear clear-cache

From what i have seen, maybe you haven't integrated PHPUnit with your project. You don't need to only install PHPUnit using PEAR, i believe the files you need are not in the project.