8

I'm following the documentation of PHPUnit; I'm trying the following example; but it keeps giving me the following error:

"Fatal error: Class "PHPUnit_Extensions_Database_TestCase" not found!

It is working fine for PHPUnit_Framework_TestCase! I also added

require_once 'PHPUnit/Autoload.php';

but still the same error!

<?php
class MyGuestbookTest extends PHPUnit_Extensions_Database_TestCase
{
    /**
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
     */
    public function getConnection()
    {
        $pdo = new PDO('sqlite::memory:');
        return $this->createDefaultDBConnection($pdo, ':memory:');
    }

    /**
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
     */
    public function getDataSet()
    {
        return $this->createFlatXMLDataSet(dirname(__FILE__).'/_files/guestbook-seed.xml');
    }
}
?>

Updated:

Actually I'm very new to PHPUnit test:

I used Slim framework of PHP: I have not started testing; just trying to understand the logic by going through the documentation; I do have a composer.json file which is the following: (now I added "phpunit/dbunit": ">=1.2" in require / require-dev, but none of them solved the problem!):

{
    "name": "slim/slim",
    "type": "library",
    "description": "Slim Framework, a PHP micro framework",
    "keywords": ["microframework","rest","router"],
    "homepage": "http://github.com/codeguy/Slim",
    "license": "MIT",
    "authors": [
        {
            "name": "Josh Lockhart",
            "email": "[email protected]",
            "homepage": "http://www.joshlockhart.com/"
        }
    ],
    "require": {        
        "php": ">=5.3.0"
    }
    ,"require-dev": {        
        "phpunit/dbunit": ">=1.2"
    },
    "suggest": {
        "ext-mcrypt": "Required for HTTP cookie encryption"
    },
    "autoload": {
        "psr-0": { "Slim": "." }
    }
}

If you need more clarification, please let me know and thank you in advance.

7
  • I'm curious about the lowercase "d" in "database" in your error message. Are you sure you've spelt the class name correctly? Commented Jun 2, 2014 at 1:21
  • Yes! I just did not copy & paste correctly here! I corrected it here! I spelled it correctly and thanks for editing! Commented Jun 2, 2014 at 1:23
  • 6
    Have you included the DBUnit optional package in your composer file (assuming you're using composer)? Commented Jun 2, 2014 at 1:26
  • I do have a composer.json file which now I added in my updated question now I added "phpunit/dbunit": ">=1.2" in require and require-dev, but none of them solved the problem! Commented Jun 2, 2014 at 1:39
  • Should I hard-code it in the way that I did now? Commented Jun 2, 2014 at 1:54

1 Answer 1

8

If you are using Composer, make sure you add the DBUnit optional package.

All credit goes to Phil's comment!

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

2 Comments

I'm getting a warning about PHPUnit_Extensions_Database_TestCase on one of my projects. I tried adding DBUnit with the composer require --dev phpunit/dbunit command, but I'm getting an error because I'm not using phpunit 6.0. Why exactly did I need to include this in my project? Apparently something I'm doing is triggering it. This particular project does have database access, but I don't have tests on database access yet.
Jay, you probably need to install a version of DBUnit compatible with your version of PHPUnit. For instance, I am using PHPUnit v5.7 so I needed to install DBUnit v2.0.3: composer require --dev phpunit/dbunit:2.0.3. Source

Your Answer

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