diff --git a/.gitignore b/.gitignore index bbd24b7c..33efb90b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ vendor/ # ignore files *.lock -.idea/ \ No newline at end of file +.idea/ +index.php \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e7d0908..235e2a7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index abd18fce..a59fccc2 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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( diff --git a/composer.json b/composer.json index d4cdb867..62426112 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Correios.php b/src/Correios.php index 22c67bab..68e7b066 100644 --- a/src/Correios.php +++ b/src/Correios.php @@ -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); } @@ -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; + } } diff --git a/src/Services/Authorization/Authentication.php b/src/Services/Authorization/Authentication.php index 0d7a19b7..9c57a672 100644 --- a/src/Services/Authorization/Authentication.php +++ b/src/Services/Authorization/Authentication.php @@ -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'); @@ -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 ]); } @@ -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 diff --git a/src/Services/Price/Price.php b/src/Services/Price/Price.php index 0081b5aa..f72d12e0 100644 --- a/src/Services/Price/Price.php +++ b/src/Services/Price/Price.php @@ -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) { diff --git a/tests/Unit/CorreiosTest.php b/tests/Unit/CorreiosTest.php index 2c2f77ae..c9de6ba7 100644 --- a/tests/Unit/CorreiosTest.php +++ b/tests/Unit/CorreiosTest.php @@ -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()) @@ -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'); }); \ No newline at end of file diff --git a/tests/Unit/Includes/ProductTest.php b/tests/Unit/Includes/ProductTest.php index ee6db41d..503814e9 100644 --- a/tests/Unit/Includes/ProductTest.php +++ b/tests/Unit/Includes/ProductTest.php @@ -9,6 +9,7 @@ $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]); @@ -16,7 +17,8 @@ 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() { @@ -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'); +}); \ No newline at end of file diff --git a/tests/Unit/Services/Authorization/AuthenticationTest.php b/tests/Unit/Services/Authorization/AuthenticationTest.php index 9196f4a4..054a7331 100644 --- a/tests/Unit/Services/Authorization/AuthenticationTest.php +++ b/tests/Unit/Services/Authorization/AuthenticationTest.php @@ -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'); @@ -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(); @@ -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');