如何在 phpunit 中模拟 git diff 的结果

How can I mock the results of git diff in phpunit

我正在 PHP 中编写数据库迁移脚本,我需要在 phpunit 中模拟 git diff 的结果。 这个想法是 git diff 只会 return 自上次提交以来在 includes/ 中添加或更新的文件的名称。但当然,随着我处理脚本并提交我的更改,这会不断变化。

这是迁移 class 和 git 差异方法:

#!/usr/bin/php
<?php

class Migrate {

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

有什么想法吗?

在 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());

您可以使用 ouzo goodies 模拟工具:

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

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

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

所有文档here