Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaconnier committed Nov 4, 2021
1 parent 6518da7 commit d4755c1
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 102 deletions.
172 changes: 172 additions & 0 deletions tests/Clients/ZohoHttpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

namespace MelbaCh\LaravelZoho\Tests\Clients;

use Illuminate\Http\Client\Request;
use Http;
use Illuminate\Http\Client\Response;
use MelbaCh\LaravelZoho\Auth\ZohoAccessToken;
use MelbaCh\LaravelZoho\Auth\ZohoAuthProvider;
use MelbaCh\LaravelZoho\Clients\ZohoHttp;
use MelbaCh\LaravelZoho\Repositories\AccessTokenRepository;
use MelbaCh\LaravelZoho\Tests\TestCase;
use MelbaCh\LaravelZoho\ZohoPendingRequest;
use MelbaCh\LaravelZoho\ZohoResponse;
use Mockery;
use Mockery\MockInterface;

class ZohoHttpTest extends TestCase
{
protected ZohoHttp $zohoClientHttp;

protected function setUp(): void
{
parent::setUp();

$accessToken = new ZohoAccessToken(['access_token' => 'mock_access_token', 'expires' => now()->addHour()->timestamp]);

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

$this->mock(ZohoAuthProvider::class, static function (MockInterface $repository) use ($accessToken)
{
$repository->shouldReceive('getAccessToken')->andReturn($accessToken);
});

$this->zohoClientHttp = new ZohoHttp;
}

protected function tearDown(): void
{
\Mockery::close();
}

/** @test */
public function it_can_make_a_request(): void
{
$fakeResponse = [
"users" => [
[
"country" => "US",
"street" => null,
"id" => "4150868000000225013",
"first_name" => "Patricia",
"last_name" => "Boyle",
],
],
"info" => [
"per_page" => 200,
"count" => 3,
"page" => 1,
"more_records" => false,
],
];

$this->zohoClientHttp->fake([
'users' => Http::response($fakeResponse),
'*' => Http::response(['error' => 'error']),
]);

$this->assertEquals(
$fakeResponse,
$this->zohoClientHttp
->get('users')->json()

);
}


/** @test */
public function it_returns_a_zoho_response_class(): void
{
$this->zohoClientHttp->fake();

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

/** @var Request $request */
$request = $this->zohoClientHttp->recorded()[0][0];
$this->assertTrue($request->hasHeader('Authorization'));
$this->assertEquals('Zoho-oauthtoken mock_access_token', $request->headers()['Authorization'][0]);
}

/** @test */
public function it_returns_a_zoho_pending_request(): void
{
$this->zohoClientHttp->fake();

$pendingRequest = $this->zohoClientHttp->asForm();

$reflection = new \ReflectionClass($pendingRequest);
$reflectionProperty = $reflection->getProperty('options');
$reflectionProperty->setAccessible(true);

$options = $reflectionProperty->getValue($pendingRequest);

$this->assertInstanceOf(ZohoPendingRequest::class, $pendingRequest);
$this->assertEquals('Zoho-oauthtoken mock_access_token', $options['headers']['Authorization']);
}

/** @test */
public function it_uses_the_zoho_bearer_header_when_performing_a_request(): void
{
$this->zohoClientHttp->fake();

$this->assertInstanceOf(ZohoResponse::class, $this->zohoClientHttp->get('url'));
}

/** @test */
public function it_call_refresh_token_when_performing_a_request(): void
{
$accessToken = $this->mock(ZohoAccessToken::class, static function (MockInterface $mock): void
{
$mock->shouldReceive('hasExpired')->once()->andReturn(true);
$mock->shouldReceive('getRefreshToken')->once()->andReturnSelf();
$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_only_once_refresh_token_when_performing_multiple_operations(): void
{
$accessToken = $this->mock(ZohoAccessToken::class, static function (MockInterface $mock): void
{
$mock->shouldReceive('hasExpired')->once()->andReturn(true);
$mock->shouldReceive('getRefreshToken')->once()->andReturnSelf();
$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
->asForm()
->attach('file')
->get('/');

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

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MelbaCh\LaravelZoho\Tests\Factories;
namespace MelbaCh\LaravelZoho\Tests\Clients;

use MelbaCh\LaravelZoho\Clients\ZohoUrl;
use MelbaCh\LaravelZoho\Repositories\DefaultConfigRepository;
Expand All @@ -9,7 +9,7 @@
use Mockery\MockInterface;


class UrlFactoryTest extends TestCase
class ZohoUrlTest extends TestCase
{
protected function setUp(): void
{
Expand Down
100 changes: 0 additions & 100 deletions tests/Factories/ZohoHttpTest.php

This file was deleted.

0 comments on commit d4755c1

Please sign in to comment.