Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evitar llamadas a endpoints durante los tests #1562

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion Core/Lib/Vies.php
Expand Up @@ -39,6 +39,9 @@ class Vies

private static $lastError = '';

/** @var SoapClient */
private static $client;

public static function check(string $cifnif, string $codiso): int
{
// comprobamos si la extensión soap está instalada
Expand Down Expand Up @@ -86,7 +89,11 @@ private static function getViesInfo(string $vatNumber, string $codiso): int
self::$lastError = '';

try {
$client = new SoapClient(self::VIES_URL, ['exceptions' => true]);
if (!self::$client instanceof SoapClient){
self::$client = new SoapClient(self::VIES_URL, ['exceptions' => true]);
}
$client = self::$client;

$json = json_encode(
$client->checkVat([
'countryCode' => $codiso,
Expand All @@ -113,4 +120,16 @@ private static function getViesInfo(string $vatNumber, string $codiso): int
Tools::log()->warning('error-checking-vat-number', ['%vat-number%' => $vatNumber]);
return -1;
}

/**
* Permite asignar la clase del cliente Soap
* Esto lo usamos para realizar los tests
* usando una clase Mock
*
* @param SoapClient $soapClient
*/
public static function setClient(SoapClient $soapClient)
{
self::$client = $soapClient;
}
}
60 changes: 60 additions & 0 deletions Test/Core/Lib/SoapClientMock.php
@@ -0,0 +1,60 @@
<?php

namespace FacturaScripts\Test\Core\Lib;

use SoapClient;

/**
* Esta clase la usamos para no tener que realizar
* peticiones a un endpoint durante los tests
*
* Simulamos que el servicio devuelve 'valid' true o false
*/
class SoapClientMock extends SoapClient
{
public function __construct()
{
//
}

/**
* @param array $requestData
* @return array
*/
public function checkVat(array $requestData)
{
$responseData = [
['results' => -1, 'number' => '', 'iso' => ''],
['results' => -1, 'number' => '123', 'iso' => ''],
['results' => 0, 'number' => '123456789', 'iso' => 'ES'],
['results' => 0, 'number' => 'ES74003828J', 'iso' => 'ES'],
['results' => 1, 'number' => 'ES74003828V', 'iso' => 'ES'],
['results' => 1, 'number' => '74003828V', 'iso' => 'ES'],
['results' => 1, 'number' => '43834596223', 'iso' => 'FR'],
['results' => 0, 'number' => '81328757100011', 'iso' => 'FR'],
['results' => 1, 'number' => '514356480', 'iso' => 'PT'],
['results' => 1, 'number' => '513969144', 'iso' => 'PT'],
['results' => 0, 'number' => '513967144', 'iso' => 'PT'],
['results' => 0, 'number' => '12345678A', 'iso' => 'IT'],
['results' => 1, 'number' => '02839750995', 'iso' => 'IT'],
['results' => 0, 'number' => '12345678A', 'iso' => 'PT'],
['results' => 1, 'number' => '503297887', 'iso' => 'PT'],
['results' => 1, 'number' => 'B13658620', 'iso' => 'ES'],
['results' => 0, 'number' => '12345678A', 'iso' => 'ES'],
['results' => 1, 'number' => 'B87533303', 'iso' => 'ES'],
['results' => 1, 'number' => 'B01563311', 'iso' => 'ES'],
];

foreach ($responseData as $data) {
if ($requestData['vatNumber'] == $data['number'] && $requestData['countryCode'] == $data['iso']){
return [
'valid' => $data['results'] === 1,
];
}
}

return [
'valid' => false,
];
}
}
8 changes: 5 additions & 3 deletions Test/Core/Lib/ViesTest.php
Expand Up @@ -24,6 +24,11 @@

final class ViesTest extends TestCase
{
protected function setUp(): void
{
Vies::setClient(new SoapClientMock());
}

public function testCheck(): void
{
$data = [
Expand All @@ -48,9 +53,6 @@ public function testCheck(): void
}

$this->assertEquals($item['results'], $check);

// esperamos medio segundo para no saturar el servicio
usleep(500000);
}
}
}
3 changes: 3 additions & 0 deletions Test/Core/Model/AgenteTest.php
Expand Up @@ -21,6 +21,7 @@

use FacturaScripts\Core\Lib\Vies;
use FacturaScripts\Core\Model\Agente;
use FacturaScripts\Test\Core\Lib\SoapClientMock;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -88,6 +89,8 @@ public function testEmailField(): void

public function testVies(): void
{
Vies::setClient(new SoapClientMock());

// creamos un agente sin cifnif
$agent = new Agente();
$agent->codagente = 'Test';
Expand Down
3 changes: 3 additions & 0 deletions Test/Core/Model/ClienteTest.php
Expand Up @@ -21,6 +21,7 @@

use FacturaScripts\Core\Lib\Vies;
use FacturaScripts\Core\Model\Cliente;
use FacturaScripts\Test\Core\Lib\SoapClientMock;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -180,6 +181,8 @@ public function testPaymentDays(): void

public function testVies(): void
{
Vies::setClient(new SoapClientMock());

// creamos un cliente sin cifnif
$cliente = new Cliente();
$cliente->nombre = 'Test';
Expand Down
3 changes: 3 additions & 0 deletions Test/Core/Model/ContactoTest.php
Expand Up @@ -23,6 +23,7 @@
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Lib\Vies;
use FacturaScripts\Core\Model\Contacto;
use FacturaScripts\Test\Core\Lib\SoapClientMock;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use FacturaScripts\Test\Traits\RandomDataTrait;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -197,6 +198,8 @@ public function testNotNullFields(): void

public function testVies(): void
{
Vies::setClient(new SoapClientMock());

// creamos un contacto sin cif/nif
$contact = new Contacto();
$contact->nombre = 'Test';
Expand Down
6 changes: 2 additions & 4 deletions Test/Core/Model/FacturaClienteTest.php
Expand Up @@ -29,6 +29,7 @@
use FacturaScripts\Core\Model\Ejercicio;
use FacturaScripts\Core\Model\FacturaCliente;
use FacturaScripts\Core\Model\Stock;
use FacturaScripts\Test\Core\Lib\SoapClientMock;
use FacturaScripts\Test\Traits\DefaultSettingsTrait;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use FacturaScripts\Test\Traits\RandomDataTrait;
Expand Down Expand Up @@ -719,10 +720,7 @@ public function testIntraCommunity(): void

public function testSetIntraCommunity(): void
{
// comprobamos primero si el VIES funciona
if (Vies::getLastError() != '') {
$this->markTestSkipped('Vies service is not available');
}
Vies::setClient(new SoapClientMock());

// establecemos la empresa en España con un cif español
$company = Empresas::default();
Expand Down
6 changes: 2 additions & 4 deletions Test/Core/Model/FacturaProveedorTest.php
Expand Up @@ -29,6 +29,7 @@
use FacturaScripts\Core\Lib\RegimenIVA;
use FacturaScripts\Core\Lib\Vies;
use FacturaScripts\Core\Model\FacturaProveedor;
use FacturaScripts\Test\Core\Lib\SoapClientMock;
use FacturaScripts\Test\Traits\DefaultSettingsTrait;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use FacturaScripts\Test\Traits\RandomDataTrait;
Expand Down Expand Up @@ -576,10 +577,7 @@ public function testIntraCommunity(): void

public function testSetIntraCommunity(): void
{
// comprobamos si el VIES funciona
if (Vies::getLastError() != '') {
$this->markTestSkipped('Vies service is not available');
}
Vies::setClient(new SoapClientMock());

// establecemos la empresa en España con un cif español
$company = Empresas::default();
Expand Down
3 changes: 3 additions & 0 deletions Test/Core/Model/ProveedorTest.php
Expand Up @@ -21,6 +21,7 @@

use FacturaScripts\Core\Lib\Vies;
use FacturaScripts\Core\Model\Proveedor;
use FacturaScripts\Test\Core\Lib\SoapClientMock;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -154,6 +155,8 @@ public function testGoodWeb(): void

public function testVies(): void
{
Vies::setClient(new SoapClientMock());

// creamos un proveedor sin cif/nif
$proveedor = new Proveedor();
$proveedor->nombre = 'Test';
Expand Down