Skip to content

Commit

Permalink
Authentication namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
mrprompt committed Dec 9, 2020
1 parent 8c4dfab commit 6ad9c85
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 2 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"description": "SDK de Integra\u0000\u0000o com a Im\u0000vel Web",
"type": "library",
"require": {
"ext-json": "*",
"guzzlehttp/guzzle": "^7.2"
},
"require-dev": {
"fakerphp/faker": "^1.12",
"phpunit/phpunit": "^9.5"
},
"license": "MIT",
Expand Down
54 changes: 52 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions src/Application/Authentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace MrPrompt\ImovelWeb\Application;

use GuzzleHttp\ClientInterface;

class Authentication
{
public function __construct(ClientInterface $client)
{
$this->client = $client;
}

public function login(string $clientId, string $clientSecret, string $grantType = 'client_credentials')
{
$response = $this->client->request(
'post',
'application/login',
[
'form_params' => [
'grant_type' => $grantType,
'client_id' => $clientId,
'client_secret' => $clientSecret,
]
]
);

return json_decode($response->getBody(), true);
}

public function logout(string $clientId, string $clientSecret, string $token)
{
$response = $this->client->request(
'post',
'application/login',
[
'form_params' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'token' => $token,
]
]
);

return json_decode($response->getBody(), true);
}
}
62 changes: 62 additions & 0 deletions tests/Application/AuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace MrPrompt\ImovelWeb\Tests\Application;

use GuzzleHttp\Psr7\Response;
use MrPrompt\ImovelWeb\Tests\Base\Base;
use MrPrompt\ImovelWeb\Application\Authentication;

class AuthenticationTest extends Base
{
/**
* @test
*/
public function loginWithValidCredentials()
{
$handleResponse = '{
"access_token": "9afb6554-28ca-422b-85c4-f14d6381d6e5",
"token_type": "bearer",
"expires_in": 315350721,
"scope": "read write trust"
}';

$handlerStack = [
new Response(200, [], $handleResponse),
];

$this->client = $this->getClient($handlerStack);
$this->service = new Authentication($this->client);

$result = $this->service->login(
$this->faker->shuffleString(),
$this->faker->randomNumber(6)
);

$this->assertArrayHasKey('access_token', $result);
$this->assertArrayHasKey('token_type', $result);
$this->assertArrayHasKey('expires_in', $result);
$this->assertArrayHasKey('scope', $result);
}

/**
* @test
*/
public function logoutWithValidCredentials()
{
$handleResponse = '';

$handlerStack = [
new Response(204, [], $handleResponse),
];

$this->client = $this->getClient($handlerStack);
$this->service = new Authentication($this->client);

$result = $this->service->logout(
$this->faker->shuffleString(),
$this->faker->randomNumber(6),
$this->faker->uuid,
);

$this->assertEmpty($result);
}
}
8 changes: 8 additions & 0 deletions tests/Base/Base.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<?php
namespace MrPrompt\ImovelWeb\Tests\Base;

use Faker\Generator;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\ClientInterface;
use MrPrompt\ImovelWeb\Tests\Traits\FakeHttpClient;

abstract class Base extends TestCase
{
use FakeHttpClient;

protected ClientInterface $client;
protected Generator $faker;
protected object $service;

protected string $token;
protected string $environment;

Expand All @@ -16,5 +23,6 @@ public function setUp(): void

$this->token = uniqid();
$this->environment = 'local';
$this->faker = \Faker\Factory::create();
}
}
20 changes: 20 additions & 0 deletions tests/Traits/FakeHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace MrPrompt\ImovelWeb\Tests\Traits;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;

trait FakeHttpClient
{
public function getClient(array $handlerStack = [])
{
$mock = new MockHandler($handlerStack);

$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$client->baseUrl = '';

return $client;
}
}

0 comments on commit 6ad9c85

Please sign in to comment.