Created
November 13, 2017 04:57
-
-
Save FMCorz/26fe0d57ad3dcbc196271e74565967d0 to your computer and use it in GitHub Desktop.
Mocking a static method call
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 | |
// Before. | |
class Db { | |
public static function query() { | |
// Do the request. | |
return $result; | |
} | |
} | |
// After. | |
class Db { | |
public static function query($sql, $params) { | |
if (!self::$instance) { | |
self::$instance = new NonStaticDb(); | |
} | |
return self::$instance->query($sql, $params); | |
} | |
public static function mockWith($instance) { | |
self::$instance = $instance; | |
} | |
} | |
class NonStaticDb { | |
public function query() { | |
// Do the request. | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment