Skip to content

Commit

Permalink
Add ability to fake the Facade
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaconnier committed Nov 15, 2021
1 parent d4755c1 commit c70ce12
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Clients/ZohoHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class ZohoHttp extends Factory
{
protected bool $isFaking = false;

/**
* @inheritDoc
*/
Expand All @@ -21,8 +23,9 @@ public function __call($method, $parameters)
return $this->macroCall($method, $parameters);
}

// todo: prevent refresh when using ::fake()
$this->refreshAccessToken();
if ($this->isFaking === false) {
$this->refreshAccessToken();
}

$response = tap($this->newPendingRequest(), function (PendingRequest $request)
{
Expand All @@ -41,6 +44,12 @@ public function __call($method, $parameters)
return $response;
}

public function fake($callback = null)
{
$this->isFaking = true;
return parent::fake($callback);
}

public function headers(): array
{
$headers = [];
Expand Down
34 changes: 34 additions & 0 deletions tests/Clients/ZohoHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ public function it_uses_the_zoho_bearer_header_when_performing_a_request(): void
$this->assertInstanceOf(ZohoResponse::class, $this->zohoClientHttp->get('url'));
}

/** @test */
public function it_prevent_refresh_token_call_when_using_fake(): void
{
$accessToken = $this->mock(ZohoAccessToken::class, static function (MockInterface $mock): void
{
$mock->shouldReceive('hasExpired')->never();
$mock->shouldReceive('getRefreshToken')->never();
$mock->shouldReceive('getToken')->once();
});

$this->mock(AccessTokenRepository::class, static function (MockInterface $repository) use ($accessToken)
{
$repository->shouldReceive('exists')->andReturn(true);
$repository->shouldReceive('get')->andReturn($accessToken);
$repository->shouldReceive('store');
});

$this->zohoClientHttp->fake();

$this->zohoClientHttp->get('/');

$this->assertInstanceOf(MockInterface::class, $accessToken);
}

/** @test */
public function it_call_refresh_token_when_performing_a_request(): void
{
Expand All @@ -137,6 +161,11 @@ public function it_call_refresh_token_when_performing_a_request(): void

$this->zohoClientHttp->fake();

$reflection = new \ReflectionClass($this->zohoClientHttp);
$reflectionProperty = $reflection->getProperty('isFaking');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->zohoClientHttp, false);

$this->zohoClientHttp->get('/');

$this->assertInstanceOf(MockInterface::class, $accessToken);
Expand All @@ -161,6 +190,11 @@ public function it_call_only_once_refresh_token_when_performing_multiple_operati

$this->zohoClientHttp->fake();

$reflection = new \ReflectionClass($this->zohoClientHttp);
$reflectionProperty = $reflection->getProperty('isFaking');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->zohoClientHttp, false);

$this->zohoClientHttp
->asForm()
->attach('file')
Expand Down

0 comments on commit c70ce12

Please sign in to comment.