Last active
July 4, 2023 16:30
-
-
Save ajaxray/18a13136d246c261ccc68689d4e4b90c to your computer and use it in GitHub Desktop.
PHP mocking built-in functions (e,g exec, file_exists etc.) for testing
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 | |
/** | |
* Example of mocking built-in functions for testing | |
* | |
* @Author : ajaxray <anis.programmer@gmail.com> | |
* @Date-Time : 03/05/2017 | |
* | |
* How it works? | |
* ---------------- | |
* Simply by redefining the function for target Namespace. | |
* Note : we can mock if global functions are used as "file_exist()", but not as "\file_exist()" | |
*/ | |
namespace Namespace\Of\Test\Class; | |
use Namespace\Of\Class\Under\Test; | |
class SomeClassTest extends \PHPUnit_Framework_TestCase | |
{ | |
protected function setUp() | |
{ | |
global $mockGlobalFunctions; | |
$mockGlobalFunctions = true; | |
// setting flase to $mockGlobalFunctions will de-activate mocking | |
} | |
public function testSomethingThatRunsExec() | |
{ | |
global $lastExecCommand; | |
// Perform the test | |
$this->assertEquals('expected command -and -args', $lastExecCommand); | |
} | |
} | |
// =================================================================================== | |
// Mechanism for mocking some built in functions () | |
// This section is placed at the bottom of SomeClassTest file | |
// ================================================================================== | |
namespace Namespace\Of\Class\Under\Test; | |
$GLOBALS['mockGlobalFunctions'] = false; | |
$GLOBALS['lastExecCommand'] = null; | |
// Overwriting `file_exists` to always return true | |
function file_exists($path) | |
{ | |
global $mockGlobalFunctions; | |
if (isset($mockGlobalFunctions) && $mockGlobalFunctions === true) { | |
return true; | |
} else { | |
return call_user_func_array('\file_exists', func_get_args()); | |
} | |
} | |
// Overwriting `mime_content_type` to return a planned mime type | |
function mime_content_type($path) | |
{ | |
global $mockGlobalFunctions; | |
if (isset($mockGlobalFunctions) && $mockGlobalFunctions === true) { | |
if(preg_match('/(png)|(jpe?g)|(gif)/', $path, $match)) { | |
return 'image/'. $match[0]; | |
} elseif (preg_match('/(pdf)|(x\-pdf)/', $path, $match)) { | |
return 'application/'. $match[0]; | |
} | |
return 'no-pdf/no-image'; | |
} else { | |
return call_user_func_array('\mime_content_type', func_get_args()); | |
} | |
} | |
// Overwriting `exec` to set the command in a global variable instead of executing | |
function exec($command, $output, $returnCode) | |
{ | |
global $mockGlobalFunctions, $lastExecCommand; | |
if (isset($mockGlobalFunctions) && $mockGlobalFunctions === true) { | |
$lastExecCommand = func_get_arg(0); | |
} else { | |
return call_user_func_array('\exec', func_get_args()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment