From 6ad9c85622d83f07a48952b9e81d11f6b3c5bd4b Mon Sep 17 00:00:00 2001 From: Thiago Paes Date: Wed, 9 Dec 2020 19:50:57 -0300 Subject: [PATCH] Authentication namespace --- composer.json | 2 + composer.lock | 54 ++++++++++++++++++++- src/Application/Authentication.php | 46 ++++++++++++++++++ tests/Application/AuthenticationTest.php | 62 ++++++++++++++++++++++++ tests/Base/Base.php | 8 +++ tests/Traits/FakeHttpClient.php | 20 ++++++++ 6 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 src/Application/Authentication.php create mode 100644 tests/Application/AuthenticationTest.php create mode 100644 tests/Traits/FakeHttpClient.php diff --git a/composer.json b/composer.json index 39f1ba2..fb84939 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index e047e6f..aa68ec2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7caef2ff32f461386f43ae4dc2e64d7f", + "content-hash": "be46e1f832beb2065daf70bdc6a21fb1", "packages": [ { "name": "guzzlehttp/guzzle", @@ -432,6 +432,54 @@ ], "time": "2020-11-10T18:47:58+00:00" }, + { + "name": "fakerphp/faker", + "version": "v1.12.0", + "source": { + "type": "git", + "url": "https://github.com/FakerPHP/Faker.git", + "reference": "9aa6c9e289860951e6b4d010c7a841802d015cd8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/FakerPHP/Faker/zipball/9aa6c9e289860951e6b4d010c7a841802d015cd8", + "reference": "9aa6c9e289860951e6b4d010c7a841802d015cd8", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "conflict": { + "fzaninotto/faker": "*" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4.1", + "ext-intl": "*", + "phpunit/phpunit": "^7.5.20 || ^8.5.8 || ^9.4.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Faker\\": "src/Faker/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "François Zaninotto" + } + ], + "description": "Faker is a PHP library that generates fake data for you.", + "keywords": [ + "data", + "faker", + "fixtures" + ], + "time": "2020-11-23T09:33:08+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.10.2", @@ -2324,7 +2372,9 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "ext-json": "*" + }, "platform-dev": [], "plugin-api-version": "1.1.0" } diff --git a/src/Application/Authentication.php b/src/Application/Authentication.php new file mode 100644 index 0000000..43efd03 --- /dev/null +++ b/src/Application/Authentication.php @@ -0,0 +1,46 @@ +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); + } +} diff --git a/tests/Application/AuthenticationTest.php b/tests/Application/AuthenticationTest.php new file mode 100644 index 0000000..6a0125c --- /dev/null +++ b/tests/Application/AuthenticationTest.php @@ -0,0 +1,62 @@ +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); + } +} diff --git a/tests/Base/Base.php b/tests/Base/Base.php index aebeb24..b067863 100644 --- a/tests/Base/Base.php +++ b/tests/Base/Base.php @@ -1,12 +1,19 @@ token = uniqid(); $this->environment = 'local'; + $this->faker = \Faker\Factory::create(); } } diff --git a/tests/Traits/FakeHttpClient.php b/tests/Traits/FakeHttpClient.php new file mode 100644 index 0000000..e1e5ed8 --- /dev/null +++ b/tests/Traits/FakeHttpClient.php @@ -0,0 +1,20 @@ + $handler]); + $client->baseUrl = ''; + + return $client; + } +} \ No newline at end of file