How can I mock git diff results in phpunit

I am writing a database migration script in PHP and I need to mock git diff results in phpunit. The idea is that git diff will only return the filenames that were added or updated in include / from the previous commit. But of course this will keep changing as I work on the script and commit my changes.

Below is the Migrate and gitDiff method:

#!/usr/bin/php
<?php

class Migrate {

    public function gitDiff(){
        return shell_exec('git diff HEAD^ HEAD --name-only includes/');
    }
}
?>

      

Any ideas?

+3


source to share


1 answer


In PHPUnit:

$mock = $this->getMockBuilder('Migrate')
                     ->setMethods(array('getDiff'))
                     ->getMock();

$mock->expects($this->any())
        ->method('getDiff')
        ->will($this->returnValue('your return'));

$this->assertEquals("your return", $mock->getDiff());

      

You can use the ouzo goodies tool :



$mock = Mock::create('Migrate');

Mock::when($mock)->getDiff()->thenReturn('your return');

$this->assertEquals("your return", $mock->getDiff());

      

All documents are here .

+6


source







All Articles