Search code examples
phpnetbeans-8netbeans-pluginsphpunit

PHPUnit Plugin in Netbeans 8.2 gives Fatal error: Class 'PHPUnit_Framework_TestSuite' not found when trying to run a test case


When I try to run PHPUnit in Netbeans I face this error:

Fatal error: Class 'PHPUnit_Framework_TestSuite' not found in C:\Users\julian\AppData\Roaming\NetBeans\8.2\phpunit\NetBeansSuite.php on line 63
Done.

This happens both in Netbeans and CLI.


I started debugging this problem by navigating to this directory: C:\Users\julian\AppData\Roaming\NetBeans\8.2\phpunit\.

That directory contained one file: NetBeansSuite.php. I opened it to look for clues, and saw this line:

class NetBeansSuite extends PHPUnit_Framework_TestSuite {

What I didn't see is any concrete PHPUnit_Framework_TestSuite class.

What's next is that the NetBeansSuite.php file doesn't have any include or require language constructs that may include the PHPUnit_Framework_TestSuite class.

So, in order to fix the fatal error problem I should include the PHPUnit_Framework_TestSuite in NetbeansSuite.php.

This is a problem because I'm not the author of NetbeansSuite.php. On top of that the author of NetbeansSuite.php wrote this in the comment section:

 <b>WARNING: User changes to this file should be avoided.</b>

I read further in the comments:

 * Copyright 2010 Oracle and/or its affiliates. All rights reserved.

I guess the NetbeansSuite.php file is outdated.


Searching on the net brought me to this stackoverflow question:

Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...?

They claim that using a namespaced version of PHPUnit_Framework_TestCase should fix the problem. So I did what they wrote.

I stubbornly changed NetBeansSuite.php. Old code:

class NetBeansSuite extends PHPUnit_Framework_TestSuite {

New code:

require_once 'PHPUnit/Autoload.php';

use PHPUnit\Framework\TestCase;

class NetBeansSuite extends TestCase {

I tried to run the test case again, and this was the unfortunately result:

Fatal error: Declaration of CalculatorTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in C:\wamp64\www\test\CalculatorTest.php on line 97

In other words, it gave me a new problem.

My system:

Windows 10
WAMP
php 7.2.14
PHPUnit 8.0.6
Netbeans version 8.2 (Netbeans PHPUnit plugin installed through Tools > Plugins. Version: 0.26.2)

My question is: does anybody know how the NetBeansSuite.php file should be like with the system described above?


The setUp method in my test case:

protected function setUp()
{
    $this->object = new Calculator;
}


Update: The Skeleton Generator project is abandoned. See link: https://github.com/sebastianbergmann/phpunit-skeleton-generator So in order to fix the Declaration of CalculatorTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp() error the corresponding return type declaration should be used in the test case.

// I added : void
protected function setUp(): void
{
    $this->object = new Calculator;
}

// : void needs to be added in the tearDown method as well
protected function tearDown(): void

Unfortunately, this gave me new problems:

Warning: require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory in C:\wamp64\www\test\CalculatorTest.php on line 4

Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.;C:\php\pear') in C:\wamp64\www\test\CalculatorTest.php on line 4

I solved this by manually installing PEAR and creating a new "PHPUnit" directory in C:\php\PEAR. Then I created a new Autoload.php file. I filled the content of Autoload.php with a PSR-4 example file found at: https://www.php-fig.org/psr/psr-4/.

This solved the Autoload problem, but I faced a new problem during the execution of a test case.

C:\wamp64\www\test>phpunit CalculatorTest.php
PHPUnit 8.0.6 by Sebastian Bergmann and contributors.



Time: 170 ms, Memory: 10.00 MB

No tests executed!

It shows No tests executed! but I have 5 tests. I'll make a new question for this. Here it is: PHPUnit 8 installed on Windows through PHAR shows No tests are executed


Solution

  • Initially indeed you should update your namespaces since it has been changed in more recent versions of PHPUnit.

    The error however indicates that your CalculatorTest::setUp() method is not compatible with the PHPUnit version. Could you maybe post that method here?