Skip to content

Commit 8c07ec8

Browse files
committed
Se ha añadido el workflow de ci.yml para test automatizado (Solo con tests de lectura o que usen GET). Se añadieron scripts a composer, y se añadió testsuites y configuraciones nuevas a phpunit.xml. Se hicieron correcciones importantes en tests/bhe y se probó exitosamente.
1 parent cde47a3 commit 8c07ec8

9 files changed

Lines changed: 146 additions & 19 deletions

File tree

.github/workflows/ci.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master # Ejecuta pruebas al hacer push en la rama 'master'
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
php-version: ['7.3', '8.0'] # Puedes cambiar las versiones según necesites
15+
16+
steps:
17+
- name: Check out repository code
18+
uses: actions/checkout@v3
19+
20+
- name: Setup PHP ${{ matrix.php-version }}
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php-version }} # Configura la versión de PHP desde la matriz
24+
extensions: curl, mbstring, intl, xdebug # Instala extensiones de PHP si es necesario
25+
toos: composer
26+
27+
- name: Install Composer dependencies
28+
run: composer install --no-progress --no-suggest
29+
30+
- name: Create temporary env file
31+
run: |
32+
echo "BHEXPRESS_API_TOKEN=${{ secrets.BHEXPRESS_API_TOKEN }}" >> tests/test.env
33+
echo 'BHEXPRESS_EMISOR_RUT="76192083-9"' >> tests/test.env
34+
35+
- name: Run PHP tests
36+
run: |
37+
composer tests
38+
39+
- name: Upload PHPUnit result report
40+
if: failure()
41+
uses: actions/upload-artifact@v4
42+
with:
43+
name: tests-testdox-php_${{ matrix.php-version }}.txt
44+
path: var/tests-testdox.txt
45+
46+
- name: Display PHP version
47+
run: php -v

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@
3131
},
3232
"autoload": {
3333
"psr-4": {"bhexpress\\api_client\\": "src/"}
34+
},
35+
"scripts": {
36+
"docs": "php tools/phpdocumentor run --config=phpdoc.xml",
37+
"tests": "XDEBUG_MODE=coverage vendor/bin/phpunit --configuration=phpunit.xml",
38+
"tests_readonly": "vendor/bin/phpunit --configuration=phpunit.xml --no-coverage --testsuite readonly",
39+
"tests_authneeded": "vendor/bin/phpunit --configuration=phpunit.xml --no-coverage --testsuite authneeded"
3440
}
3541
}

phpunit.xml

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
1-
<phpunit bootstrap="tests/bootstrap.php" colors="true" verbose="true">
1+
<phpunit
2+
backupGlobals="true"
3+
bootstrap="tests/bootstrap.php"
4+
cacheDirectory="var/cache/phpunit"
5+
processIsolation="true"
6+
executionOrder="depends,defects"
7+
colors="true"
8+
testdox="true"
9+
verbose="true"
10+
requireCoverageMetadata="true"
11+
beStrictAboutCoverageMetadata="true"
12+
beStrictAboutOutputDuringTests="false"
13+
beStrictAboutChangesToGlobalState="true"
14+
stopOnError="true"
15+
stopOnFailure="true"
16+
stopOnWarning="true"
17+
stopOnDeprecation="true"
18+
stopOnNotice="true"
19+
failOnEmptyTestSuite="true"
20+
failOnWarning="true"
21+
failOnRisky="true"
22+
failOnDeprecation="true"
23+
failOnPhpunitDeprecation="true"
24+
failOnNotice="true"
25+
>
226
<testsuites>
3-
<testsuite name="API BHExpress: Cliente de API en PHP - Pruebas Unitarias.">
27+
<testsuite name="BHExpress: Cliente de API en PHP - Pruebas Unitarias.">
428
<directory>tests</directory>
529
</testsuite>
30+
<testsuite name="readonly">
31+
<file>tests/bhe/BoletaListarTest.php</file>
32+
<file>tests/bhe/BoletaPdfTest.php</file>
33+
</testsuite>
34+
<testsuite name="authneeded">
35+
<file>tests/bhe/BoletaEmitirTest.php</file>
36+
<file>tests/bhe/BoletaEmailTest.php</file>
37+
<file>tests/bhe/BoletaAnularTest.php</file>
38+
</testsuite>
639
</testsuites>
740
<filter>
841
<whitelist>
942
<directory suffix=".php">src</directory>
1043
</whitelist>
1144
</filter>
45+
<php>
46+
<ini name="memory_limit" value="-1" />
47+
<env name="BHEXPRESS_API_URL" value="https://apigateway.cl" />
48+
<env name="BHEXPRESS_EMISOR_RUT" value="76192083-9" />
49+
<env name="TEST_PERIODO" value="202407" />
50+
</php>
1251
</phpunit>

