Skip to content

Instantly share code, notes, and snippets.

@FMCorz
Created November 13, 2017 04:57
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 FMCorz/26fe0d57ad3dcbc196271e74565967d0 to your computer and use it in GitHub Desktop.
Save FMCorz/26fe0d57ad3dcbc196271e74565967d0 to your computer and use it in GitHub Desktop.
Mocking a static method call
<?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