Frank W.
Frank W.

Reputation: 787

Database test faillure with dbUnit

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.

Upvotes: 1

Views: 231

Answers (1)

Nikita U.
Nikita U.

Reputation: 3618

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.

Upvotes: 1

Related Questions