0

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

Julian
  • 4,396
  • 5
  • 39
  • 51

2 Answers2

1

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?

Dirkos
  • 488
  • 1
  • 10
  • 33
  • The setUp method looks like this: `protected function setUp() { $this->object = new Calculator; }`. No real rocket science. Skeleton generator wrote it actually. Maybe it has something to do with static / non static ?? – Julian Mar 27 '19 at 15:38
  • @Julian actually that looks ok with later versions of PHPUnit. However how does the method look like at `PHPUnit\Framework\TestCase::setUp` ? Also how did you install PHPUnit? Since as far as i can see it is with a netbeans plugin? If yes which version would it be – Dirkos Mar 27 '19 at 15:43
  • I just installed it as a PHAR, used this link: https://phpunit.de/getting-started/phpunit-8.html and this: https://phpunit.de/manual/6.5/en/installation.html#installation.phar.windows – Julian Mar 27 '19 at 15:44
  • the PHPUnit plugin in Netbeans is Version: 0.26.2. – Julian Mar 27 '19 at 15:58
  • I solved the problem. PHPUnit Plugin Version 0.26.2 seems to be outdated. Therefore Skeleton Generator gives an out-dated test case too. So, I just used the example at the getting-started page and it worked. Link to getting-started page: https://phpunit.de/getting-started/phpunit-8.html – Julian Mar 29 '19 at 09:16
  • @julian, i suggest to install your dependencies with composer since they are always project specific and not locked to your IDE – Dirkos Mar 29 '19 at 14:18
0

For the same issue, I directly changed the file "NetBeansSuite.php" (C:\Users\USERNAME\AppData\Roaming\NetBeans\8.2\phpunit) as following :

class NetBeansSuite extends \PHPUnit\Framework\TestSuite {

After that, CLI and Netbeans was working.

Flavien M.
  • 21
  • 5