I get phpunit and install it as this link using the simplest way for test purposes.
I just download the phpunit.phar file, chmod & rename & move to /usr/local/bin Then, I run phpunit --version, its ok.
PHPUnit 3.7.27 by Sebastian BergmannI write a simple test
public function testSomething(){ $this -> assertTrue(true) }Then I go into the source file folder,
phpunit --colors TestIt works. So, I decide write a complex demo.
My project folder structure is like this.
Project Name --> app --> api --> testsNow I write a simple class in
app/api/FlyApi.php<?php class FlyApi { public function makeFly(){ //do something else } }Then I write another test class for FlyApi.php
<?php class FlyApiTest extends PHPUnit_Framework_TestCase { public function testFly(){ //new a FlyApi $flyApi = new FlyApi(); //do something } }At this line
$flyApi = new FlyApi()I got the error.PHP Fatal error: Class 'FlyApi' not found in /home/kevin/Workspace/fly/app/api/FlyApi.php on line 23
Yes, this line
$flyApi = new FlyApi()
3 Answers
Are you using Laravel's phpunit.xml file? It includes Laravel's (and Composer's) autoload.php file which lets you use all your autoloaded classes within it.
Finally, what's the whole error? It should (hopefully) tell you what class it's trying to load (which will give you clues if the namespace is wrong or something).
FlyApibecause PHPUnit does not do some magic auto-loading for you. It's your job to provide the unit under test before testing it (or if the class yet does not exist: it's perfectly okay that the test fails with a crash of Phpunit as the test has been written before the code as in TDD).