0

am trying to write my first test in PHPUnit in Netbeans. I tried to follow the example on the Netbeans website http://netbeans.org/kb/docs/php/phpunit.html but I keep getting the error below. I dont have a clue what is happening as I did everything just the way I was instructed on the website.

> PHP Fatal error:  Class 'Calculator' not found in C:\wamp\www\Calculator\tests\CalculatorTest.php on line 18
PHP Stack trace:
PHP   1. {main}() C:\wamp\bin\php\php5.3.8\phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() C:\wamp\bin\php\php5.3.8\phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() C:\wamp\bin\php\php5.3.8\pear\PHPUnit\TextUI\Command.php:130
PHP   4. PHPUnit_TextUI_TestRunner->doRun() C:\wamp\bin\php\php5.3.8\pear\PHPUnit\TextUI\Command.php:192
PHP   5. PHPUnit_Framework_TestSuite->run() C:\wamp\bin\php\php5.3.8\pear\PHPUnit\TextUI\TestRunner.php:325
PHPUnit 3.6.12 by Sebastian Bergmann.
PHP   6. PHPUnit_Framework_TestSuite->run() C:\wamp\bin\php\php5.3.8\pear\PHPUnit\Framework\TestSuite.php:705
PHP   7. PHPUnit_Framework_TestSuite->runTest() C:\wamp\bin\php\php5.3.8\pear\PHPUnit\Framework\TestSuite.php:745
PHP   8. PHPUnit_Framework_TestCase->run() C:\wamp\bin\php\php5.3.8\pear\PHPUnit\Framework\TestSuite.php:772
PHP   9. PHPUnit_Framework_TestResult->run() C:\wamp\bin\php\php5.3.8\pear\PHPUnit\Framework\TestCase.php:751
PHP  10. PHPUnit_Framework_TestCase->runBare() C:\wamp\bin\php\php5.3.8\pear\PHPUnit\Framework\TestResult.php:649
PHP  11. CalculatorTest->setUp() C:\wamp\bin\php\php5.3.8\pear\PHPUnit\Framework\TestCase.php:801

Any help would be appreciated to help me understand what am doing wrong please.

Screen shot enter image description here

0

2 Answers 2

7

You should make sure that the file containing the class Calculator can be found by the unittest.

require_once dirname(__FILE__) . '/../Calculator.php';

The test class is placed in a subdirectory, so you need to declare the path relative to the file, containing the unittest. Maybe your application uses autoloading, but your unittest doesn't know about it.

If you use Netbeans to create the unittest, the path should already be correct.

Sign up to request clarification or add additional context in comments.

5 Comments

Bro U legend like.Spent all night till this morning at this and here you gave me the answer. We should be best friends forever. Thanks.
You are welcome! Did you know, that in NetBeans you can right click on a file (in the tree), and then select Tools/Create PHPUnit tests? It will generate an empty unittest for you.
That was what I did from the tutorial from netbeans website. Netbeans generated the test for me automatically,but I must have done something silly while going through the tutorial. Thanks once again and have the best day ever.
Spent a whole day and this solved my problem.
Thanks this answer helped me too. Not sure why it didn't add that line automatically. 1 vote from me.
0
PHP Fatal error:  Class 'Calculator' not found

If you did the same as in the webpage, why can Calculator be loaded in the web?

Try including the files he is complaining about.

4 Comments

But the file it is complaining about is there. In the right folder.I uploaded a screen shot. Please have a look at it above
So the file is sitting around in the source folder. Did you include it?
The file is actually in the right folder. The test class couldn't finding the the main class so I added this line of code require_once dirname(__FILE__) . '/../Calculator.php'; that @martinstoeckli suggested into my test class and it worked. Thanks for your help and have a great day.
I told you the same just didn't know the path to the file. I am glade it's solved. Have fun!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.