1

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

6
  • Do you use composer for autoloading purposes in your project? Commented Jan 26, 2017 at 20:10
  • No, I don't use it Commented Jan 26, 2017 at 20:12
  • Then it is a case of simply moving require from 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. Commented Jan 26, 2017 at 20:18
  • I edit my post, I tried to use autoloader, but it dosn't work. Can you help me, how can I write autoloading? Commented Jan 26, 2017 at 20:22
  • I would recommend to use composer for autoloading, not writing the autoloader yourself. It is fairly simple. Check article phpenthusiast.com/blog/how-to-autoload-with-composer the chapter "How to autoload the PSR-4 way?". Commented Jan 26, 2017 at 20:33

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I understood. But github project doesn't work for me. How I must run project? I use phpstprm configuration and run it with phpunit-4.8.32.phar
How do you define "doesn't work"? All using command line. For autoloader version: - checkout branch master - download composer.phar into project directory wget https://getcomposer.org/composer.phar - generate autoloader files php composer.phar dump-autoload - run tests phpunit I'm using PHPUnit 4.8.31. For non-autoloader: - checkout branch global-functions - run tests phpunit
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.