Skip to content

Commit

Permalink
Refatoraçoes para melhor legibilidade e testes
Browse files Browse the repository at this point in the history
  • Loading branch information
claudsonm committed Sep 14, 2019
1 parent b363f7a commit 2d3e5d9
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 54 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
23 changes: 11 additions & 12 deletions src/CepPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
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,23 +25,21 @@ class CepPromise
const ERROR_VALIDATION_CODE = 1;

/**
* Dispara a cadeia de execução para obtenção das informações do CEP dado.
*
* @param $cepRawValue
*
* @throws CepPromiseException
* Busca as informações referente ao CEP informado.
*
* @param string|int $cep
* @return Address
* @throws CepPromiseException
*/
public static function fetch($cepRawValue)
public static function fetch($cep)
{
return (new self())->run($cepRawValue);
return (new self())->run($cep);
}

/**
* Define o encadeamento das promises.
* Dispara a cadeia de execução para obtenção das informações do CEP dado.
*
* @param $cepRawValue
* @param string|int $cepRawValue
* @return Address
*/
public function run($cepRawValue): Address
Expand Down Expand Up @@ -108,8 +107,8 @@ private function validateInputLength()

private function leftPadWithZeros()
{
return function (string $cepCleanValue) {
return str_pad($cepCleanValue, self::CEP_SIZE, '0', STR_PAD_LEFT);
return function (string $cepSanitized) {
return str_pad($cepSanitized, self::CEP_SIZE, '0', STR_PAD_LEFT);
};
}

Expand Down
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
50 changes: 48 additions & 2 deletions tests/Unit/CepPromiseUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Claudsonm\CepPromise\Tests\Unit;

use ReflectionMethod;
use PHPUnit\Framework\TestCase;
use Claudsonm\CepPromise\Address;
use Claudsonm\CepPromise\CepPromise;
use Claudsonm\CepPromise\Exceptions\CepPromiseException;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

class CepPromiseUnitTest extends TestCase
{
Expand Down Expand Up @@ -51,4 +52,49 @@ public function testIfFetchMethodIsStatic()
$fetchMethod = new ReflectionMethod(CepPromise::class, 'fetch');
$this->assertTrue($fetchMethod->isStatic());
}

public function testAnAddressCanBeConvertedToAnArray()
{
$address = $this->getTestAddressAsObject();
$this->assertEqualsCanonicalizing($this->getTestAddressAsArray(), $address->toArray());
}

public function testAnAddressCanBeCreatedFromAnArray()
{
$address = Address::create($this->getTestAddressAsArray());
$this->assertEqualsCanonicalizing($this->getTestAddressAsObject(), $address);
}

/**
* Retorna o endereço de teste na forma de um array associativo.
*
* @return array
*/
private function getTestAddressAsArray(): array
{
return [
'city' => 'Aracaju',
'district' => 'Santo Antônio',
'state' => 'SE',
'street' => 'Avenida Presidente Juscelino Kubitschek',
'zipCode' => '49060535',
];
}

/**
* Retorna o endereço de teste na forma de um objeto.
*
* @return Address
*/
private function getTestAddressAsObject(): Address
{
$address = new Address();
$address->city = 'Aracaju';
$address->state = 'SE';
$address->street = 'Avenida Presidente Juscelino Kubitschek';
$address->district = 'Santo Antônio';
$address->zipCode = '49060535';

return $address;
}
}

0 comments on commit 2d3e5d9

Please sign in to comment.