Skip to content

Instantly share code, notes, and snippets.

@Lukas0025
Last active June 16, 2021 15:38
Show Gist options
  • Save Lukas0025/ad50008162233099541686a9fe1060f6 to your computer and use it in GitHub Desktop.
Save Lukas0025/ad50008162233099541686a9fe1060f6 to your computer and use it in GitHub Desktop.
mysqli mockup for testing
<?php
/**
* mysqli mockup for testing
* @autor Lukáš Plevač <lukas@plevac.eu>
* @date 16.6.2021
* CC0 license - No Rights Reserved.
*
*/
/**
* Mockup for mysqli connection
*/
class mysqli_mockup {
/**
* array with rules from canPrepare function
*/
private $_prepareArr = [];
/**
* Add command what database can prepare
* @param $array with command what can prepere, bind values, and result example:
* [
* 'prepare' => "SELECT * FROM profile WHERE fbid = ?",
* 'bind' => ["s", ["1d028378e12ca6bdafa3b8b21bc5a9ea"]],
* 'result' => [
* 'name' => 'Lukáš Plevač',
* 'email' => 'lukas@plevac.eu'
* ]
* ]
* @return null
*/
public function canPrepare($array) {
array_push($this->_prepareArr, $array);
}
/**
* Clear all commands what can database prepare
* @return null
*/
public function clearCanPrepare() {
$this->_prepareArr = [];
}
/**
* Find rule by command
* @param $rule string wih SQL command of rule
* @return array of rule from function canPrepare
*/
private function findRule($rule) {
foreach ($this->_prepareArr as $saverule) {
if ($saverule['prepare'] == $rule) {
return $saverule;
}
}
return false;
}
/**
* Fake function for Mysqli
*/
public function prepare($string) {
$rule = $this->findRule($string);
if (!$rule) {
throw new Exception("Cant find rule for {$string} prepare");
}
return new prepare_mockup($rule);
}
}
class prepare_mockup {
/**
* rule from mysqli_mockup::canPrepare
*/
private $_rule;
/**
* @param $rule rule from mysqli_mockup::canPrepare
*/
function __construct($rule) {
$this->_rule = $rule;
}
/**
* Fake function for Mysqli
*/
public function bind_param($types, ...$args) {
if ($types <> $this->_rule['bind'][0]) {
throw new Exception("Arg types not match with {$types} expected {$this->_rule['bind'][0]}");
}
for ($i = 0; $i < count($args); $i++) {
if ($args[$i] <> $this->_rule['bind'][1][$i]) {
throw new Exception("Arg value not match on postion {$i} with {$args[$i]} expected {$this->_rule['bind'][1][$i]}");
}
}
}
/**
* Fake function for Mysqli
*/
public function execute() {}
/**
* Fake function for Mysqli
*/
public function get_result() {
if (count($this->_rule['result']) > 0) {
return new results_mockup($this->_rule);
} else {
return false;
}
}
}
class results_mockup {
/**
* rule from mysqli_mockup::canPrepare
*/
private $_rule;
/**
* @param $rule rule from mysqli_mockup::canPrepare
*/
function __construct($rule) {
$this->_rule = $rule;
}
/**
* Fake function for Mysqli
*/
public function fetch_array() {
return $this->_rule['result'];
}
}
/* --------------------- Usage ------------------------*/
//setup test
$database_mock = new mysqli_mockup();
$database_mock->canPrepare([
'prepare' => "SELECT * FROM profile WHERE fbid = ?",
'bind' => ["s", ["1d028378e12ca6bdafa3b8b21bc5a9ea"]],
'result' => [
'name' => 'Lukáš Plevač',
'email' => 'lukas@plevac.eu',
'emailver' => 1
]
]);
//now just test
$link = $database_mock;
//$link = mysqli_connect($config->host, $config->user, $config->pass, $config->dbname);
$sql = $link->prepare("SELECT * FROM profile WHERE fbid = ?");
$sql->bind_param("s", "1d028378e12ca6bdafa3b8b21bc5a9ea");
$sql->execute();
$result = $sql->get_result();
if ($result) {
print_r($result->fetch_array());
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment