Skip to content

Commit 8959129

Browse files
Correccion codigo para PHPStan y PHPUnit - Incorporacion de soporte v1 y v2
1 parent 850b170 commit 8959129

38 files changed

Lines changed: 425 additions & 319 deletions

src/ApiBase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public function __construct(
6262
private function setupAuth(array $credenciales): void
6363
{
6464
$tipo = key($credenciales); // Detecta si es 'pass' o 'cert'
65-
$datos = $credenciales[$tipo] ?? [];
65+
$datos = [];
66+
67+
if($tipo !== null && isset($credenciales[$tipo])){
68+
$datos = $credenciales[$tipo];
69+
}
6670

6771
$identificador = $datos['rut'] ??
6872
$datos['cert-data'] ??

src/ApiClient.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace apigatewaycl\api_client;
2525

2626
use Psr\Http\Message\ResponseInterface;
27+
use GuzzleHttp\Psr7\Utils;
2728

2829
/**
2930
* Cliente de la API de API Gateway.
@@ -38,7 +39,7 @@ class ApiClient
3839
*
3940
* @var string
4041
*/
41-
private $api_url = 'https://legacy.apigateway.cl';
42+
private $api_url = 'https://app.apigateway.cl';
4243

4344
/**
4445
* El prefijo para las rutas de la API.
@@ -52,7 +53,7 @@ class ApiClient
5253
*
5354
* @var string
5455
*/
55-
private $api_version = 'v1';
56+
private $api_version = 'v2';
5657

5758
/**
5859
* El token de autenticación para la API.
@@ -75,6 +76,13 @@ class ApiClient
7576
*/
7677
private $last_response = null;
7778

79+
/**
80+
* Forma de autentificar de las distintas api's.
81+
*
82+
* @var string|null
83+
*/
84+
private $authorization = null;
85+
7886
/**
7987
* Constructor del cliente de la API.
8088
*
@@ -91,6 +99,14 @@ public function __construct(?string $token = null, ?string $url = null)
9199
$this->api_url = $url ?: $this->env(
92100
'APIGATEWAY_API_URL'
93101
) ?: $this->api_url;
102+
103+
$this->authorization = 'Token';
104+
105+
$this->api_version = $this->env('VERSION');
106+
if($this->api_version == 'v1'){
107+
$this->api_url = 'https://legacy.apigateway.cl';
108+
$this->authorization = 'Bearer';
109+
}
94110
}
95111

96112
/**
@@ -117,6 +133,16 @@ public function setToken(string $token): static
117133
return $this;
118134
}
119135

136+
/**
137+
* Obtiene la última URL utilizada en la solicitud HTTP.
138+
*
139+
* @return string|null
140+
*/
141+
public function getLastApiUrl(): string|null
142+
{
143+
return $this->api_url;
144+
}
145+
120146
/**
121147
* Obtiene la última URL utilizada en la solicitud HTTP.
122148
*
@@ -373,7 +399,7 @@ public function consume(
373399

374400
// preparar cabeceras que se usarán
375401
$options[\GuzzleHttp\RequestOptions::HEADERS] = array_merge([
376-
'Authorization' => 'Bearer ' . $this->api_token,
402+
'Authorization' => $this->authorization . ' ' . $this->api_token,
377403
'Content-Type' => 'application/json',
378404
'Accept' => 'application/json',
379405
], $headers);
@@ -393,6 +419,12 @@ public function consume(
393419

394420
// Ejecutar consulta al SII.
395421
try {
422+
// echo $method;
423+
// echo $this->last_url;
424+
// echo $options;
425+
// fwrite(STDERR, $method);
426+
// fwrite(STDERR, $this->last_url);
427+
// fwrite(STDERR, json_encode($options));
396428
$this->last_response = $client->request(
397429
method: $method,
398430
uri: $this->last_url,
@@ -426,6 +458,15 @@ public function consume(
426458
}
427459

428460
// Entregar respuesta (contenida en el mismo objeto del cliente).
461+
462+
$response_body = (string) $this->last_response->getBody();
463+
$data = json_decode($response_body, true);
464+
if(json_last_error() === JSON_ERROR_NONE && isset($data['data'])){
465+
$data = $data['data'];
466+
$response_data = Utils::streamFor(json_encode($data));
467+
$this->last_response = $this->last_response->withBody($response_data);
468+
}
469+
429470
return $this;
430471
}
431472

src/sii/BheRecibidas.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function observarBheRecibida(
136136
int $causa = 1
137137
): ResponseInterface {
138138
$url = sprintf(
139-
'/sii/bhe/recibidas/observar/%s/%s/%d',
139+
'/sii/bhe/recibidas/observar/%s/%s?causa=%d',
140140
$emisor,
141141
$numero,
142142
$causa

tests/Helpers/FunctionHelpers.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Helpers;
6+
7+
use apigatewaycl\api_client\ApiException;
8+
use PHPUnit\Framework\SkippedTestSuiteError;
9+
10+
trait FunctionHelpers
11+
{
12+
protected static $client;
13+
14+
protected static function requireEnv(string $str_var): void
15+
{
16+
$value =
17+
$_ENV[$str_var]
18+
?? $_SERVER[$str_var]
19+
?? getenv($str_var);
20+
21+
if ($value == false || $value == null || $value == '') {
22+
throw new SkippedTestSuiteError(
23+
sprintf($str_var . ' no está definido.')
24+
);
25+
}
26+
}
27+
28+
protected function tearDown(): void
29+
{
30+
if (self::$client && method_exists(self::$client, 'getLastApiUrl')) {
31+
echo "\nEjecutando " .$this->name(). "() en "
32+
.self::$client->getLastApiUrl()." : ";
33+
}
34+
parent::tearDown();
35+
}
36+
37+
protected function handleApiException(ApiException $e): void
38+
{
39+
$code = (int) $e->getCode();
40+
$message = sprintf('[ApiException %d] %s',
41+
$code,
42+
$e->getMessage());
43+
if($code >= 400 && $code < 500){
44+
$this->markTestSkipped($message);
45+
}
46+
$this->fail($message);
47+
48+
}
49+
}

tests/Helpers/RequiresEnvironment.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/sii/actividades/ListarActividadesEconomicasTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,32 @@
2525
use apigatewaycl\api_client\sii\ActividadesEconomicas;
2626
use PHPUnit\Framework\Attributes\CoversClass;
2727
use PHPUnit\Framework\TestCase;
28-
use Tests\Helpers\RequiresEnvironment;
28+
use Tests\Helpers\FunctionHelpers;
2929

3030
#[CoversClass(ActividadesEconomicas::class)]
3131
/**
3232
* Clase de pruebas que permite obtener una lista de actividades económicas.
3333
*/
3434
class ListarActividadesEconomicasTest extends TestCase
3535
{
36-
use RequiresEnvironment;
36+
use FunctionHelpers;
3737

3838
protected static $verbose;
3939

4040
protected static $client;
4141

42+
private static $version;
43+
4244
public static function setUpBeforeClass(): void
4345
{
4446
self::requireEnv('APIGATEWAY_API_TOKEN');
4547
self::$verbose = env(varname: 'TEST_VERBOSE', default: false);
4648
self::$client = new ActividadesEconomicas();
49+
self::$version = env('TEST_VERSION') ?? 'v2';
4750
}
4851

4952
public function testListarActividadesEconomicas(): void
5053
{
51-
echo "test_listar_actividades_economicas(): ";
5254
try {
5355
$response = self::$client->listarActividades();
5456

@@ -61,11 +63,7 @@ public function testListarActividadesEconomicas(): void
6163
"\n";
6264
}
6365
} catch (ApiException $e) {
64-
$this->fail(sprintf(
65-
'[ApiException %d] %s',
66-
$e->getCode(),
67-
$e->getMessage()
68-
));
66+
$this->handleApiException($e);
6967
}
7068
}
7169
}

tests/sii/bhe_emitidas/AnularBheEmitidaTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
use apigatewaycl\api_client\sii\BheEmitidas;
2626
use PHPUnit\Framework\Attributes\CoversClass;
2727
use PHPUnit\Framework\TestCase;
28-
use Tests\Helpers\RequiresEnvironment;
28+
use Tests\Helpers\FunctionHelpers;
2929

3030
#[CoversClass(BheEmitidas::class)]
3131
class AnularBheEmitidaTest extends TestCase
3232
{
33-
use RequiresEnvironment;
33+
use FunctionHelpers;
3434

3535
protected static $verbose;
3636

@@ -40,9 +40,14 @@ class AnularBheEmitidaTest extends TestCase
4040

4141
private static $contribuyente_rut;
4242

43+
private static $version;
44+
4345
public static function setUpBeforeClass(): void
4446
{
4547
self::requireEnv('APIGATEWAY_API_TOKEN');
48+
self::requireEnv('TEST_CONTRIBUYENTE_RUT');
49+
self::requireEnv('TEST_CONTRIBUYENTE_CLAVE');
50+
self::requireEnv('TEST_BHE_EMITIDO_FOLIO');
4651
self::$verbose = env(varname: 'TEST_VERBOSE', default: false);
4752
self::$contribuyente_rut = env('TEST_CONTRIBUYENTE_RUT');
4853
$contribuyente_clave = env('TEST_CONTRIBUYENTE_CLAVE');
@@ -53,6 +58,7 @@ public static function setUpBeforeClass(): void
5358
],
5459
];
5560
self::$client = new BheEmitidas(self::$auth);
61+
self::$version = env('TEST_VERSION') ?? 'v2';
5662
}
5763

5864
public function testAnularBheEmitida(): void
@@ -73,11 +79,7 @@ public function testAnularBheEmitida(): void
7379
"\n";
7480
}
7581
} catch (ApiException $e) {
76-
$this->fail(sprintf(
77-
'[ApiException %d] %s',
78-
$e->getCode(),
79-
$e->getMessage()
80-
));
82+
$this->handleApiException($e);
8183
}
8284
}
8385
}

tests/sii/bhe_emitidas/DescargarPdfBheEmitidaTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
use apigatewaycl\api_client\sii\BheEmitidas;
2626
use PHPUnit\Framework\Attributes\CoversClass;
2727
use PHPUnit\Framework\TestCase;
28-
use Tests\Helpers\RequiresEnvironment;
28+
use Tests\Helpers\FunctionHelpers;
2929

3030
#[CoversClass(BheEmitidas::class)]
3131
class DescargarPdfBheEmitidaTest extends TestCase
3232
{
33-
use RequiresEnvironment;
33+
use FunctionHelpers;
3434

3535
protected static $verbose;
3636

@@ -42,9 +42,14 @@ class DescargarPdfBheEmitidaTest extends TestCase
4242

4343
private static $periodo;
4444

45+
private static $version;
46+
4547
public static function setUpBeforeClass(): void
4648
{
4749
self::requireEnv('APIGATEWAY_API_TOKEN');
50+
self::requireEnv('TEST_CONTRIBUYENTE_RUT');
51+
self::requireEnv('TEST_CONTRIBUYENTE_CLAVE');
52+
self::requireEnv('TEST_PERIODO_YMD');
4853
self::$verbose = env(varname: 'TEST_VERBOSE', default: false);
4954
self::$contribuyente_rut = env('TEST_CONTRIBUYENTE_RUT');
5055
$contribuyente_clave = env('TEST_CONTRIBUYENTE_CLAVE');
@@ -55,7 +60,8 @@ public static function setUpBeforeClass(): void
5560
],
5661
];
5762
self::$client = new BheEmitidas(self::$auth);
58-
self::$periodo = env('TEST_PERIODO');
63+
self::$periodo = env('TEST_PERIODO_YMD');
64+
self::$version = env('TEST_VERSION') ?? 'v2';
5965
}
6066

6167
public function testDescargarPdfBheEmitida(): void
@@ -72,11 +78,9 @@ public function testDescargarPdfBheEmitida(): void
7278
);
7379

7480
if (count($documentos_array) <= 0) {
75-
$this->markTestSkipped(
76-
"\n".
77-
"No hay BHEs emitidas para esta prueba.".
78-
"\n"
79-
);
81+
$this->markTestIncomplete(
82+
"No hay BHEs emitidas para esta prueba."
83+
);
8084
}
8185

8286
$codigo = $documentos_array[0]['codigo'];
@@ -116,11 +120,7 @@ public function testDescargarPdfBheEmitida(): void
116120
"\n";
117121
}
118122
} catch (ApiException $e) {
119-
$this->fail(sprintf(
120-
'[ApiException %d] %s',
121-
$e->getCode(),
122-
$e->getMessage()
123-
));
123+
$this->handleApiException($e);
124124
}
125125
}
126126
}

0 commit comments

Comments
 (0)