-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |