Skip to content

Inspecting Execution Info

Jarret Byrne edited this page May 22, 2017 · 1 revision

While Wye allows you to mock the results returned to queries so that you can execute tests without needing a database, it also provides several ways to inspect how your code is interacting with the database. With these inspection features, you can build deeper tests that check assumptions about database usage, as well as debug or learn about how your code or another library uses the database.

Get Number Of Queries Executed

You can find out how many queries your code executed through the PDO object by calling Wye::numQueries().

use Stratedge\Wye\Wye;

$num_queries = Wye::numQueries();

$this->assertSame(3, $num_queries); //Make sure we ran as many queries as we expected

Get All Executed Queries

Every PDOStatement that is executed is stored by Wye in the order they are run. To retrieve all the PDOStatements that were executed, you can call Wye::getStatements().

use Stratedge\Wye\Wye;

$statements = Wye::getStatements();

foreach ($statements as $statement) {
    //$statement is an instance of Stratedge\Wye\PDO\PDOStatement
}

Get Single Executed Query

More often than not, you will find more value in retrieving a single PDOStatement instead of all the statements. To retrieve a specific statement, pass indx of the statement to Wye::getStatementAtIndex().

use Stratedge\Wye\Wye;

$statement = Wye::getStatementAtIndex(2); //An index of 2 is the 3rd query executed

//$statement is an instance of Stratedge\Wye\PDO\PDOStatement

Inspect An Executed Query

Once you have retrieved one or more of Wye's extended PDOStatement objects, there is a variety of helpful methods that will allow you to learn more about each individual query's execution, or to test your assumptions about a particular query.

Get The SQL Statement

If you wish to inspect the actual SQL statement that was run, you can call PDOStatement::getStatement().

NOTE
The SQL statement returned by PDOStatement::getStatement() is the string given to PDO::prepare() or PDOStatement::execute(), and as such will contain the placeholders for parameter bindings, not the actual bound values.

use Stratedge\Wye\Wye;

$statement = Wye::getStatementAtIndex(0);

$sql = $statement->getStatement();

$this->assertStringStartsWith('SELECT', $sql);

Get Parameter Bindings

Whether parameter bindings are provided through PDOStatement::bindValue() or given when PDOStatement::execute() is called, Wye makes sure to record them all. For each binding you can inspect the parameter, the bound value, and the data type. Bindings are stored in a BindingCollection object, which as a Collection functions like an array with additional helper methods.

To retrieve the BindingCollection you can call PDOStatement::getBindings().

WAIT, THERE'S MORE
For more information on how to use the BindingCollection and individual Binding objects, see the documentation on Inspecting Bindings.

use Stratedge\Wye\Wye;

$statement = Wye::getStatementAtIndex(0);

$bindings = $statement->getBindings();

//$bindings is an instance of Stratedge\Wye\Collections\BindingCollection

$this->assertCount(3, $bindings);