So, here is the strucuture of my project
project_name
/classes
Calculator.php
/test
CalculatorTest.php
/vendor
// Related composer/phpunit etc ... stuff
composer.json
composer.lock
Calculator.php
<?php
class Calculator{
public function double($input){
return $input * 2;
}
}
CalculatorTest.php
<?php
require_once '../classes/Calculator.php';
class CalculatorTest extends PHPUnit_Framework_TestCase{
public $testClass;
public function setUp(){
$testClass = new Calculator;
}
public function testDouble(){
$result = $testClass->double(2);
$this->assertEquals(4, $result);
}
}
When I run phpunit ../../test, I get the following error:
Warning: require_once(../classes/Calculator.php): failed to open stream: No such file or directory in C:\wamp\www\phpunit\test\CalculatorTest.php on line 3
Fatal error: require_once(): Failed opening required '../classes/Calculator.php' (include_path='.;C:\php\pear') in C:\wamp\www\phpunit\test\CalculatorTest.php on line 3
I can't find a fix. Do you have any idea?
EDIT
Got, it, forget the spl_autoload_register within the tests classes:
spl_autoload_register(function ($class) {
require_once 'classes/' . $class . '.php';
});
You're opening the file in a different page. So the path is incorrect.
try this:
require_once dirname(__FILE__) . '/classes/Calculator.php
And if Calculator is in the same folder as classes as calculator test than you dont need to set the path /classes