Reputation: 113
I have such structure of my project:
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
Upvotes: 1
Views: 2997
Reputation: 331
if you are having problems with auto loader, and tests not working, try to do what I done, and change your composer.json
to include the lines:
"autoload": {
"psr-4": {
"foo\\": "foo"
}
},
foo is the namespace. and the difference was I had psr-0 instead of psr-4.
Worked straight after!
Upvotes: 1
Reputation: 121
I've carved two versions for you.
See https://github.com/Grawer/starckoverflow-41881997
First (branch global-functions) is a version with globally declared functions, the very basic one. What was done is basically the require
is moved from file containing test to bootstrap.php
, and that's all.
Second version (branch master), with autoloader is more complicated, and I realized that you might hold on with doing it. Still, I was insisting so here it is. You was almost there. There was a typo in your composer.json
as you wrote prs-4
and it is supposed to be psr-4
. What was left was to add autoloader require to bootstrap.php
, and you would also need to put fib
function inside a class. Then you could access it, without needing to require that class in the test class file.
Upvotes: 1