From 4d9daa6fcaf7cd0674888f7174214a7ee4aa40aa Mon Sep 17 00:00:00 2001 From: Francimar Alves Date: Tue, 5 Apr 2022 18:21:17 -0300 Subject: [PATCH] =?UTF-8?q?tests:=20Otimiza=20mem=C3=B3ria=20nos=20testes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpunit.xml | 11 ++++++++++- src/NFe/Common/Ajuste.php | 4 ++-- src/NFe/Database/IBPT.php | 2 -- tests/NFe/Common/ConfiguracaoTest.php | 14 ++++++++------ tests/NFe/Core/SEFAZTest.php | 15 +++++++++++++-- tests/NFe/Database/BancoTest.php | 6 +++--- tests/NFe/Task/InutilizacaoTest.php | 15 +++++++++++---- 7 files changed, 47 insertions(+), 20 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 526669f..f88042b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,13 @@ - + src/NFe @@ -15,6 +23,7 @@ + diff --git a/src/NFe/Common/Ajuste.php b/src/NFe/Common/Ajuste.php index 50bfe75..d153c35 100644 --- a/src/NFe/Common/Ajuste.php +++ b/src/NFe/Common/Ajuste.php @@ -102,8 +102,8 @@ public function __construct($ajuste = []) $this->setTempoLimite(30); $root_path = dirname(dirname(dirname(__DIR__))); $cert_dir = $root_path . '/storage/certs'; - $this->setArquivoChavePublica($cert_dir . '/public.pem'); - $this->setArquivoChavePrivada($cert_dir . '/private.pem'); + $this->getCertificado()->setArquivoChavePublica($cert_dir . '/public.pem'); + $this->getCertificado()->setArquivoChavePrivada($cert_dir . '/private.pem'); $this->setPastaXmlBase($root_path . '/storage/xml'); $this->setPastaXmlInutilizado('{ambiente}/inutilizado'); diff --git a/src/NFe/Database/IBPT.php b/src/NFe/Database/IBPT.php index 829c2ab..994a8f5 100644 --- a/src/NFe/Database/IBPT.php +++ b/src/NFe/Database/IBPT.php @@ -67,8 +67,6 @@ private function load($uf) return false; } $data = json_decode($content, true); - unset($this->tabela); - gc_collect_cycles(); $this->tabela = [$uf => $data]; return $data; } diff --git a/tests/NFe/Common/ConfiguracaoTest.php b/tests/NFe/Common/ConfiguracaoTest.php index 3232b4b..9b08780 100644 --- a/tests/NFe/Common/ConfiguracaoTest.php +++ b/tests/NFe/Common/ConfiguracaoTest.php @@ -4,6 +4,7 @@ class ConfiguracaoTest extends \PHPUnit\Framework\TestCase { + /** @var Configuracao */ private $config; protected function setUp(): void @@ -51,11 +52,12 @@ public function testFields() public function testDataExpiracao() { $this->config - ->setArquivoChavePublica(dirname(dirname(__DIR__)) . '/resources/certs/public.pem') - ->setArquivoChavePrivada(dirname(dirname(__DIR__)) . '/resources/certs/private.pem'); - $this->config - ->setChavePublica($this->config->getChavePublica()) - ->setChavePrivada($this->config->getChavePrivada()); - $this->assertEquals('2010-10-02', date('Y-m-d', $this->config->getExpiracao())); + ->getCertificado() + ->setArquivoChavePublica(dirname(dirname(__DIR__)) . '/resources/certs/public.pem') + ->setArquivoChavePrivada(dirname(dirname(__DIR__)) . '/resources/certs/private.pem'); + $this->config->getCertificado() + ->setChavePublica($this->config->getCertificado()->getChavePublica()) + ->setChavePrivada($this->config->getCertificado()->getChavePrivada()); + $this->assertEquals('2010-10-02', date('Y-m-d', $this->config->getCertificado()->getExpiracao())); } } diff --git a/tests/NFe/Core/SEFAZTest.php b/tests/NFe/Core/SEFAZTest.php index c886605..a8f094e 100644 --- a/tests/NFe/Core/SEFAZTest.php +++ b/tests/NFe/Core/SEFAZTest.php @@ -2,6 +2,7 @@ namespace NFe\Core; +use NFe\Database\Estatico; use NFe\Logger\Log; use NFe\Task\Inutilizacao; use NFe\Task\Tarefa; @@ -23,11 +24,21 @@ protected function tearDown(): void */ public static function createSEFAZ() { + $banco = SEFAZ::getInstance()->getConfiguracao()->getBanco(); + if ($banco instanceof Estatico) { + $banco->setIBPT(null); + } + SEFAZ::getInstance()->getConfiguracao()->setBanco(null); + SEFAZ::getInstance()->getConfiguracao()->setCertificado(null); + SEFAZ::getInstance()->setConfiguracao(null); + gc_collect_cycles(); $emitente = \NFe\Entity\EmitenteTest::createEmitente(); $sefaz = SEFAZ::getInstance(true); $sefaz->getConfiguracao() - ->setArquivoChavePublica(dirname(dirname(__DIR__)) . '/resources/certs/public.pem') - ->setArquivoChavePrivada(dirname(dirname(__DIR__)) . '/resources/certs/private.pem') + ->getCertificado() + ->setArquivoChavePublica(dirname(dirname(__DIR__)) . '/resources/certs/public.pem') + ->setArquivoChavePrivada(dirname(dirname(__DIR__)) . '/resources/certs/private.pem'); + $sefaz->getConfiguracao() ->setEmitente($emitente); return $sefaz; } diff --git a/tests/NFe/Database/BancoTest.php b/tests/NFe/Database/BancoTest.php index 7ef1cf9..2c81236 100644 --- a/tests/NFe/Database/BancoTest.php +++ b/tests/NFe/Database/BancoTest.php @@ -222,12 +222,12 @@ public function onlineTestWSDL() global $app; $sefaz = \NFe\Core\SEFAZTest::createSEFAZ(); - $sefaz->getConfiguracao() + $sefaz->getConfiguracao()->getCertificado() ->setArquivoChavePublica(dirname(dirname(dirname(__DIR__))) . '/docs/certs/public.pem') ->setArquivoChavePrivada(dirname(dirname(dirname(__DIR__))) . '/docs/certs/private.pem'); $banco = $sefaz->getConfiguracao()->getBanco(); - $chave_publica = $sefaz->getConfiguracao()->getArquivoChavePublica(); - $chave_privada = $sefaz->getConfiguracao()->getArquivoChavePrivada(); + $chave_publica = $sefaz->getConfiguracao()->getCertificado()->getArquivoChavePublica(); + $chave_privada = $sefaz->getConfiguracao()->getCertificado()->getArquivoChavePrivada(); $soap = new \Curl\Curl(); $soap->setOpt(CURLOPT_SSLCERT, $chave_publica); $soap->setOpt(CURLOPT_SSLKEY, $chave_privada); diff --git a/tests/NFe/Task/InutilizacaoTest.php b/tests/NFe/Task/InutilizacaoTest.php index 2882548..9c5efa3 100644 --- a/tests/NFe/Task/InutilizacaoTest.php +++ b/tests/NFe/Task/InutilizacaoTest.php @@ -8,6 +8,11 @@ class InutilizacaoTest extends \PHPUnit\Framework\TestCase { public static function criaInutilizacao() { + \NFe\Core\SEFAZ::getInstance() + ->getConfiguracao()->getEmitente()->getEndereco() + ->getMunicipio()->getEstado()->setUF('PR'); + \NFe\Core\SEFAZ::getInstance() + ->getConfiguracao()->getEmitente()->setCNPJ('08380787000176'); $inutilizacao = new \NFe\Task\Inutilizacao(); $inutilizacao->setUF(\NFe\Core\SEFAZ::getInstance() ->getConfiguracao()->getEmitente()->getEndereco() @@ -57,7 +62,7 @@ public function testInutilizaInutilizado() $inutilizacao->fromArray($inutilizacao); $inutilizacao->fromArray($inutilizacao->toArray()); $inutilizacao->fromArray(null); - } catch (Exception $e) { + } catch (\Exception $e) { \NFe\Common\CurlSoap::setPostFunction(null); throw $e; } @@ -84,14 +89,16 @@ public function testInutilizaRejeitado() $inutilizacao = self::criaInutilizacao(); $dom = $inutilizacao->getNode()->ownerDocument; $dom = $inutilizacao->assinar(); + $this->expectException('\Exception'); try { - $this->expectException('\Exception'); $inutilizacao->envia($dom); - } catch (Exception $e) { + } catch (\Exception $e) { \NFe\Common\CurlSoap::setPostFunction(null); $this->assertEquals('241', $inutilizacao->getStatus()); + throw $e; + } finally { + \NFe\Common\CurlSoap::setPostFunction(null); } - \NFe\Common\CurlSoap::setPostFunction(null); } public function testNormalization()