Download the PHP package cspray/database-testing-phpunit without Composer
On this page you can find all versions of the php package cspray/database-testing-phpunit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cspray/database-testing-phpunit
More information about cspray/database-testing-phpunit
Files in cspray/database-testing-phpunit
Package database-testing-phpunit
Short Description A testing extension for cspray/database-testing to integrate with PHPUnit.
License
Informations about the package database-testing-phpunit
cspray/database-testing-phpunit
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.
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).
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!
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.
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.
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.