I have such structure of my project:
- Root -- /src -- /tests
In src are my file such a Task1.php. In tests folder are my tests files. Example:
<?php
require __DIR__ . '/../src/Task1.php';
class Task1Test extends PHPUnit_Framework_TestCase
{
public function testTask1(){
$this->assertEquals([1,1,1,3,5,9],fib([1,1,1],6));
}
}
I include my src files using require __DIR__ . '/../src/Task1.php';
I want to add bootstrap.php file and include src files there. And then add bootstrap.php in phpunit.xml.
My phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" strict="true" bootstrap="bootstrap.php"></phpunit>
What should I write in bootstrap.php file?
I try add Autoloader.php file in tests folder:
<?php
class AutoLoader {
static private $classNames = array();
/**
* Store the filename (sans extension) & full path of all ".php" files found
*/
public static function registerDirectory($dirName) {
$di = new DirectoryIterator($dirName);
foreach ($di as $file) {
if ($file->isDir() && !$file->isLink() && !$file->isDot()) {
// recurse into directories other than a few special ones
self::registerDirectory($file->getPathname());
} elseif (substr($file->getFilename(), -4) === '.php') {
// save the class name / path of a .php file found
$className = substr($file->getFilename(), 0, -4);
AutoLoader::registerClass($className, $file->getPathname());
}
}
}
public static function registerClass($className, $fileName) {
AutoLoader::$classNames[$className] = $fileName;
}
public static function loadClass($className) {
if (isset(AutoLoader::$classNames[$className])) {
require_once(AutoLoader::$classNames[$className]);
}
}
}
spl_autoload_register(array('AutoLoader', 'loadClass'));
And bootsstrap.php in tests folder:
<?php
include_once('AutoLoader.php');
// Register the directory to your include files
AutoLoader::registerDirectory(__DIR__ . '/../src/');
But it doesn't work
requirefrom each test file to bootstrap.php file. Mind the paths. If you have many files and/or subdirectories it can be quite time consuming. Therefore next step I can suggest is to look into autoloading.