1

I'm trying to execute some databasetests with php unit but I keep getting the following errors: following errors:

My composer.json look like this:

{
    "require": {
    "phpunit/phpunit": "4.8",
    "phpunit/dbunit": "^2.0"
}

This is the page I'm trying to perform the test on:

<?php 
require_once 'autoload.php';

class testDatabase extends \PHPUnit_Extensions_Database_TestCase {


public function getConnection() {
    $db = new PDO(
        "mysql:host=localhost;dbname=mrf", 
        "", "");
    return $this->createDefaultDBConnection($db, "bulletproof");
}

}
?>

I use the sql autoloader to load the main php classes and that looks like the following:

<?php 
spl_autoload_register(function($class) {
    require_once '../classes/' . $class . '.php';
});
?>

In my opinion, the strange thing is that the regular phpunit tests do run, but the dbUnit test do not. I tried several upgrades of composer, phpUnit and dbUnit downloaded from packagist but without succes.

Would be great if you could help me out, Thanks in advance.

Frank W.
  • 777
  • 3
  • 14
  • 33

1 Answers1

1

When you run composer install, composer generates file PROJECT_ROOT/vendor/autoload.php. That file contains all stuff needed for autoloading your project dependencies, so based on what I see on the screenshot, you need to require_once it as well as your own.


As a side note: phpunit has an option --bootstrap=filename, which is executed before all tests run. You can run

phpunit --bootstrap=PROJECT_ROOT/vendor/autoload.php databaseTest.php

After that you can create something like phpunit_bootstrap.php, put your require_once 'autoload.php'; here and composers vendor/autoload.php so you can run tests like

phpunit --bootstrap=path/to/your/phpunit_bootstrap.php AnythingYouWantToTest.php

without need to insert require_once 'autoload.php'; in every test file.

Nikita U.
  • 3,540
  • 1
  • 28
  • 37
  • Hi there, Thanks for your answer. When I do like you say and require the `vendor/autoload.php` I get the following Error: `Argument #3 of of phpunit_textUI_resultprinter::construct--() must be a value from "never", "auto" or "always"` I searched this error and moved phpunit from `require-dev` to `dev`. Unfortunately no result. I run phpunit 5.3.2 right now. You have any idea's? Thanks! – Frank W. May 12 '16 at 06:02
  • Do you run `vendor/bin/phpunit` or your system-wide installed version? Unfortunately no ideas from the first view except guesses. Maybe it deserves another question on this site – Nikita U. May 12 '16 at 06:30
  • yes it is. Hmm to bad... I really liked the idea of testdriven development but untill not it has been a pain to get it to work :P Thanks for your help and I will open another question. – Frank W. May 12 '16 at 06:32