Skip to content

Commit

Permalink
Client optimization to open wsdl connection only when "find" method i…
Browse files Browse the repository at this point in the history
…s called.
  • Loading branch information
merorafael committed Jun 3, 2017
1 parent 1498da9 commit 14c8a73
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
15 changes: 3 additions & 12 deletions src/Mero/Correios/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,12 @@

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

public function __construct()
{
$this->wsdlClient = $this->createSoapClient();
}

/**
* Return the Correios SOAP client.
*
* @return \SoapClient SOAP client
*/
protected function createSoapClient()
protected function createWsdlConnection()
{
return new \SoapClient(
"https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl"
Expand All @@ -47,7 +37,8 @@ public function findAddressByZipCode($zipCode)
throw new InvalidZipCodeException('The brazilian zip code should have exacly 8 characters.');
}
try {
$address = $this->wsdlClient->__soapCall('consultaCEP', [
$wsdlConnection = $this->createWsdlConnection();
$address = $wsdlConnection->__soapCall('consultaCEP', [
'consultaCEP' => [
'cep' => $zipCode
]
Expand Down
23 changes: 9 additions & 14 deletions tests/Mero/Correios/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,13 @@ protected function createClientMock($soapClientMocked)
{
$client = $this
->getMockBuilder(Client::className())
->disableOriginalConstructor()
->setMethods(["createSoapClient"])
->setMethods(["createWsdlConnection"])
->getMock();

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

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

return $client;
}

Expand All @@ -75,8 +70,8 @@ protected function createClientMock($soapClientMocked)
*/
public function testInvalidZipCodeException()
{
$wsdlClient = $this->createSoapClientMock();
$client = $this->createClientMock($wsdlClient);
$wsdlConnection = $this->createSoapClientMock();
$client = $this->createClientMock($wsdlConnection);
$client->findAddressByZipCode("200430900");
}

Expand All @@ -85,12 +80,12 @@ public function testInvalidZipCodeException()
*/
public function testAddressNotFoundException()
{
$wsdlClient = $this
$wsdlConnection = $this
->getMockBuilder("\SoapClient")
->disableOriginalConstructor()
->getMock();

$wsdlClient
$wsdlConnection
->method("__soapCall")
->willThrowException(new \SoapFault(
"soap:Server",
Expand All @@ -101,14 +96,14 @@ public function testAddressNotFoundException()
]
));

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

public function testFindAddressByZipCode()
{
$wsdlClient = $this->createSoapClientMock();
$client = $this->createClientMock($wsdlClient);
$wsdlConnection = $this->createSoapClientMock();
$client = $this->createClientMock($wsdlConnection);
$address = $client->findAddressByZipCode("22640102");
$this->assertEquals("Avenida das Américas", $address->getAddress());
$this->assertEquals("Barra da Tijuca", $address->getNeighborhood());
Expand Down

0 comments on commit 14c8a73

Please sign in to comment.