48

I've got a fresh copy of PHPUnit installed on my system (Ubuntu 11), but whenever I type phpunit in the console I get the following error:

PHP Fatal error: Call to undefined method PHP_CodeCoverage_Filter::getInstance() in /usr/bin/phpunit on line 39

I have PHPUnit's code coverage installed, as far as I know:

>sudo pear install phpunit/PHP_CodeCoverage

phpunit/PHP_CodeCoverage is already installed and is the same as the released version 1.1.1

install failed

Why am I getting this error and how can I fix it?

Ian Hunter
  • 9,466
  • 12
  • 61
  • 77
  • 1
    What versions of PHPUnit is installed? `pear info phpunit/PHPUnit` It seems that the `phpunit` executable script was not updated to 3.6.x. – David Harkness Feb 08 '12 at 01:05
  • `Release Date 2012-01-27 10:49:19 Release Version 3.6.10 (stable)` – Ian Hunter Feb 08 '12 at 01:26
  • 1
    As of the end of 2014 you cannot install phpunit via pear at all anymore. https://github.com/sebastianbergmann/phpunit/wiki/End-of-Life-for-PEAR-Installation-Method – siliconrockstar Feb 20 '15 at 03:36

7 Answers7

118

Ubuntu 11.10 has had an issue for a while that hasn't been fixed. This is the only thing that will get phpunit to work with pear. (Outside of using pear you can look up a way to do it without pear. There is an article online about that but I wouldn't want that kind of burden to do it manually). This is the only thing that worked for me:

sudo apt-get remove phpunit

sudo pear channel-discover pear.phpunit.de

sudo pear channel-discover pear.symfony-project.com

sudo pear channel-discover components.ez.no

sudo pear update-channels

sudo pear upgrade-all

sudo pear install --alldeps phpunit/PHPUnit

sudo pear install --force --alldeps phpunit/PHPUnit
NicJ
  • 4,070
  • 1
  • 25
  • 18
Anthony
  • 1,189
  • 2
  • 6
  • 2
  • It also worked for my (old) 12.04 insall (10.04 -> 11.04 -> 11.10 -> 12.04) – Jens Jun 10 '12 at 09:10
  • 1
    Supergreen, saved me a lot of time! Works like a charm. – Pavel S. Jun 17 '12 at 11:52
  • If you are troubleshooting from another install, maybe you get a bash: not found message. Just open another tab or terminal to fix it. – Ricardo Martins Oct 11 '12 at 20:52
  • 3
    I found another solution that didn't have that last line (`sudo pear install --force --alldeps phpunit/PHPUnit`). By running that last line, finally, it instantly worked. – Tyler Collier Oct 17 '12 at 00:08
34

The executable script that loads PHPUnit must not have been updated when going to 3.6.x. Reinstall it.

sudo pear uninstall phpunit/PHPUnit
sudo pear install phpunit/PHPUnit

If this doesn't work, make sure PEAR itself is up-to-date.

Community
  • 1
  • 1
David Harkness
  • 35,992
  • 10
  • 112
  • 134
23

For some, Anthony's solution will not work fully because of the Unknown remote channel: pear.symfony.com or phpunit/PHPUnit requires package "channel://pear.symfony.com/Yaml".

SO here is the upgraded solution that solves this:

sudo apt-get remove phpunit

sudo pear channel-discover pear.phpunit.de

sudo pear channel-discover pear.symfony-project.com

sudo pear channel-discover components.ez.no

sudo pear channel-discover pear.symfony.com

sudo pear update-channels

sudo pear upgrade-all

sudo pear install pear.symfony.com/Yaml

sudo pear install --alldeps phpunit/PHPUnit

sudo pear install --force --alldeps phpunit/PHPUnit
Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261
  • After a LOT of fishing around to get phpUnit working on my Ubuntu 12.04 VM, this solution worked for me. Thanks! – J. LaRosee Sep 17 '13 at 18:09
  • Fixed Ubuntu 12.04 - don't see what everyone was complaining about, that was *so* simples! – jmc Sep 27 '13 at 13:11
1

The method getInstance() seems to have been dropped from the class. https://github.com/sebastianbergmann/php-code-coverage/blob/master/PHP/CodeCoverage/Filter.php#L78

Use the constructor instead if you come across this error. However, this is not applicable to the opening post as the command came from PHPUnit itself.

Tails
  • 636
  • 7
  • 16
1

it works for me. at the beginning, I didn't use --force for the last command, I got a fatal error that "Call to undefined method PHP_CodeCoverage_Filter::getInstance". Then I used --force, which resolved this issue.

casilin
  • 11
  • 2
0

While i had the same problem and managed to solve it using setting correct environment variables.

You can get to the solve here http://rkrants.blogspot.in/2013/01/installing-phpunitpear-in-ubuntu-1210.html

In short I had to re-install PHPUnit using pear after setting the variables correct.

I used a Ubuntu 12.10 installation and it works perfectly now.

Rabimba Karanjai
  • 186
  • 2
  • 5
  • 14
0

I came across the same problem, managed to solve it using composer

Try these steps -

First uninstall phpunit

sudo apt-get remove phpunit

Install composer - http://getcomposer.org/doc/01-basic-usage.md#installation

$ curl -sS https://getcomposer.org/installer | php

Install phpunit - http://phpunit.de/manual/3.7/en/installation.html

For a system-wide installation via Composer, you can run:

$ composer global require 'phpunit/phpunit=3.7.*'

You will also have to make sure that you have ~/.composer/vendor/bin/ in your path.

Adrian Wragg
  • 7,311
  • 3
  • 26
  • 50