From 14c8a73b6195098ef102070410d310394d47aed6 Mon Sep 17 00:00:00 2001 From: Rafael Mello Date: Sat, 3 Jun 2017 16:39:41 -0300 Subject: [PATCH] Client optimization to open wsdl connection only when "find" method is called. --- src/Mero/Correios/Client.php | 15 +++------------ tests/Mero/Correios/ClientTest.php | 23 +++++++++-------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/Mero/Correios/Client.php b/src/Mero/Correios/Client.php index b9d1cb4..cd86425 100644 --- a/src/Mero/Correios/Client.php +++ b/src/Mero/Correios/Client.php @@ -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" @@ -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 ] diff --git a/tests/Mero/Correios/ClientTest.php b/tests/Mero/Correios/ClientTest.php index 1ccadc7..9dcb295 100644 --- a/tests/Mero/Correios/ClientTest.php +++ b/tests/Mero/Correios/ClientTest.php @@ -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; } @@ -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"); } @@ -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", @@ -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());