Skip to content

Commit

Permalink
README improvements; Added "createSoapClient" method on Client; Unit …
Browse files Browse the repository at this point in the history
…tests not request webservice connection(SoapClient mocked).
  • Loading branch information
merorafael committed Jun 3, 2017
1 parent b5a9368 commit 5ac7587
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 30 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Mero Correios
=============

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/9fadab25-eff8-4439-b3e5-51315e18e413/mini.png)](https://insight.sensiolabs.com/projects/9fadab25-eff8-4439-b3e5-51315e18e413)
[![Build Status](https://travis-ci.org/merorafael/correios.svg?branch=master)](https://travis-ci.org/merorafael/correios)
[![Coverage Status](https://coveralls.io/repos/github/merorafael/correios/badge.svg?branch=master)](https://coveralls.io/github/merorafael/correios?branch=master)
[![Build Status](https://travis-ci.org/merorafael/php-correios.svg?branch=master)](https://travis-ci.org/merorafael/php-correios)
[![Coverage Status](https://coveralls.io/repos/github/merorafael/php-correios/badge.svg?branch=master)](https://coveralls.io/github/merorafael/php-correios?branch=master)
[![Latest Stable Version](https://poser.pugx.org/mero/correios/v/stable.svg)](https://packagist.org/packages/mero/correios)
[![Total Downloads](https://poser.pugx.org/mero/correios/downloads.svg)](https://packagist.org/packages/mero/correios)
[![License](https://poser.pugx.org/mero/correios/license.svg)](https://packagist.org/packages/mero/correios)
Expand All @@ -22,3 +22,31 @@ Instalation with composer
1. Open your project directory;
2. Run `composer require mero/correios` to add `Mero Correios`
in your project vendor.

Client methods
--------------

| Method | Description | Parameters | Return | Exceptions |
| -------------------- | --------------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| findAddressByZipCode | Find address informations using the zip code. | $zipCode | [Mero\Correios\Model\Address](https://github.com/merorafael/php-correios/blob/master/src/Mero/Correios/Model/Address.php) | [AddressNotFoundException](https://github.com/merorafael/php-correios/blob/master/src/Mero/Correios/Exception/AddressNotFoundException.php) and [InvalidZipCodeException](https://github.com/merorafael/php-correios/blob/master/src/Mero/Correios/Exception/InvalidZipCodeException.php) |

Usage
-----

Declare an instance of object `Mero\Correios\Client` and use the methods
available in the client.

**Example:**

```php
<?php

$correios = new \Mero\Correios\Client();
$address = $correios->findAddressByZipCode('22640102'); // Return Address object related to '22640-102' zip-code.

echo $address->getAddress(); // Return the address 'Avenida das Américas'
echo $address->getNeighborhood(); // Return the neighborhood 'Barra da Tijuca'
echo $address->getCity(); // Return the city 'Rio de Janeiro'
echo $address->getState(); // Return the state 'RJ'
echo $address->getZipCode(); // Return the address '22640102'
```
42 changes: 31 additions & 11 deletions src/Mero/Correios/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,32 @@

class Client
{
/**
* Correios WSDL URI
*/
const CORREIOS_WSDL_URI = "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl";

/**
* @var \SoapClient Correios WSDL Client
*/
protected $wsdl;
protected $wsdlClient;

public function __construct()
{
$this->wsdl = new \SoapClient(self::CORREIOS_WSDL_URI);
$this->wsdlClient = $this->createSoapClient();
}

/**
* Return the Correios SOAP client.
*
* @return \SoapClient SOAP client
*/
protected function createSoapClient()
{
return new \SoapClient(
"https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl"
);
}

/**
* Find address informations using the zip code.
*
* @param string $zipCode Zip code
* @param string $zipCode Zip code without special characters
*
* @return Address Address informations
*
Expand All @@ -35,11 +42,12 @@ public function __construct()
*/
public function findAddressByZipCode($zipCode)
{
if (strlen($zipCode) != 9) {
throw new InvalidZipCodeException('The brazilian zip code should have exacly 9 characters.');
$zipCode = preg_replace('/[^0-9]/', '', $zipCode);
if (strlen($zipCode) != 8) {
throw new InvalidZipCodeException('The brazilian zip code should have exacly 8 characters.');
}
try {
$address = $this->wsdl->__soapCall('consultaCEP', [
$address = $this->wsdlClient->__soapCall('consultaCEP', [
'consultaCEP' => [
'cep' => $zipCode
]
Expand All @@ -56,4 +64,16 @@ public function findAddressByZipCode($zipCode)
throw new AddressNotFoundException($e->getMessage());
}
}

/**
* Return the class name. Please, not use this method if you are using PHP 5.5 or above.
*
* @return string Class name
*
* @deprecated In PHP 5.5 or above you don't need to use this method. The native constant "::class" was implemented.
*/
public static function className()
{
return get_called_class();
}
}
103 changes: 86 additions & 17 deletions tests/Mero/Correios/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,118 @@

namespace Mero\Correios;

use Mero\Correios\Exception\InvalidZipCodeException;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

class ClientTest extends PHPUnitTestCase
{
/**
* @var Client Correios Client
*/
protected $client;

/**
* @inheritDoc
*/
public function setUp()
{
if (!extension_loaded('soap')) {
$this->markTestSkipped('This test requires soap to run');
if (!extension_loaded("soap")) {
$this->markTestSkipped("This test requires soap to run");
}
$this->client = new Client();
}

/**
* Return the Soap Client mocked.
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function createSoapClientMock()
{
$soapClientMocked = $this
->getMockBuilder("\SoapClient")
->disableOriginalConstructor()
->getMock();

$soapClientMocked
->method("__soapCall")
->willReturn((object) [
"return" => (object) [
"end" => "Avenida das Américas",
"bairro" => "Barra da Tijuca",
"cidade" => "Rio de Janeiro",
"uf" => "RJ",
"cep" => "22640102",
],
]);

return $soapClientMocked;
}

/**
* Return the Correios Client mocked for not accessing webservice.
*
* @param \PHPUnit_Framework_MockObject_MockObject $soapClientMocked
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function createClientMock($soapClientMocked)
{
$client = $this
->getMockBuilder(Client::className())
->disableOriginalConstructor()
->setMethods(["createSoapClient"])
->getMock();

$client
->method("createSoapClient")
->willReturn($soapClientMocked);

$reflectedClass = new \ReflectionClass(Client::className());
$constructor = $reflectedClass->getConstructor();
$constructor->invoke($client, []);

return $client;
}

/**
* @expectedException \Mero\Correios\Exception\InvalidZipCodeException
*/
public function testInvalidZipCodeException()
{
$this->client->findAddressByZipCode('200430-900');
$wsdlClient = $this->createSoapClientMock();
$client = $this->createClientMock($wsdlClient);
$client->findAddressByZipCode("200430900");
}

/**
* @expectedException \Mero\Correios\Exception\AddressNotFoundException
*/
public function testAddressNotFoundException()
{
$this->client->findAddressByZipCode('20000-000');
$wsdlClient = $this
->getMockBuilder("\SoapClient")
->disableOriginalConstructor()
->getMock();

$wsdlClient
->method("__soapCall")
->willThrowException(new \SoapFault(
"soap:Server",
"CEP NAO ENCONTRADO",
"",
(object) [
"SigepClienteException" => "CEP NAO ENCONTRADO"
]
));

$client = $this->createClientMock($wsdlClient);
$client->findAddressByZipCode("20000000");
}

public function testFindAddressByZipCode()
{
$address = $this->client->findAddressByZipCode('20081-262');
$this->assertEquals('Rua Sacadura Cabral', $address->getAddress());
$this->assertEquals('Saúde', $address->getNeighborhood());
$this->assertEquals('Rio de Janeiro', $address->getCity());
$this->assertEquals('RJ', $address->getState());
$this->assertEquals('20081262', $address->getZipCode());
$wsdlClient = $this->createSoapClientMock();
$client = $this->createClientMock($wsdlClient);
$address = $client->findAddressByZipCode("22640102");
$this->assertEquals("Avenida das Américas", $address->getAddress());
$this->assertEquals("Barra da Tijuca", $address->getNeighborhood());
$this->assertEquals("Rio de Janeiro", $address->getCity());
$this->assertEquals("RJ", $address->getState());
$this->assertEquals("22640102", $address->getZipCode());
}
}
}

0 comments on commit 5ac7587

Please sign in to comment.