Skip to content

Commit

Permalink
Merge pull request #50 from devaguia/changes_1_0_3
Browse files Browse the repository at this point in the history
Changes 1.0.3
  • Loading branch information
devaguia authored Aug 24, 2024
2 parents 177ab68 + 76bb25a commit 534669a
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ vendor/

# ignore files
*.lock
.idea/
.idea/
index.php
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.0.3] - 2024-08-23

### Fixed
- Fix the products dimention names;

### Added
- Set default lotId property value in Correios class;
- Create getLotId method in Correios class;
- Create getRequestNumber method in Correios class;
- Create getDr method in Authentication class;
- Create getContract method in Authentication class;

## [1.0.1] - 2023-11-22

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ $correios = new \Correios\Correios(

// Use um número de requisição e ID do lot personalizado - Use a custom request number and Lot ID
$correios->setRequestNumber(requestNumber: '20230831');
$correios->setLotId(requestNumber: '20230831LT');
$correios->getRequestNumber();

$correios->setLotId(lotId: '20230831LT');
$correios->getLotId();
```

### Rastro (Tracking)
Expand Down Expand Up @@ -136,6 +139,9 @@ $responseBody = $correios->authentication()->getResponseBody();
$responseCode = $correios->authentication()->getResponseCode();
$errors = $correios->authentication()->getErrors();

// Pega o número do e da diretoria com base na responsta da autenticação - Gets the board number based on the authentication response
$contractNumber = $correios->authentication()->getContract();
$drNumber = $correios->authentication()->getDr();

// Usando um token gerado anteriormente - Using a token generated earlie
$correios = new \Correios\Correios(
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "devaguia/correios-php",
"version": "1.0.2",
"version": "1.0.3",
"description": "Correios API library for PHP",
"license": "GPL-3.0",
"scripts": {
Expand Down
15 changes: 13 additions & 2 deletions src/Correios.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class Correios

public function __construct(string $username, string $password, string $postcard, bool $isTestMode = false, string $token = '')
{
$this->requestNumber = time();
$this->lotId = '';
$timestamp = time();
$this->lotId = "{$timestamp}LT";
$this->requestNumber = $timestamp;

$this->authenticate($username, $password, $postcard, $isTestMode, $token);
}
Expand Down Expand Up @@ -91,8 +92,18 @@ public function setRequestNumber(string $requestNumber): void
$this->requestNumber = $requestNumber;
}

public function getRequestNumber(): string
{
return $this->requestNumber;
}

public function setLotId(string $lotId): void
{
$this->lotId = $lotId;
}

public function getLotId(): string
{
return $this->lotId;
}
}
49 changes: 30 additions & 19 deletions src/Services/Authorization/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ class Authentication extends AbstractRequest
private string $username;
private string $password;
private string $dr;
private object $cartaoPostagem;
private string $contract;
private string $postcard;
private string $token;
private \DateTime $tokenExpiration;
public function __construct(string $username, string $password, string $contract, bool $isTestMode = false)
public function __construct(string $username, string $password, string $postcard, bool $isTestMode = false)
{
$this->username = $username;
$this->password = $password;
$this->contract = $contract;
$this->cartaoPostagem = (object) [];
$this->postcard = $postcard;

$this->setEnvironment($isTestMode ? 'sandbox' : 'production');
$this->setEndpoint('token/v1/autentica/cartaopostagem');
Expand All @@ -32,7 +31,7 @@ public function __construct(string $username, string $password, string $contract
private function buildBody(): void
{
$this->setBody([
'numero' => $this->contract
'numero' => $this->postcard
]);
}

Expand All @@ -48,35 +47,47 @@ public function generateToken(): void
{
try {
$this->sendRequest();
$data = $this->getResponseBody();

if (isset($data->token) && isset($data->expiraEm)) {
$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;
}
$data = $this->getResponseBody();
$this->setTokenProperties($data);
$this->setContract($data);
$this->setDr($data);

} catch (ApiRequestException $e) {
$this->errors[$e->getCode()] = $e->getMessage();
}
}

public function setCartaoPostagem(object $cartaoPostagem): void
private function setTokenProperties(object $data): void
{
$this->cartaoPostagem = $cartaoPostagem;
if (isset($data->token) && isset($data->expiraEm)) {
$this->token = $data->token;
$this->tokenExpiration = new \DateTime($data->expiraEm);
}
}

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

private function setDr(object $data): void
{
if (isset($data->cartaoPostagem->dr)) {
$this->dr = $data->cartaoPostagem->dr;
}
}

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

private function setContract(object $data): void
{
if (isset($data->cartaoPostagem->contrato)) {
$this->contract = $data->cartaoPostagem->contrato;
}
}

public function getToken(): string
Expand Down
10 changes: 5 additions & 5 deletions src/Services/Price/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ private function buildBody(array $serviceCodes, array $products, array $fields):
private function setOptionalParams(Product $product, array $productParam): array
{
if ($product->getWidth() > 0) {
$productParam['width'] = $product->getWidth();
$productParam['largura'] = $product->getWidth();
}

if ($product->getHeight() > 0) {
$productParam['height'] = $product->getHeight();
$productParam['altura'] = $product->getHeight();
}

if ($product->getLength() > 0) {
$productParam['length'] = $product->getLength();
$productParam['comprimento'] = $product->getLength();
}

if ($product->getDiameter() > 0) {
$productParam['diameter'] = $product->getDiameter();
$productParam['diametro'] = $product->getDiameter();
}

if ($product->getCubicWeight() > 0) {
$productParam['cubicWeight'] = $product->getCubicWeight();
$productParam['psCubico'] = $product->getCubicWeight();
}

if ($product->getObjectType() > 1 && $product->getObjectType() <= 3) {
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/CorreiosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
})->with('correios');
});


describe('getErrors() method', function() {
test('It should be possible to access the getErrors() method', function(Correios $correios){
expect($correios->getErrors())
Expand All @@ -107,4 +108,34 @@
expect($correios->getErrors())
->toBeArray();
})->with('correios');
});

describe('getLotId() method', function() {
test('It should be possible to access the getLotId() method', function(Correios $correios){
expect($correios->getLotId())
->not->toBeNull();
})->with('correios');

test('The getLotId() method must return the same value that was entered in the setLotId() method', function(Correios $correios){
$timestamp = time();
$correios->setLotId($timestamp);

expect($correios->getLotId())
->toBe((string) $timestamp);
})->with('correios');
});

describe('getRequestNumber() method', function() {
test('It should be possible to access the getRequestNumber() method', function(Correios $correios){
expect($correios->getRequestNumber())
->not->toBeNull();
})->with('correios');

test('The getRequestNumber() method must return the same value that was entered in the setRequestNumber() method', function(Correios $correios){
$timestamp = time();
$correios->setRequestNumber($timestamp);

expect($correios->getRequestNumber())
->toBe((string) $timestamp);
})->with('correios');
});
18 changes: 17 additions & 1 deletion tests/Unit/Includes/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
$length = fake()->randomFloat(1, 1, 100);
$diameter = fake()->randomFloat(1, 1, 100);
$cubicWeight = fake()->randomFloat(1, 1, 100);
$objectType = fake()->randomNumber(1);

dataset('weight', [$weight]);
dataset('width', [$width]);
dataset('height', [$height]);
dataset('length', [$length]);
dataset('diameter', [$diameter]);
dataset('cubicWeight', [$cubicWeight]);
dataset('product', [new Product($weight, $width, $height, $length, $diameter, $cubicWeight)]);
dataset('objectType', [$objectType]);
dataset('product', [new Product($weight, $width, $height, $length, $diameter, $cubicWeight, $objectType)]);


describe('weight property', function() {
Expand Down Expand Up @@ -109,3 +111,17 @@
})->with('product', 'cubicWeight');
});

describe('object type property', function() {
test('It should be possible to access the objectType property using the getObjectType() method', function(Product $product){
expect($product->getObjectType())
->not->toBeNull()
->toBeInt();
})->with('product');

test('The getObjectType() method must return the same value insert on the constructor method', function(Product $product, int $objectType){
expect($product->getObjectType())
->not->toBeNull()
->toBeInt()
->toBe($objectType);
})->with('product', 'objectType');
});
16 changes: 7 additions & 9 deletions tests/Unit/Services/Authorization/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
test('It should be possible to use the setToken() method', function(string $username, string $password, string $contract, string $token) {
$authentication = new Authentication($username, $password, $contract, true);

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

})->with('username', 'password', 'contract', 'token');
Expand Down Expand Up @@ -140,16 +140,15 @@
$authentication = new Authentication($username, $password, $contract, true);
$authentication->getToken();

expect(fn() =>
$authentication->getDr()
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();
Expand All @@ -160,10 +159,9 @@
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()
expect(
fn() => $authentication->getContract()
)->not->toThrow(Exception::class);

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

0 comments on commit 534669a

Please sign in to comment.