Download the PHP package cspray/database-test-case without Composer
On this page you can find all versions of the php package cspray/database-test-case. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package database-test-case
DatabaseTestCase
A library to facilitate testing database interactions using PHPUnit 10+.
Features this library currently provides:
- Handles typical database setup and teardown
- Simple representation of a table's rows
- Mechanism for loading fixture data specific to each test
Features this library does not currently provide, but plans to:
- Semantic assertions on the state of a database
- Representation for the information schema of a given table
The rest of this document details how to install this library, make use of its TestCase
, and what database
connection objects are supported out-of-the-box.
Installation
Composer is the only supported method for installing this library.
Usage Guide
Using this library starts by creating a PHPUnit test that extends Cspray\DatabaseTestCase\DatabaseTestCase
. This class
overrides various setup and teardown functions provided by PHPUnit to ensure that a database connection is established
and that database interactions happen against a known state. The DatabaseTestCase
requires implementations
to provide a Cspray\DatabaseTestCase\ConnectionAdapter
. This implementation is ultimately responsible for calls to the
database required by the testing framework. The ConnectionAdapter
also provides access to the underlying connection,
for example a PDO
instance, that you can use in your code under test. Check out the section titled "Database Connections"
for ConnectionAdapter
instances supported out-of-the-box and how you could implement your own.
In our example, going to assume that you have a PostgreSQL database with a table that has the following DDL:
Now, we can write a series of tests that interact with the database.
TestCase Hooks
There are several critical things the DatabaseTestCase
must take care of for database tests to work properly. To do that
we must do something in all the normally used PHPUnit TestCase
hooks. To be clear those methods are:
TestCase::setUpBeforeClass
TestCase::setUp
TestCase::tearDown
TestCase::tearDownAfterClass
To make sure that DatabaseTestCase
processes these hooks correctly they have been marked as final
. There are new
methods that have been provided that allow for the same effective hooks.
Old Hook | New Hook |
---|---|
TestCase::setUpBeforeClass |
DatabaseTestCase::beforeAll |
TestCase::setUp |
DatabaseTestCase::beforeEach |
TestCase::tearDown |
DatabaseTestCase::afterEach |
TestCase::tearDownAfterClass |
DatabaseTestCase::afterAll |
Database Connections
Connection Adapter | Connection Instance | Library | Database | Implemented |
---|---|---|---|---|
Cspray\DatabaseTestCase\PdoConnectionAdapter |
PDO |
PHP PDO | PostgreSQL | :white_check_mark: |
Cspray\DatabaseTestCase\PdoConnectionAdapter |
PDO |
PHP PDO | MySQL | :white_check_mark: |
Cspray\DatabaseTestCase\AmpPostgresConnectionAdapter |
Amp\Postgres\PostgresLink |
amphp/postgres@^2 | PostgreSQL | :white_check_mark: |
Amp\Mysql\MysqlLink |
amphp/mysql@^3 | MySQL | :x: |