thijsferyn/pdomocker

There is no license information available for the latest version (v0.1.4) of this package.

A PDO mocking framework for PHPUnit

v0.1.4 2015-12-07 13:07 UTC

This package is auto-updated.

Last update: 2024-11-04 20:30:15 UTC


README

Build Status

PDOMocker

A PDO mocking wrapper around PHPUnit_MockObject

Example code

<?php
namespace PDOMocker;

require_once __DIR__.'/vendor/autoload.php';

$rows = [
    new Row(['id'=>1, 'name'=>'someValue'],false),
    new Row(['id'=>2, 'name'=>'someOtherValue'],false)
];

$updatedRows = [
    new Row(['id'=>1, 'name'=>'newValue'],false),
    new Row(['id'=>2, 'name'=>'otherNewValue'],false)
];

$exception = new \PDOException('someError');

$mocker = new Mocker(); 
$mocker
    ->registerQuery(new Query\Select('SELECT * FROM someTable WHERE id=1',[$rows[0]]))
    ->registerQuery(new Query\Select('SELECT * FROM someTable',$rows))    
    ->registerQuery(new Query\Insert("INSERT INTO someTable (id,name) VALUES (1,'someValue')",[$rows[0]]))
    ->registerQuery(new Query\Update('UPDATE someTable WHERE id=1',[$rows[0]],[$updatedRows[0]]))    
    ->registerQuery(new Query\Insert("INSERT INTO otherTable (id,name) VALUES (1,'someValue')",[],$exception))
    ->registerQuery(new Query\Delete('DELETE FROM someTable WHERE id=1',[$rows[0]]));        
        
$pdo = $mocker->getMock();  

$stmt = $pdo->query("SELECT * FROM someTable WHERE id=1");
/**
 * array(0) {
 * }
 */
var_dump($stmt->fetchAll());

$pdo->query("INSERT INTO someTable (id,name) VALUES (1,'someValue')"); 

$stmt = $pdo->query("SELECT * FROM someTable WHERE id=1");
/**
 * array(2) {
 *  [0] =>
 *  array(2) {
 *    'id' =>
 *    int(1)
 *    'name' =>
 *    string(9) "someValue"
 *  }
 *}
 */
var_dump($stmt->fetchAll());

$pdo->query('UPDATE someTable WHERE id=1');               
 
$stmt = $pdo->query("SELECT * FROM someTable WHERE id=1");
/**
 * array(2) {
 *  [0] =>
 *  array(2) {
 *    'id' =>
 *    int(1)
 *    'name' =>
 *    string(9) "newValue"
 *  }
 *}
 */
var_dump($stmt->fetchAll());

$pdo->query('DELETE FROM someTable WHERE id=1');                

/**
 * array(0) {
 * }
 */
$stmt = $pdo->query("SELECT * FROM someTable WHERE id=1");
var_dump($stmt->fetchAll());

/**
 * string(9) "someError"
 */
try {
    $pdo->query("INSERT INTO otherTable (id,name) VALUES (1,'someValue')");    
} catch(\PDOException $e) {
    var_dump($e->getMessage());
}