Skip to content

Commit 25312e0

Browse files
Correccion PHPStan, PHPUnit, Upgrade php 8.5
1 parent feb0682 commit 25312e0

23 files changed

Lines changed: 119 additions & 103 deletions

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
strategy:
1515
matrix:
16-
php-version: ['8.4', '8.5']
16+
php-version: ['8.5']
1717

1818
steps:
1919
- name: Check out repository code
@@ -33,6 +33,7 @@ jobs:
3333
env:
3434
LIBREDTE_HASH: ${{ secrets.LIBREDTE_HASH }}
3535
LIBREDTE_RUT: ${{ vars.LIBREDTE_RUT }}
36+
ADD_SKIPPED: true
3637
run: |
3738
composer tests-readonly
3839

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"source": "https://github.com/LibreDTE/libredte-api-client-php"
1717
},
1818
"require": {
19-
"php": "^8.2 || ^8.3",
19+
"php": "^8.5",
2020
"ext-curl": "*"
2121
},
2222
"autoload": {
@@ -27,12 +27,13 @@
2727
"autoload-dev": {
2828
"psr-4": {
2929
"libredte\\dte_facturacion\\": "tests/dte_facturacion/",
30+
"libredte\\helpers\\": "tests/Helpers/",
3031
"libredte\\pagos_cobros_masivos\\": "tests/pagos_cobros_masivos/"
3132
}
3233
},
3334
"require-dev": {
3435
"friendsofphp/php-cs-fixer": "^3.94",
35-
"phpstan/phpstan": "^1.12",
36+
"phpstan/phpstan": "^2",
3637
"phpunit/phpunit": "^13.0",
3738
"vlucas/phpdotenv": "^5.6"
3839
},

src/ApiClient.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ class ApiClient
7373
* @param string|null $url URL base de la API de LibreDTE.
7474
* @throws ApiException si el hash de autenticación no está presente.
7575
*/
76-
public function __construct(string $hash = null, string $url = null)
77-
{
76+
public function __construct(
77+
?string $hash = null,
78+
?string $url = null
79+
) {
7880
$hash = $hash ?: $this->env('LIBREDTE_HASH');
7981
if (!$hash) {
8082
throw new ApiException(message: 'LIBREDTE_HASH missing');
@@ -126,8 +128,11 @@ public function setSSL($sslcheck = true): void
126128
* @param array $headers Encabezados adicionales para la solicitud.
127129
* @return array Respuesta de la API.
128130
*/
129-
public function post(string $resource, mixed $data = null, array $headers = []): array|bool
130-
{
131+
public function post(
132+
string $resource,
133+
mixed $data = null,
134+
array $headers = []
135+
): array|bool {
131136
$headers = array_merge($this->headers, $headers);
132137
return $this->client->query(
133138
method: 'POST',
@@ -148,8 +153,11 @@ public function post(string $resource, mixed $data = null, array $headers = []):
148153
* @param array $headers Encabezados adicionales para la solicitud.
149154
* @return array Respuesta de la API.
150155
*/
151-
public function get(string $resource, mixed $data = null, array $headers = []): array|bool
152-
{
156+
public function get(
157+
string $resource,
158+
mixed $data = null,
159+
array $headers = []
160+
): array|bool {
153161
$headers = array_merge($this->headers, $headers);
154162
return $this->client->query(
155163
method: 'GET',

src/ApiException.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,20 @@
3535
*/
3636
class ApiException extends \Exception
3737
{
38-
// Aquí van los métodos y propiedades de la clase
38+
private ?string $responseBody;
39+
40+
public function __construct(
41+
string $message = '',
42+
int $code = 0,
43+
?string $responseBody = null,
44+
?\Throwable $previous = null
45+
) {
46+
parent::__construct($message, $code, $previous);
47+
$this->responseBody = $responseBody;
48+
}
49+
50+
public function getResponseBody(): ?string
51+
{
52+
return $this->responseBody;
53+
}
3954
}

src/HttpCurlClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function query(
130130
$url = sprintf("%s?%s", $url, $data);
131131
}
132132
} else {
133-
curl_setopt($curl, CURLOPT_POST, 1);
133+
curl_setopt($curl, CURLOPT_POST, true);
134134
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
135135
if ($data) {
136136
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
@@ -144,17 +144,17 @@ public function query(
144144
curl_setopt($curl, CURLOPT_HTTPHEADER, array_values($headers));
145145
// realizar consulta a curl recuperando cabecera y cuerpo
146146
curl_setopt($curl, CURLOPT_URL, $url);
147-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
148-
curl_setopt($curl, CURLOPT_HEADER, 1);
147+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
148+
curl_setopt($curl, CURLOPT_HEADER, true);
149149
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->sslcheck);
150150
$response = curl_exec($curl);
151151
if (!$response) {
152152
$this->errors[] = curl_error($curl);
153153
return false;
154154
}
155155
$headers_size = curl_getinfo(handle: $curl, option: CURLINFO_HEADER_SIZE);
156-
// cerrar conexión de curl
157-
curl_close($curl);
156+
157+
158158
// entregar respuesta de la solicitud
159159
$response_headers = $this->parseResponseHeaders(
160160
substr(

tests/Helpers/FunctionHelpers.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace libredte\helpers;
6+
7+
use libredte\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 handleApiException(ApiException $e): void
29+
{
30+
$code = (int) $e->getCode();
31+
$message = sprintf(
32+
'[ApiException %d] %s',
33+
$code,
34+
$e->getMessage()
35+
);
36+
if ($code >= 400 && $code < 500 && env('ADD_SKIPPED', false)) {
37+
$this->markTestSkipped($message);
38+
}
39+
$this->fail($message);
40+
41+
}
42+
}

tests/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141

4242
/**
4343
* Función que carga una variable de entorno o su valor por defecto
44-
* @param string varname Variable que se desea consultar
45-
* @param mixed|null default Valor por defecto de la variable
44+
* @param string $varname Variable que se desea consultar
45+
* @param mixed|null $default Valor por defecto de la variable
4646
*/
4747
function env(string $varname, mixed $default = null)
4848
{

tests/dte_facturacion/AbstractDteFacturacion.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525

2626
use libredte\api_client\ApiClient;
2727
use libredte\api_client\ApiException;
28+
use libredte\helpers\FunctionHelpers;
2829
use PHPUnit\Framework\TestCase;
2930

3031
abstract class AbstractDteFacturacion extends TestCase
3132
{
33+
use FunctionHelpers;
34+
3235
/**
3336
* Variable para desplegar resultados.
3437
*
@@ -97,8 +100,10 @@ abstract class AbstractDteFacturacion extends TestCase
97100
*/
98101
public static function setUpBeforeClass(): void
99102
{
103+
self::requireEnv('LIBREDTE_HASH');
104+
self::requireEnv('LIBREDTE_RUT');
100105
self::$verbose = (bool)env(varname: 'TEST_VERBOSE', default: 'false');
101-
self::$emisor_rut = (
106+
self::$emisor_rut = (int)(
102107
explode(
103108
'-',
104109
(string)env('LIBREDTE_RUT')

tests/dte_facturacion/BuscarDteTempTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ public function testBuscarDteTemp(): void
7979
} catch (ApiException $e) {
8080
// Si falla, desplegará el mensaje y error en el siguiente formato:
8181
// [ApiException codigo-http] mensaje]
82-
$this->fail(sprintf(
83-
'[ApiException %d] %s',
84-
$e->getCode(),
85-
$e->getMessage()
86-
));
82+
$this->handleApiException($e);
8783
}
8884
}
8985
}

tests/dte_facturacion/DescargarPdfDteTempTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,7 @@ public function testDescargarPdfDteTemp(): void
101101
} catch (ApiException $e) {
102102
// Si falla, desplegará el mensaje y error en el siguiente formato:
103103
// [ApiException codigo-http] mensaje]
104-
$this->fail(sprintf(
105-
'[ApiException %d] %s',
106-
$e->getCode(),
107-
$e->getMessage()
108-
));
104+
$this->handleApiException($e);
109105
}
110106
}
111107
}

0 commit comments

Comments
 (0)