tests/bhe/BoletaAnularTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function setUpBeforeClass(): void
3737
self::$verbose = env('TEST_VERBOSE', false);
3838
self::$client = new ApiClient();
3939
self::$emisor_rut = env('BHEXPRESS_EMISOR_RUT');
40-
self::$numero_bhe = env('TEST_EMAIL_NUMEROBHE', '0');
40+
self::$numero_bhe = env('TEST_NUMEROBHE', '0');
4141
self::$causa = 3;
4242
}
4343

tests/bhe/BoletaEmailTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,41 @@ class BoletaEmailTest extends TestCase
3030
protected static $client;
3131
protected static $emisor_rut;
3232
protected static $email;
33-
protected static $numero_bhe;
3433

3534
public static function setUpBeforeClass(): void
3635
{
3736
self::$verbose = env('TEST_VERBOSE', false);
3837
self::$client = new ApiClient();
3938
self::$emisor_rut = env('BHEXPRESS_EMISOR_RUT');
40-
self::$email = env('TEST_EMAIL_CORREO', '');
41-
self::$numero_bhe = env('TEST_EMAIL_NUMEROBHE', '0');
39+
self::$email = env('TEST_CORREO', '');
40+
}
41+
42+
private function _buscar(): Psr\Http\Message\MessageInterface
43+
{
44+
$fecha_desde = '2015-12-31';
45+
$fecha_hasta = date('Y-m-d');
46+
$url = '/bhe/boletas?fecha_desde='.$fecha_desde.'&fecha_hasta='.$fecha_hasta; # Buscar algo similar a urlencode
47+
try {
48+
$response = self::$client->get($url);
49+
50+
} catch (ApiException $e) {
51+
throw new ApiException (sprintf('[ApiException %d] %s', $e->getCode(), $e->getMessage()));
52+
}
53+
return $response;
4254
}
4355

