I had the same problem a few months ago. This is what worked for me:
You could namespace your test with the namespace of the class under test. Then, before the test class, create the function in that namespace, like this:
namespace The\Same\Like\The\Class\Under\Test
use \Mockery;
function shell_exec($cmd)
{
return YourTest::$functions->shell_exec($cmd);
}
class YourTest extends \PHPUnit_Framework_TestCase {
public static $functions;
public function setUp()
{
self::$functions = Mockery::mock();
}
}
Now, when you're testing your class, and want to mock the function, you could write it like this:
self::$functions->shouldReceive('shell_exec')->with($cmd)->once();
Or in whatever way you want to mock it.
In phpspec:
This is exactly why I didn't use phpspec when I had this problem.
I think (but not sure) that with a phpspec approach in mind, you would wrap the shell command in a class, like this:
class Shell{
public function exec($cmd)
{
return shell_exec($cmd);
}
}
I guess, there's no need to test this class, since it's just a wrapper. Then, you would use this class instead of the function in your other classes, and when it comes to mocking, you could mock this class.
reference : https://laracasts.com/discuss/channels/testing/i-need-to-mock-a-php-function