I'm using the GitHub API through a Laravel API Wrapper. I've created a dependency injection class. How can I mock the exists
method within the App\Http\GitHub.php
class?
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);
}
}
Test:
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'));
}
Running this test actually hits the API rather than just returning the mocked true
value, what am I doing wrong here?