Laravel:如何模拟依赖注入类方法

hin*_*e02 5 php mocking github-api laravel mockery

我正在通过Laravel API Wrapper使用GitHub API。我创建了一个依赖注入类。如何在类中模拟该方法?existsApp\Http\GitHub.php

App\Http\GitHub.php

use GrahamCampbell\GitHub\GitHubManager;

class Github
{
    public $username;

    public $repository;

    public function __construct($username, $repository, GitHubManager $github)
    {
        $this->username = $username;

        $this->repository = $repository;

        $this->github = $github;
    }

    public static function make($username, $repository)
    {
        return new static($username, $repository, app(GitHubManager::class));
    }

    /**
     * Checks that a given path exists in a repository.
     *
     * @param  string  $path
     * @return bool
     */
    public function exists($path)
    {
        return $this->github->repository()->contents()->exists($this->username, $this->repository, $path);
    }
}
Run Code Online (Sandbox Code Playgroud)

测试:

    use App\Http\GitHub;
    public function test_it_can_check_if_github_file_exists()
    {
        $m = Mockery::mock(GitHub::class);
        $m->shouldReceive('exists')->andReturn(true);
        app()->instance(GitHub::class, $m);

        $github = GitHub::make('foo', 'bar');

        $this->assertTrue($github->exists('composer.lock'));
    }
Run Code Online (Sandbox Code Playgroud)

运行此测试实际上会命中 API,而不仅仅是返回模拟true值,我在这里做错了什么?

mrh*_*rhn 2

这里存在树问题,即实例化对象的方式。您在模拟对象上调用两个方法并将其绑定到错误的实例的方式。

依赖注入

静态方法通常是一种反模式,构造函数参数与容器的工作方式不兼容,因此您将无法使用resolve(Github::class);. 通常Laravel类通过使用 setter 来解决这个问题。

class Github
{
    public $username;

    public $repository;

    public $github;

    public function __construct(GitHubManager $github)
    {
        $this->github = $github;
    }

    public function setUsername(string $username) {
        $this->username = $username;

        return $this;
    }

    public function setRepository(string $repository) {
        $this->repository = $repository;

        return $this;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以使用以下方法调用您的代码。

resolve(Github::class)->setUsername('Martin')->setRepository('my-repo')->exists();
Run Code Online (Sandbox Code Playgroud)

方法的链接

这里有两个对模拟对象的调用,它们是链接的,因此您应该创建一个与此类似的模拟链。现在,模拟对象不知道内容,因此会失败。

$m = Mockery::mock(GitHub::class);

$m->shouldReceive('contents')
    ->andReturn($m);

$m->shouldReceive('exists')
    ->with('Martin', 'my-repo', 'your-path')
    ->once()
    ->andReturn(true);
Run Code Online (Sandbox Code Playgroud)

绑定实例

使用容器时,它将根据类自动加载它,因此GithubManager如果使用app(),resolve()或在构造函数中解析,以下代码将依赖注入。

public function __construct(GithubManager $manager)
Run Code Online (Sandbox Code Playgroud)

此代码将在上面的解析示例中注入 GithubManager,但在您的示例中,您将其绑定到 GitHub 类,该类不会自动加载,并且您应该始终模拟链中最远的类。因此你的实例绑定应该是。

app()->instance(GitHubManager::class, $m);
Run Code Online (Sandbox Code Playgroud)