A library that makes unit testing database-driven code in PHP a breeze. Mock the native PDO class, define query results, and inspect executed statements.

v0.8.0 2018-02-04 03:32 UTC

This package is auto-updated.

Last update: 2024-04-20 15:51:45 UTC


README

Build Status Latest Stable Version Total Downloads License

A library that makes unit testing database-driven code in PHP a breeze. Mock the native PDO class, define query results, and inspect executed statements.

"[In firefighting a] hose appliance used for splitting one line into two discharges. Often a gated wye is used to allow and disallow water flow through the two separate discharges." - https://en.wikipedia.org/wiki/Glossary_of_firefighting_equipment#Wye

Installation

Wye is registered with Packagist and can be installed with Composer. Run the following on the command line:

composer require --dev stratedge/wye

Once Wye has been included in your project, just make sure to require Composer's autoloader:

require_once 'vendor/autoload.php';

Basic Usage Example

use Stratedge\Wye\Wye;

//In test setup
//-------------

//Reset the Wye to its clean state
Wye::reset()

//Create a Wye PDO object
$pdo = Wye::makePDO();

//Inject PDO into database layer
Database::setConnection($pdo);

//In test
//-------

//Create a Result object
$result = Wye::makeResult();

//Add a row or two to return
$result->addRow(['type' => 'Pumper', 'apparatus' => 'Engine 1']);

//Attach Result to Wye to be served when a query is executed
$result->attach();

//Run code to test
$class_to_test->doSomething();

//Inspect execution
$stmt = Wye::getStatementAtIndex(0);
$this->assertStringStartsWith('SELECT', $stmt->getStatement());
$this->assertCount(2, count($stmt->getBindings());
$this->assertSame('id', $stmt->getBindings()->first()->getParameter());
$this->assertSame(1, Wye::getNumQueries());
//and more!

WAIT, THERE'S MORE
For a much more in-depth look at Wye's usage, check out the extensive documentation, especially the section on Basic Usage.

Documentation

Complete and up-to-date documentation is available on the Wiki.

Some of the major topics discussed include: An Introduction, Defining Results, Inspecting Execution Info, and Inspecting Bindings.

Todo & Roadmap

List of enhancements and implementations is available on the Todo & Roadmap page of the wiki.

Issue Tracking

If you should find an issue in Wye, and you think you can fix it, by all means please do so. Pull requests are gladly accepted. If you don't have the time or energy to fix it, please log the issue with as much detail as possible so I can take a look.

Issues can be logged on the Github issue tracker.

Acknowledgements

Wye is built on top of an idea I first saw implemented by my friend/colleague Josh.