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

标签 php git phpunit shell-exec git-diff

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

这是 Migrate 类和 gitDiff 方法:

#!/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 .

关于php - 如何在 phpunit 中模拟 git diff 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27909670/

相关文章:

git 快进一次提交

php - 如何正确测试使用其他服务的服务方法

php - xdebug 扩展未加载

php - 测试基本授权

PHP Web 套接字无法在 SSL 中连接

php - Wordpress 查询仅显示置顶帖子

Git 将一个分支中的多个提交 rebase 到 master

php - Sonata FormMapper 从整数字段添加链接

php - 填充表中的下拉菜单,然后从另一个表中设置所选选项

git - Visual Studio 2015 Update 1 破坏了 git 存储库界面