4456
public function test_boleta_email()
4557
{
58+
$response_body = $this->_buscar();
59+
$body_dec = json_decode($response_body->getBody()->getContents(), true);
60+
$numero_bhe = $body_dec['results'][0]['numero'];
61+
$url = '/bhe/email/'.$numero_bhe;
62+
4663
$destinatario = [
4764
'destinatario' => [
4865
'email' => self::$email
4966
]
5067
];
51-
$url = '/bhe/email/'.self::$numero_bhe;
5268

5369
try {
5470
$response = self::$client->post($url, $destinatario);

tests/bhe/BoletaEmitirTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function setUpBeforeClass(): void
3636
self::$verbose = env('TEST_VERBOSE', false);
3737
self::$client = new ApiClient();
3838
self::$emisor_rut = env('BHEXPRESS_EMISOR_RUT');
39-
self::$fecha_emis = env('TEST_EMITIR_FECHAEMIS', date('Y-m-d'));
39+
self::$fecha_emis = env('TEST_FECHAEMIS', date('Y-m-d'));
4040
}
4141

4242
public function test_boleta_emitir()

tests/bhe/BoletaListarTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function setUpBeforeClass(): void
3434
{
3535
self::$verbose = env('TEST_VERBOSE', false);
3636
self::$client = new ApiClient();
37-
self::$periodo = env('TEST_LISTAR_PERIODO', date('Ym'));
37+
self::$periodo = env('TEST_PERIODO', date('Ym'));
3838
}
3939

4040
public function test_boleta_listar()

tests/bhe/BoletaPdfTest.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,45 @@ class BoletaPdfTest extends TestCase
2929
protected static $verbose;
3030
protected static $client;
3131
protected static $emisor_rut;
32-
protected static $numero_bhe;
32+
protected static $periodo;
3333
protected static $archivo_pdf;
3434

3535
public static function setUpBeforeClass(): void
3636
{
3737
self::$verbose = env('TEST_VERBOSE', false);
3838
self::$client = new ApiClient();
3939
self::$emisor_rut = env('BHEXPRESS_EMISOR_RUT');
40-
self::$numero_bhe = env('TEST_PDF_NUMEROBHE', '0');
41-
self::$archivo_pdf = self::$emisor_rut . '_bhe_' . self::$numero_bhe . '.pdf';
40+
self::$periodo = env('TEST_PERIODO', date('Ym'));
41+
}
42+
43+
private function _buscar(): Psr\Http\Message\MessageInterface
44+
{
45+
$url = '/bhe/boletas?periodo='.self::$periodo; # Buscar algo similar a urlencode
46+
try {
47+
$response = self::$client->get($url);
48+
49+
} catch (ApiException $e) {
50+
throw new ApiException (sprintf('[ApiException %d] %s', $e->getCode(), $e->getMessage()));
51+
}
52+
return $response;
4253
}
4354

4455
public function test_boleta_pdf()
4556
{
46-
$url = '/bhe/pdf/'.self::$numero_bhe;
57+
$response_body = $this->_buscar();
58+
$body_dec = json_decode($response_body->getBody()->getContents(), true);
59+
$numero_bhe = $body_dec['results'][0]['numero'];
60+
$url = '/bhe/pdf/'.$numero_bhe;
61+
62+
$archivo_pdf = self::$emisor_rut . '_bhe_' . $numero_bhe . '.pdf';
4763

4864
try {
4965
$pdf = self::$client->get($url);
5066
$this->assertNotEmpty($pdf, 'El contenido del PDF no debe estar vacío');
5167

5268
// Intentar guardar el PDF en el sistema de archivos y verificar si el archivo existe
53-
file_put_contents(self::$archivo_pdf, $pdf->getBody());
54-
$this->assertFileExists(self::$archivo_pdf, 'El archivo PDF debe existir en el sistema de archivos');
69+
file_put_contents($archivo_pdf, $pdf->getBody());
70+
$this->assertFileExists($archivo_pdf, 'El archivo PDF debe existir en el sistema de archivos');
5571

5672
if (self::$verbose) {
5773
echo "\n",'test_boleta_pdf() pdf ',' Se ha generado exitosamente un PDF. ',"\n";

tests/test.env-dist

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
# Autenticación en API Gateway
1010
BHEXPRESS_API_URL="https://bhexpress.cl"
11-
BHEXPRESS_API_TOKEN=""
11+
BHEXPRESS_API_TOKEN=""# Token secreto de BHExpress.
1212

1313
# Emisor
14-
BHEXPRESS_EMISOR_RUT=""
14+
BHEXPRESS_EMISOR_RUT=""# RUT del emisor de BHExpress
1515

16-
# Receptor
17-
BHEXPRESS_RECEPTOR_EMAIL=""
16+
# Variables de prueba
17+
TEST_PERIODO=""# Periodo de listado. Formato AAAAMM. La variable se utiliza en múltiples pruebas, por lo que es casi obligatoria.
18+
TEST_FECHAEMIS=""# Fecha de emision de BHE. Formato aa-mm-dd. La variable se utiliza en BoletaEmitirTest
19+
TEST_NUMEROBHE=""# Identificador de tu BHE en BHExpress. La variable se utiliza en BoletaAnularTest.php
20+
TEST_CORREO=""# Correo destinatario del BHE. La variable se utiliza en BoletaEmitirTest.php

0 commit comments

Comments
 (0)