Skip to content

Commit

Permalink
Merge pull request #45 from rafaelstz/patch-1
Browse files Browse the repository at this point in the history
Update Authentication.php with DR and Contract
  • Loading branch information
devaguia authored Aug 21, 2024
2 parents e10ee4f + de31713 commit 177ab68
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Services/Authorization/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Authentication extends AbstractRequest
{
private string $username;
private string $password;
private string $dr;
private object $cartaoPostagem;
private string $contract;
private string $token;
private \DateTime $tokenExpiration;
Expand All @@ -17,6 +19,7 @@ public function __construct(string $username, string $password, string $contract
$this->username = $username;
$this->password = $password;
$this->contract = $contract;
$this->cartaoPostagem = (object) [];

$this->setEnvironment($isTestMode ? 'sandbox' : 'production');
$this->setEndpoint('token/v1/autentica/cartaopostagem');
Expand Down Expand Up @@ -51,11 +54,31 @@ public function generateToken(): void
$this->token = $data->token;
$this->tokenExpiration = new \DateTime($data->expiraEm);
}

// Set DR and Contract
if (isset($data->cartaoPostagem->dr) && isset($data->cartaoPostagem->contrato)) {
$this->cartaoPostagem->dr = $data->cartaoPostagem->dr;
$this->cartaoPostagem->contract = $data->cartaoPostagem->contrato;
}
} catch (ApiRequestException $e) {
$this->errors[$e->getCode()] = $e->getMessage();
}
}

public function setCartaoPostagem(object $cartaoPostagem): void
{
$this->cartaoPostagem = $cartaoPostagem;
}

public function getDr(): string
{
return $this->cartaoPostagem->dr ?? '';
}

public function getContract(): string
{
return $this->cartaoPostagem->contract ?? '';
}

public function getToken(): string
{
return $this->token ?? '';
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Services/Authorization/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
$contract = fake()->regexify('[0-9]{10}');
$password = fake()->password();
$token = fake()->regexify('[0-9]{10}[A-Z]{5}[a-z]{5}');
$dr = fake()->regexify('[0-9]{10}');

dataset('username', [$username]);
dataset('password', [$password]);
dataset('contract', [$contract]);
dataset('token', [$token]);
dataset('dr', [$dr]);

test('It should be possible instance the authentication class without generate any errors in the array list', function(string $username, string $password, string $contract) {
$authentication = new Authentication($username, $password, $contract, true);
Expand Down Expand Up @@ -129,5 +131,50 @@
->not->toBeNull()
->toBeInt();

})->with('username', 'password', 'contract');
});


describe('getDr() method', function() {
test('It should be possible to use the getDr() method', function(string $username, string $password, string $contract) {
$authentication = new Authentication($username, $password, $contract, true);
$authentication->getToken();

expect(fn() =>
$authentication->getDr()
)->not->toThrow(Exception::class);

})->with('username', 'password', 'contract');

test('The getDr() method must return a string', function(string $username, string $password, string $contract) {
$authentication = new Authentication($username, $password, $contract, true);
$authentication->getToken();

expect($authentication->getDr())
->not->toBeNull()
->toBeString();

})->with('username', 'password', 'contract');
});

describe('getContract() method', function() {
test('It should be possible to use the getContract() method', function(string $username, string $password, string $contract) {
$authentication = new Authentication($username, $password, $contract, true);
$authentication->getToken();

expect(fn() =>
$authentication->getToken()
)->not->toThrow(Exception::class);

})->with('username', 'password', 'contract');

test('The getContract() method must return a string', function(string $username, string $password, string $contract) {
$authentication = new Authentication($username, $password, $contract, true);
$authentication->getToken();

expect($authentication->getContract())
->not->toBeNull()
->toBeString();

})->with('username', 'password', 'contract');
});

0 comments on commit 177ab68

Please sign in to comment.