-
Notifications
You must be signed in to change notification settings - Fork 0
Defining Results
In order to define a new result for a query, you must first create a new Result
object. You can create a new Result
object by calling Wye::makeResult()
.
use Stratedge\Wye\Wye;
$result = Wye::makeResult(); //Returns new Stratedge\Wye\Result object
A Result
object is useless until it is attached to the Wye. Results are used in the order that they are attached, and they can be attached as many times as desired. More results can be attached than queries executed - the extras will not be used.
use Stratedge\Wye\Wye;
//The Result object can attach itself
$result->attach();
//Alternatively, you can add the object directly to the Wye
Wye::addResult($result);
Being able to define the rows that an executed query returns is probably the single most useful feature of Wye. When you control the rows that are returned, you can craft specific scenarios for testing.
There are a few options for defining rows on the Result
object, allowing you to choose the methods you find easiest or most readable as you wish.
Rows defined as an associative array use keys for column names and values as column values.
Adding a single row can be done by calling Result::addRow()
.
use Stratedge\Wye\Wye;
$result = Wye::makeResult();
$result->addRow(['type' => 'Pumper', 'apparatus' => 'Engine 8'])->attach();
//$result now has 1 row
NOTE
You may callResult::addRow()
multiple times - a new row is added with each call.
To bulk-add multiple rows in one call, you can use Result::addRows()
. The method accepts one or more rows as individual arguments.
use Stratedge\Wye\Wye;
$result = Wye::makeResult();
$result->addRows(
['type' => 'Squad', 'apparatus' => 'Rescue 2'],
['type' => 'Quint', 'apparatus' => 'Ladder 3']
)->attach();
//$result now has 2 rows
In order to use an indexed array to define one or more rows, the corresponding column names must be set separately since the indexed array doesn't have meaningful keys to use as column names.
To add a single row as an indexed array, first define the columns with Result::setColumns()
, and then pass your indexed array to Result::addRow()
.
use Stratedge\Wye\Wye;
$result = Wye::makeResult();
//2 ways to define columns...
$result->setColumns('type', 'apparatus'); //Columns defined as individual parameters
$result->setColumns(['type', 'apparatus'); //Or columns defined in an indexed array
//With columns defined, addRow() accepts an indexed array
$result->addRow(['Tender', 'Tanker 3'])->attach();
NOTE
You may callResult::addRow()
multiple times - a new row is added with each call.
To add multiple rows as indexed arrays, first define the columns with Result::setColumns()
, and then pass your indexed arrays as individual parameters to Result::addRows()
.
use Stratedge\Wye\Wye;
$result = Wye::makeResult();
//2 ways to define columns...
$result->setColumns('type', 'apparatus'); //Columns defined as individual parameters
$result->setColumns(['type', 'apparatus'); //Or columns defined in an indexed array
//With columns defined, addRow() accepts an indexed array
$result->addRows(
['Tender', 'Tanker 3'],
['Pumper', 'Engine 6']
)->attach();
When inserting new rows with auto-incrementing primary keys, many database systems will allow you to get the ID of the last inserted row. Since Wye does not actually create any new rows, you can define an ID value yourself. Set the last-insert-ID for any results used for inserts with Result::setLastInsertId()
.
use Stratedge\Wye\Wye;
$result = Wye::makeResult();
$result->setLastInsertId(54)->attach();
//PDO::lastInsertId() will now return 54 for this result
By default, when PDO::rowCount()
is called the count of rows that have been defined on the Result
object will be returned. If you wish to provide a specific number of rows, you can override that value with Result::setNumRows()
.
use Stratedge\Wye\Wye;
$result = Wye::makeResult();
$result->setNumRows(7)->attach();
//PDOStatement::rowCount() will now return 7 for this result