Skip to content

Commit

Permalink
Substituição dos métodos estáticos e refatoração nos testes
Browse files Browse the repository at this point in the history
  • Loading branch information
claudsonm committed Sep 14, 2019
2 parents 12c14d9 + 2d3e5d9 commit 2d5213e
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 111 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
}
],
"require": {
"php": "^7.0",
"guzzlehttp/guzzle": "~6.0",
"ext-simplexml": "*",
"ext-json": "*"
Expand Down
7 changes: 6 additions & 1 deletion src/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Address
*
* @param array $data
*
* @return \Claudsonm\CepPromise\Address
* @return Address
*/
public static function create(array $data = [])
{
Expand All @@ -61,6 +61,11 @@ public static function create(array $data = [])
return $address;
}

/**
* Converte a instância da classe em um array associativo.
*
* @return array
*/
public function toArray()
{
return (array) $this;
Expand Down
140 changes: 75 additions & 65 deletions src/CepPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace Claudsonm\CepPromise;

use Claudsonm\CepPromise\Exceptions\CepPromiseException;
use Claudsonm\CepPromise\Providers\CepAbertoProvider;
use Claudsonm\CepPromise\Providers\CorreiosProvider;
use Claudsonm\CepPromise\Providers\ViaCepProvider;
use Exception;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\FulfilledPromise;
use Claudsonm\CepPromise\Providers\ViaCepProvider;
use Claudsonm\CepPromise\Providers\CorreiosProvider;
use Claudsonm\CepPromise\Providers\CepAbertoProvider;
use Claudsonm\CepPromise\Exceptions\CepPromiseException;

/**
* Classe responsável por receber o CEP e disparar as requisições aos providers.
* Efetua a consulta pelas informações de um CEP em diferentes serviços de
* forma concorrente, retornando a resposta mais rápida..
*
* @author Claudson Martins <[email protected]>
*/
Expand All @@ -24,15 +25,24 @@ class CepPromise
const ERROR_VALIDATION_CODE = 1;

/**
* Normaliza o CEP dado e efetua as requisições.
*
* @param $cepRawValue
* Busca as informações referente ao CEP informado.
*
* @throws \Claudsonm\CepPromise\Exceptions\CepPromiseException
* @param string|int $cep
* @return Address
* @throws CepPromiseException
*/
public static function fetch($cep)
{
return (new self())->run($cep);
}

/**
* Dispara a cadeia de execução para obtenção das informações do CEP dado.
*
* @return \Claudsonm\CepPromise\Address
* @param string|int $cepRawValue
* @return Address
*/
public static function fetch($cepRawValue)
public function run($cepRawValue): Address
{
$promise = new FulfilledPromise($cepRawValue);
$cepData = $promise
Expand All @@ -48,60 +58,34 @@ public static function fetch($cepRawValue)
return Address::create($cepData);
}

private static function fetchCepFromProviders()
{
return function (string $cepWithLeftPad) {
$promises = array_merge(
ViaCepProvider::createPromiseArray($cepWithLeftPad),
CepAbertoProvider::createPromiseArray($cepWithLeftPad),
CorreiosProvider::createPromiseArray($cepWithLeftPad)
);

return Promise\any($promises);
};
}

private static function handleProvidersError()
private function validateInputType()
{
return function (Exception $onRejected) {
if ($onRejected instanceof Promise\AggregateException) {
throw new CepPromiseException(
'Todos os serviços de CEP retornaram erro.',
self::ERROR_PROVIDER_CODE,
$onRejected->getReason()
);
return function ($cepRawValue) {
if (is_string($cepRawValue) || is_int($cepRawValue)) {
return $cepRawValue;
}

throw $onRejected;
};
}

private static function leftPadWithZeros()
{
return function (string $cepCleanValue) {
return str_pad($cepCleanValue, self::CEP_SIZE, '0', STR_PAD_LEFT);
throw new CepPromiseException(
'Erro ao inicializar a instância do CepPromise.',
self::ERROR_VALIDATION_CODE,
[
[
'message' => 'Você deve informar o CEP utilizando uma string ou um inteiro.',
'service' => 'cep_validation',
],
]
);
};
}

private static function removeSpecialCharacters()
private function removeSpecialCharacters()
{
return function (string $cepRawValue) {
return preg_replace('/\D+/', '', $cepRawValue);
};
}

private static function throwApplicationError()
{
return function (Exception $exception) {
throw new CepPromiseException(
$exception->getMessage(),
$exception->getCode(),
$exception->getErrors() ?? []
);
};
}

private static function validateInputLength()
private function validateInputLength()
{
return function (string $cepNumbers) {
if (strlen($cepNumbers) <= self::CEP_SIZE) {
Expand All @@ -121,22 +105,48 @@ private static function validateInputLength()
};
}

private static function validateInputType()
private function leftPadWithZeros()
{
return function ($cepRawValue) {
if (is_string($cepRawValue) || is_int($cepRawValue)) {
return $cepRawValue;
return function (string $cepSanitized) {
return str_pad($cepSanitized, self::CEP_SIZE, '0', STR_PAD_LEFT);
};
}

private function fetchCepFromProviders()
{
return function (string $cepWithLeftPad) {
$promises = array_merge(
ViaCepProvider::createPromiseArray($cepWithLeftPad),
CepAbertoProvider::createPromiseArray($cepWithLeftPad),
CorreiosProvider::createPromiseArray($cepWithLeftPad)
);

return Promise\any($promises);
};
}

private function handleProvidersError()
{
return function (Exception $onRejected) {
if ($onRejected instanceof Promise\AggregateException) {
throw new CepPromiseException(
'Todos os serviços de CEP retornaram erro.',
self::ERROR_PROVIDER_CODE,
$onRejected->getReason()
);
}

throw $onRejected;
};
}

private function throwApplicationError()
{
return function (Exception $exception) {
throw new CepPromiseException(
'Erro ao inicializar a instância do CepPromise.',
self::ERROR_VALIDATION_CODE,
[
[
'message' => 'Você deve informar o CEP utilizando uma string ou um inteiro.',
'service' => 'cep_validation',
],
]
$exception->getMessage(),
$exception->getCode(),
$exception->getErrors() ?? []
);
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/Contracts/BaseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Claudsonm\CepPromise\Contracts;

use GuzzleHttp\Client;
use GuzzleHttp\Promise\Promise;

abstract class BaseProvider implements ProviderInterface
{
Expand All @@ -16,14 +17,14 @@ abstract class BaseProvider implements ProviderInterface
/**
* O cliente HTTP utilizado para realizar os requests.
*
* @var \GuzzleHttp\Client
* @var Client
*/
protected $client;

/**
* A instância da Promise para consulta no serviço de busca.
*
* @var \GuzzleHttp\Promise\Promise
* @var Promise
*/
protected $promise;

Expand Down
4 changes: 3 additions & 1 deletion src/Contracts/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Claudsonm\CepPromise\Contracts;

use GuzzleHttp\Promise\Promise;

interface ProviderInterface
{
/**
* Cria a Promise para obter os dados de um CEP no provedor do serviço.
*
* @param string $cep
*
* @return \GuzzleHttp\Promise\Promise
* @return Promise
*/
public function makePromise(string $cep);
}
6 changes: 3 additions & 3 deletions src/Providers/CepAbertoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Claudsonm\CepPromise\Providers;

use Claudsonm\CepPromise\Contracts\BaseProvider;
use Claudsonm\CepPromise\Exceptions\CepPromiseProviderException;
use Exception;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use Claudsonm\CepPromise\Contracts\BaseProvider;
use Claudsonm\CepPromise\Exceptions\CepPromiseProviderException;

class CepAbertoProvider extends BaseProvider
{
Expand Down
6 changes: 3 additions & 3 deletions src/Providers/CorreiosProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Claudsonm\CepPromise\Providers;

use Claudsonm\CepPromise\Contracts\BaseProvider;
use Claudsonm\CepPromise\Exceptions\CepPromiseProviderException;
use Exception;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use Claudsonm\CepPromise\Contracts\BaseProvider;
use Claudsonm\CepPromise\Exceptions\CepPromiseProviderException;

class CorreiosProvider extends BaseProvider
{
Expand Down
6 changes: 3 additions & 3 deletions src/Providers/ViaCepProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Claudsonm\CepPromise\Providers;

use Claudsonm\CepPromise\Contracts\BaseProvider;
use Claudsonm\CepPromise\Exceptions\CepPromiseProviderException;
use Exception;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use Claudsonm\CepPromise\Contracts\BaseProvider;
use Claudsonm\CepPromise\Exceptions\CepPromiseProviderException;

class ViaCepProvider extends BaseProvider
{
Expand Down
40 changes: 9 additions & 31 deletions tests/Feature/CepPromiseFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,19 @@ public function testExceptionFetchingNonExistentCep()

public function testFetchingUsingValidIntegerWithoutLeadingZeros()
{
$addressRetrieved = CepPromise::fetch(5010000);
$this->assertInstanceOf(Address::class, $addressRetrieved);

return $addressRetrieved;
}

public function testFetchingUsingValidStringWithLeadingZeros()
{
$addressRetrieved = CepPromise::fetch('05010000');
$this->assertInstanceOf(Address::class, $addressRetrieved);

return $addressRetrieved;
}

/**
* @depends testFetchingUsingValidIntegerWithoutLeadingZeros
*
* @param \Claudsonm\CepPromise\Address $address
*/
public function testInformationRetrievedFromIntegerWithoutLeadingZeros(Address $address)
{
$this->assertEquals('São Paulo', $address->city);
$this->assertEquals('Perdizes', $address->district);
$address = CepPromise::fetch(8542130);
$this->assertInstanceOf(Address::class, $address);
$this->assertEquals('Ferraz de Vasconcelos', $address->city);
$this->assertEquals('Cidade Kemel', $address->district);
$this->assertEquals('SP', $address->state);
$this->assertEquals('Rua Caiubi', $address->street);
$this->assertEquals('05010000', $address->zipCode);
$this->assertEquals('Avenida Luiz Rosa da Costa', $address->street);
$this->assertEquals('08542130', $address->zipCode);
}

/**
* @depends testFetchingUsingValidStringWithLeadingZeros
*
* @param \Claudsonm\CepPromise\Address $address
*/
public function testInformationRetrievedFromStringWithLeadingZeros(Address $address)
public function testFetchingUsingValidStringWithLeadingZeros()
{
$address = CepPromise::fetch('05010000');
$this->assertInstanceOf(Address::class, $address);
$this->assertEquals('São Paulo', $address->city);
$this->assertEquals('Perdizes', $address->district);
$this->assertEquals('SP', $address->state);
Expand Down
Loading

0 comments on commit 2d5213e

Please sign in to comment.