-
-
Save GRMule/a4a740b8fd5e26aac989 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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