Skip to content

Instantly share code, notes, and snippets.

@GRMule
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GRMule/a4a740b8fd5e26aac989 to your computer and use it in GitHub Desktop.
Save GRMule/a4a740b8fd5e26aac989 to your computer and use it in GitHub Desktop.
<?php
// given a class that looks like this:
class User {
public function __construct($id=false) {
if ($id > 0) {
$userRow = db::getRow('SELECT * FROM users WHERE id = '.$id);
if (!$userRow)
return false;
foreach ($userRow as $k=>$v){
if (property_exists($this, $k)){
$this->$k = $v;
}
}
}
}
}
// mock the DB static
class db {
static public $fail = false;
static private $testValues = array(
'user'=>array('first_name'=>'Bob', 'last_name'=>'Franklin'),
'otherThing'=>array('valid'=>'values')
);
static public $testWhat = 'user';
static function getRow($sql){
if (self::$fail === false)
return self::$testValues[self::$testWhat];
return array();
}
}
// in tests:
// works
db::$testWhat = 'user';
$SUT = new User(10);
//... test continues
// failing test
db::$fail = true;
db::$testWhat = 'user';
$SUT = new User(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment