Skip to content

Commit 7d2aca2

Browse files
authored
Merge pull request #5 from Pabloferrandezroca/actualizarPlugin
Actualizar plugin
2 parents f11126d + e34721a commit 7d2aca2

File tree

6 files changed

+248
-7
lines changed

6 files changed

+248
-7
lines changed

.github/workflows/tests.yml

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
name: Tests del Plugin FacturaScripts
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
tests:
13+
name: Tests en ${{ matrix.database }} con PHP ${{ matrix.php-version }}
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
php-version: ['8.0', '8.1', '8.2', '8.3', '8.4']
20+
database: ['mysql', 'postgresql']
21+
22+
env:
23+
NOMBRE_PLUGIN: "SamplePlugin"
24+
25+
services:
26+
mysql:
27+
image: mariadb:11
28+
ports:
29+
- 3306:3306
30+
env:
31+
MARIADB_ROOT_PASSWORD: toor
32+
MARIADB_DATABASE: facturascripts_tests
33+
options: >-
34+
--health-cmd="mariadb-admin ping"
35+
--health-interval=10s
36+
--health-timeout=5s
37+
--health-retries=3
38+
39+
postgres:
40+
image: postgres:13
41+
ports:
42+
- 5432:5432
43+
env:
44+
POSTGRES_PASSWORD: toor
45+
POSTGRES_DB: facturascripts_tests
46+
options: >-
47+
--health-cmd=pg_isready
48+
--health-interval=10s
49+
--health-timeout=5s
50+
--health-retries=5
51+
52+
steps:
53+
- name: Instalar PHP y extensiones
54+
uses: shivammathur/setup-php@v2
55+
with:
56+
php-version: ${{ matrix.php-version }}
57+
extensions: json, fileinfo, simplexml, zip, dom, pdo, pdo_mysql, mysql, mysqli, pgsql, pdo_pgsql, bcmath, gd, curl, soap
58+
tools: composer
59+
coverage: none
60+
61+
- name: Clonar FacturaScripts
62+
uses: actions/checkout@v4
63+
with:
64+
fetch-depth: 0
65+
repository: 'NeoRazorX/facturascripts'
66+
67+
- name: Clonar Plugin ${{ env.NOMBRE_PLUGIN }}
68+
uses: actions/checkout@v4
69+
with:
70+
fetch-depth: 0
71+
path: Plugins/${{ env.NOMBRE_PLUGIN }}
72+
73+
- name: Cache de dependencias de Composer
74+
uses: actions/cache@v3
75+
with:
76+
path: ~/.composer/cache/files
77+
key: composer-${{ runner.os }}-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
78+
restore-keys: |
79+
composer-${{ runner.os }}-${{ matrix.php-version }}-
80+
composer-${{ runner.os }}-
81+
82+
- name: Instalar dependencias de FacturaScripts
83+
run: |
84+
mkdir -p MyFiles
85+
touch MyFiles/plugins.json
86+
composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader
87+
88+
- name: Crear archivo de configuración
89+
run: |
90+
cat > config.php << 'EOF'
91+
<?php
92+
93+
define('FS_COOKIES_EXPIRE', 604800);
94+
define('FS_LANG', 'es_ES');
95+
define('FS_TIMEZONE', 'Europe/Madrid');
96+
define('FS_ROUTE', '');
97+
98+
${{ matrix.database == 'mysql' && format('
99+
define(''FS_DB_TYPE'', ''mysql'');
100+
define(''FS_DB_HOST'', ''127.0.0.1'');
101+
define(''FS_DB_PORT'', ''3306'');
102+
define(''FS_DB_USER'', ''root'');
103+
') || format('
104+
define(''FS_DB_TYPE'', ''postgresql'');
105+
define(''FS_DB_HOST'', ''localhost'');
106+
define(''FS_DB_PORT'', ''5432'');
107+
define(''FS_DB_USER'', ''postgres'');
108+
') }}
109+
define('FS_DB_NAME', 'facturascripts_tests');
110+
define('FS_DB_PASS', 'toor');
111+
define('FS_DB_FOREIGN_KEYS', true);
112+
define('FS_DB_TYPE_CHECK', true);
113+
define('FS_MYSQL_CHARSET', 'utf8');
114+
define('FS_MYSQL_COLLATE', 'utf8_bin');
115+
116+
define('FS_HIDDEN_PLUGINS', '');
117+
define('FS_DEBUG', false);
118+
define('FS_DISABLE_ADD_PLUGINS', false);
119+
define('FS_DISABLE_RM_PLUGINS', false);
120+
define('FS_NF0', 2);
121+
EOF
122+
123+
- name: Copiar archivos de tests del Plugin
124+
run: |
125+
if [ -d "Plugins/${{ env.NOMBRE_PLUGIN }}/Test/main" ]; then
126+
cp -r Plugins/${{ env.NOMBRE_PLUGIN }}/Test/main Test/Plugins
127+
else
128+
echo "No se encontraron tests para el plugin"
129+
exit 1
130+
fi
131+
132+
- name: Instalar el Plugin ${{ env.NOMBRE_PLUGIN }}
133+
run: php Test/install-plugins.php
134+
135+
- name: Ejecutar tests en ${{ matrix.database }}
136+
run: vendor/bin/phpunit -c phpunit-plugins.xml --verbose
137+
138+
- name: Mostrar logs en caso de fallo
139+
if: failure()
140+
run: |
141+
echo "=== Logs de la base de datos ==="
142+
if [ "${{ matrix.database }}" = "mysql" ]; then
143+
docker logs $(docker ps -q --filter ancestor=mariadb:11) || true
144+
else
145+
docker logs $(docker ps -q --filter ancestor=postgres:13) || true
146+
fi

Init.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
*/
1919
namespace FacturaScripts\Plugins\SamplePlugin;
2020

21-
use FacturaScripts\Core\Base\InitClass;
21+
use FacturaScripts\Core\Template\InitClass;
22+
use FacturaScripts\Core\Tools;
23+
2224

2325
/**
2426
* Description of Init
@@ -28,14 +30,20 @@
2830
class Init extends InitClass
2931
{
3032

31-
public function init()
33+
public function init(): void
3234
{
3335
$this->loadExtension(new Extension\Controller\ListProducto());
3436
$this->loadExtension(new Extension\Model\Producto());
3537
}
3638

37-
public function update()
39+
public function update(): void
40+
{
41+
;
42+
}
43+
44+
public function uninstall(): void
3845
{
3946
;
4047
}
48+
4149
}

Model/DummyModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
namespace FacturaScripts\Plugins\SamplePlugin\Model;
33

4-
use FacturaScripts\Core\Model\Base\ModelClass;
5-
use FacturaScripts\Core\Model\Base\ModelTrait;
4+
use FacturaScripts\Core\Template\ModelClass;
5+
use FacturaScripts\Core\Template\ModelTrait;
66
use FacturaScripts\Core\Tools;
77

88
class DummyModel extends ModelClass
@@ -41,7 +41,7 @@ public static function tableName(): string {
4141
return 'dummies';
4242
}
4343

44-
public function clear() {
44+
public function clear(): void {
4545
parent::clear();
4646
// define here default values for cols
4747
$this->price = 0;

Test/main/DummyModelTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace FacturaScripts\Test\Plugins;
4+
5+
use FacturaScripts\Plugins\SamplePlugin\Model\DummyModel;
6+
use FacturaScripts\Test\Traits\LogErrorsTrait;
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class DummyModelTest extends TestCase
10+
{
11+
use LogErrorsTrait;
12+
13+
public function testPrimaryColumn(): void
14+
{
15+
$this->assertEquals('id', DummyModel::primaryColumn());
16+
}
17+
18+
public function testTableName(): void
19+
{
20+
$this->assertEquals('dummies', DummyModel::tableName());
21+
}
22+
23+
public function testClear(): void
24+
{
25+
$model = new DummyModel();
26+
$model->clear();
27+
28+
$this->assertEquals(0, $model->price);
29+
$this->assertEquals("new", $model->dummy_type);
30+
$this->assertGreaterThanOrEqual(0, $model->random_number);
31+
$this->assertEquals("middle", $model->resell_value);
32+
$this->assertEquals(0, $model->dummy_rating);
33+
$this->assertNotEmpty($model->programmed_date);
34+
$this->assertNotEmpty($model->programmed_hour);
35+
}
36+
37+
public function testSaveInsert(): void
38+
{
39+
$model = new DummyModel();
40+
$model->clear();
41+
$insert = $model->saveInsert();
42+
43+
$this->assertNotEmpty($model->creation_date);
44+
$this->assertNotEmpty($model->modification_date);
45+
$this->assertTrue($insert);
46+
$model->delete();
47+
}
48+
49+
public function testSaveInsertNoInsert(): void
50+
{
51+
$model = new DummyModel();
52+
$model->clear();
53+
$model->id = 999;
54+
$model->saveInsert();
55+
56+
$model2 = new DummyModel();
57+
$model2->clear();
58+
$model2->id = 999;
59+
$insert = $model2->saveInsert();
60+
61+
//no se debe insertar porque ese id ya existe
62+
$this->assertFalse($insert);
63+
$model->delete();
64+
}
65+
66+
public function testSaveUpdate(): void
67+
{
68+
$model = new DummyModel();
69+
$model->clear();
70+
$model->saveInsert();
71+
$fechaM = $model->modification_date;
72+
73+
//esperamos 2 segundos para que la fecha de modificacion sea diferente
74+
sleep(2);
75+
$update = $model->saveUpdate();
76+
77+
$this->assertTrue($update);
78+
$this->assertNotEquals($fechaM, $model->modification_date);
79+
$model->delete();
80+
}
81+
82+
protected function tearDown(): void
83+
{
84+
$this->logErrors();
85+
}
86+
}

Test/main/install-plugins.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SamplePlugin

facturascripts.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
description = 'Plugin de ejemplo para FacturaScripts.'
2-
min_version = 2024.94
2+
min_version = 2025
33
name = 'SamplePlugin'
44
version = 0.5

0 commit comments

Comments
 (0)