I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test.
I would like to have all my classes autoloaded into each unit test without having to use include
or require
at the top of my test but this is proving to be difficult!
This is what my directory structure looks like (a trailing / slash indicates a directory, not a file):
- composer.json
- composer.lock
- composer.phar
- lib/
- tests/
- vendor/
- bin/
- composer/
- phpunit/
- symfony/
- autoload.php
My composer.json file includes the following:
"require": {
"phpunit/phpunit": "3.7.*",
"phpunit/phpunit-selenium": ">=1.2"
}
My returning.php class file includes the following:
<?php
class Returning {
public $var;
function __construct(){
$this->var = 1;
}
}
?>
My returningTest.php test file includes the following:
<?php
class ReturningTest extends PHPUnit_Framework_TestCase
{
protected $obj = null;
protected function setUp()
{
$this->obj = new Returning;
}
public function testExample()
{
$this->assertEquals(1, $this->obj->var);
}
protected function tearDown()
{
}
}
?>
However, when I run ./vendor/bin/phpunit tests
from the command-line, I get the following error:
PHP Fatal error: Class 'Returning' not found in
/files/code/php/db/tests/returningTest.php on line 8
I noticed that composer
produced an autoload.php
file in vendor/autoload.php
but not sure if this is relevant for my problem.
Also, in some other answers on Stack Overflow people have mentioned something about using PSR-0 in composer and the namespace
command in PHP, but I have not been successful in using either one.
Please help! I just want to autoload my classes in PHPUnit so I can just use them to create objects without worrying about include
or require
.
Update: 14th of August 2013
I have now created an Open Source project called PHPUnit Skeleton to help you get up and running with PHPUnit testing easily for your project.
See Question&Answers more detail:
os