cspray/database-testing-phpunit

There is no license information available for the latest version (dev-main) of this package.

A testing extension for cspray/database-testing to integrate with PHPUnit.

dev-main 2025-05-09 15:22 UTC

This package is auto-updated.

Last update: 2025-05-09 15:43:17 UTC


README

A PHPUnit Extension for cspray/database-testing, designed to facilitate setting up a test database suitable for automated testing.

Installation

Composer is the only supported method for installing this library.

composer require --dev cspray/database-testing-phpunit

Please note, it is highly likely you'll need a library that provides an implementation of Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapter! This library does not come with the adapter appropriate for your database connection!

Review the tests!

If you're here to learn more about how to use cspray/databse-testing in your PHPUnit tests, check out the tests in this library! The phpunit.xml config and the implemented tests provide a working example on how to use it!

Quick Start

The examples below uses a ConnectionAdapter and ConnectionAdapterFactory available from cspray/database-testing-pdo. If PDO is not an appropriate connection for your use case, please replace with the appropriate ConnectionAdapter implementation.

Register your Extension

The first step is to register the extension in your PHPUnit configuration. Ensure the following XML is present in phpunit.xml (or the file you use for you PHPUnit config).

<extensions>
    <bootstrap class="Cspray\DatabaseTesting\PhpUnit\DatabaseTestingExtension" />
</extensions>

Attribute your TestCase

The next step is to add the #[RequiresTestDatabase] attribute to your PHPUnit TestCase. There's no inheritance or traits required, you don't need to change what TestCase you extend!

<?php declare(strict_types=1);

namespace Cspray\DatabaseTesting\PhpUnit\Demo;

use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
use PHPUnit\Framework\TestCase;

#[RequiresTestDatabase(
    new SqliteConnectionAdapterFactory(
        ':memory:',
        '/path/to/my/schema'
    ),
    new TransactionWithRollback()
)]
final class MyDemoTest extends TestCase {

}

Inject the TestDatabase

To work with your test database there is a helper class with the type, Cspray\DatabaseTesting\TestDatabase. This object allows accessing the underlying test database connection, load fixtures, and access the data in a given table.

This object is managed by the extension. It is created when your TestCase has started, before any tests or setup have run. This property MUST BE static, the extension does not have access to the TestCase instance that is running.

<?php declare(strict_types=1);

namespace Cspray\DatabaseTesting\PhpUnit\Demo;

use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
use Cspray\DatabaseTesting\PhpUnit\InjectTestDatabase;
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
use Cspray\DatabaseTesting\TestDatabase;
use PHPUnit\Framework\TestCase;

#[RequiresTestDatabase(
    new SqliteConnectionAdapterFactory(
        ':memory:',
        '/path/to/my/schema'
    ),
    new TransactionWithRollback()
)]
final class MyDemoTest extends TestCase {

    #[InjectTestDatabase]
    private static TestDatabase $testDatabase;

}

Load Fixtures and Run Test

In our example, we're going to load tests both in setUp and as an attribute on a given test. This example is meant to show in what order fixtures get ran, and to show that there are alternatives to loading fixtures if you do not like attributes.

<?php declare(strict_types=1);

namespace Cspray\DatabaseTesting\PhpUnit\Demo;

use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\Fixture\LoadFixture;
use Cspray\DatabaseTesting\Fixture\SingleRecordFixture;
use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
use Cspray\DatabaseTesting\PhpUnit\InjectTestDatabase;
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
use Cspray\DatabaseTesting\TestDatabase;
use PHPUnit\Framework\TestCase;

#[RequiresTestDatabase(
    new SqliteConnectionAdapterFactory(
        ':memory:',
        '/path/to/my/schema'
    ),
    new TransactionWithRollback()
)]
final class MyDemoTest extends TestCase {

    #[InjectTestDatabase]
    private static TestDatabase $testDatabase;

    protected function setUp() : void{
        parent::setUp(); // TODO: Change the autogenerated stub
        self::$testDatabase->loadFixtures([
            new SingleRecordFixture('my_table', ['name' => 'from setup'])
        ]);
    }
    
    #[LoadFixture(
        new SingleRecordFixture('my_table', ['name' => 'from attribute'])
    )]
    public function testTableHasCorrectRecords() : void {
        $table = self::$testDatabase->table('my_table');
        
        self::assertCount(2, $table);
        self::assertSame('from attribute', $table->row(0)->get('name'));
        self::assertSame('from setup', $table->row(1)->get('name'));
    }

}

Generally speaking, I would not recommend mixing approaches to loading fixtures. Either use the #[LoadFixture] Attribute, or use TestDatabase helper in your setup or test methods.