3

I'm making a simple laravel package to wrap bash executions and make it be able to mock as laravel facade, but I have a problem.

Most of Php functions that runs bash commands uses pass-by-reference parameters, and the question is, how can I mock the return value of the pass-by-reference of the method?

MyFacade:

class MyFacade
{
    public function run(string $command, &$return_val): string
    {
        return system($command, $return_val);
    }
}

My test:

$passByReferenceValue = 1;

MyFacade::shouldReceive('run')->with('ping google.com', $passByReferenceValue)
->once()->andReturn("PING RESULT");

$return = MyFacade::run('ping google.com', $passByReferenceValue);

$this->assertEquals(1, $passByReferenceValue);
$this->assertEquals("PING RESULT", $return);

It actually works because I set the value of $passByReferenceValue and it's not changed. What I want is to pass a null pointer as second parameter of run method and make the mock to change it.

Example:

MyFacade::shouldReceive('run')->with('ping google.com', SOME_MAGIC_CODE_RETURNS_1)
->once()->andReturn("PING RESULT");

$resultCode = null;
$return = MyFacade::run('ping www.google.com', $resultCode)

$this->assertEquals(1, $resultCode);
$this->assertEquals("PING RESULT", $return);

Thanks

axsor
  • 470
  • 1
  • 6
  • 16

0 Answers0