-
Notifications
You must be signed in to change notification settings - Fork 320
Feat/rerun tests #3102
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
Open
kwizer15
wants to merge
5
commits into
jeedom:alpha
Choose a base branch
from
kwizer15:feat/rerun-tests
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,469
−334
Open
Feat/rerun tests #3102
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e39e04
Re-run tests
kwizer15 3a6515d
Install phpunit with composer
kwizer15 f04833c
Add optionnal root_user configuration for database
kwizer15 7b08a34
Re-add pluginTest and mark as skipped
kwizer15 e818c78
Complete documentation
kwizer15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| name: Tests | ||
|
|
||
| on: | ||
| push: | ||
| pull_request: | ||
| branches: | ||
| - 'master' | ||
| - 'beta' | ||
| - 'alpha' | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| phpunit: | ||
| name: PHP Unit | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| php-version: ['7.4', '8.2'] | ||
| services: | ||
| mariadb: | ||
| image: mariadb:10.6 | ||
| env: | ||
| MYSQL_ROOT_PASSWORD: root | ||
| ports: | ||
| - 3306:3306 | ||
| options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Setup PHP | ||
| uses: shivammathur/setup-php@v2 | ||
| with: | ||
| php-version: '${{ matrix.php-version }}' | ||
| extensions: json pdo_mysql curl gd imap xml opcache soap xml zip ssh2 mbstring ldap yaml snmp pcov | ||
| coverage: none | ||
|
|
||
| - name: Install dependencies | ||
| run: composer install --prefer-dist --no-progress --optimize-autoloader | ||
|
|
||
| # Download PHPUnit | ||
| - name: Download PHPUnit | ||
| run: wget -O phpunit.phar https://phar.phpunit.de/phpunit-9.phar && chmod +x phpunit.phar | ||
|
|
||
| # Setup cache directory | ||
| - name: Setup cache directory | ||
| run: | | ||
| mkdir -p /tmp/jeedom/cache | ||
| chmod 774 /tmp/jeedom | ||
| chmod 774 /tmp/jeedom/cache | ||
| # Initialize configuration | ||
| - name: Initialize configuration | ||
| run: | | ||
| cp core/config/common.config.sample.php core/config/common.config.php | ||
| sed -i 's/#HOST#/127.0.0.1/g' core/config/common.config.php | ||
| sed -i 's/#PORT#/3306/g' core/config/common.config.php | ||
| sed -i 's/#DBNAME#/jeedom/g' core/config/common.config.php | ||
| sed -i 's/#USERNAME#/root/g' core/config/common.config.php | ||
| sed -i 's/#PASSWORD#/root/g' core/config/common.config.php | ||
| # Run Legacy test suite | ||
| - name: Run legacy test suite | ||
| env: | ||
| DATABASE_DSN: mysql://root:root@localhost:3306/jeedom_test | ||
| run: ./phpunit.phar --coverage-text --colors=never --testsuite "Legacy tests" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,3 +47,6 @@ tmp/* | |
| .env | ||
| .phpstan.cache | ||
| phpstan.phar | ||
|
|
||
| phpunit.phar | ||
| .phpunit.result.cache | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| # Tests unitaires avec PHPUnit | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Pré-requis | ||
|
|
||
| - PHP 7.4 ou supérieur | ||
| - MySQL/MariaDB configuré et fonctionnel | ||
| - Composer installé | ||
|
|
||
| ### 1. Télécharger PHPUnit | ||
kwizer15 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```bash | ||
| wget -O phpunit.phar https://phar.phpunit.de/phpunit-9.phar | ||
| chmod +x phpunit.phar | ||
| ``` | ||
|
|
||
| ### 2. Mettre à jour les dépendances | ||
|
|
||
| ```bash | ||
| composer update --ignore-platform-reqs | ||
| ``` | ||
|
|
||
| ### 3. Configuration | ||
|
|
||
| Copier le fichier de configuration d'exemple et l'adapter : | ||
|
|
||
| ```bash | ||
| cp core/config/common.config.sample.php core/config/common.config.php | ||
| ``` | ||
|
|
||
| Modifier le fichier `core/config/common.config.php` avec vos paramètres de base de données : | ||
|
|
||
| ```php | ||
| <?php | ||
| define('DEBUG', 1); | ||
|
|
||
| global $CONFIG; | ||
| $CONFIG = [ | ||
| 'db' => [ | ||
| 'host' => '127.0.0.1', | ||
| 'port' => '3306', | ||
| 'dbname' => 'jeedom', // Le bootstrap ajoutera '_test' | ||
| 'username' => 'root', | ||
| 'password' => 'root', | ||
| ], | ||
| ]; | ||
| ``` | ||
|
|
||
| ### 4. Configuration du cache (optionnel) | ||
|
|
||
| Pour éviter les problèmes de permissions avec le FileCache, créer le répertoire de cache : | ||
|
|
||
| ```bash | ||
| sudo mkdir -p /tmp/jeedom/cache | ||
| sudo chmod 774 /tmp/jeedom | ||
| sudo chmod 774 /tmp/jeedom/cache | ||
| sudo chown -R $USER:$USER /tmp/jeedom | ||
| ``` | ||
|
|
||
| **Note :** Le fichier `tests/bootstrap.php` se charge automatiquement de : | ||
| - Ajouter le suffixe `_test` au nom de la base de données | ||
| - Créer/recréer la base de données de test | ||
| - Initialiser le schéma de base de données | ||
| - Créer un utilisateur admin par défaut | ||
|
|
||
| ## Utilisation | ||
|
|
||
| ### Exécuter tous les tests | ||
|
|
||
| ```bash | ||
| # Exécuter tous les tests | ||
| ./phpunit.phar | ||
|
|
||
| # Ou avec plus de détails | ||
| ./phpunit.phar --coverage-text --colors=never | ||
| ``` | ||
|
|
||
| ### Exécuter une suite de tests spécifique | ||
|
|
||
| ```bash | ||
| # Exécuter seulement les tests legacy | ||
| ./phpunit.phar --testsuite "Legacy tests" | ||
| ``` | ||
|
|
||
| ### Exécuter un test spécifique | ||
|
|
||
| ```bash | ||
| # Exécuter un fichier de test particulier | ||
| ./phpunit.phar tests/configTest.php | ||
|
|
||
| # Exécuter une méthode de test spécifique | ||
| ./phpunit.phar tests/configTest.php::testMethod | ||
| ``` | ||
|
|
||
| ### Exécuter les tests d'un dossier | ||
|
|
||
| ```bash | ||
| # Tests des classes | ||
| ./phpunit.phar tests/class/ | ||
|
|
||
| # Tests des utilitaires PHP | ||
| ./phpunit.phar tests/php/ | ||
| ``` | ||
|
|
||
| ## Structure des tests | ||
|
|
||
| Les tests sont organisés comme suit : | ||
|
|
||
| ``` | ||
| tests/ | ||
| ├── bootstrap.php # Configuration d'amorçage des tests | ||
| ├── cacheTest.php # Tests du système de cache | ||
| ├── configTest.php # Tests de configuration | ||
| ├── cronTest.php # Tests des tâches cron | ||
| ├── logTest.php # Tests des logs | ||
| ├── pluginTest.php # Tests des plugins | ||
| ├── scenarioExpressionTest.php # Tests des expressions de scénarios | ||
| ├── userTest.php # Tests des utilisateurs | ||
| ├── class/ # Tests des classes principales | ||
| │ ├── ajaxTest.php | ||
| │ └── scenarioTest.php | ||
| ├── com/ # Tests des communications | ||
| │ └── shellTest.php | ||
| └── php/ # Tests des utilitaires PHP | ||
| └── utilsTest.php | ||
| ``` | ||
|
|
||
| ## Configuration PHPUnit | ||
|
|
||
| Le fichier `phpunit.xml.dist` contient la configuration des tests : | ||
|
|
||
| ```xml | ||
| <phpunit bootstrap="./tests/bootstrap.php" colors="true"> | ||
| <testsuites> | ||
| <testsuite name="Legacy tests"> | ||
| <directory suffix="Test.php">./tests</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| <coverage processUncoveredFiles="true"> | ||
| <include> | ||
| <directory suffix=".php">core/class</directory> | ||
| </include> | ||
| </coverage> | ||
| </phpunit> | ||
| ``` | ||
|
|
||
| ## Conseils de développement | ||
|
|
||
| ### Créer un nouveau test | ||
|
|
||
| 1. Créer un fichier `*Test.php` dans le dossier approprié | ||
| 2. Étendre la classe `PHPUnit\Framework\TestCase` | ||
| 3. Nommer les méthodes de test avec le préfixe `test` | ||
|
|
||
| Exemple : | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class MonNouveauTest extends TestCase | ||
| { | ||
| public function testMonTest() | ||
| { | ||
| $this->assertTrue(true); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Debugging | ||
|
|
||
| Pour déboguer vos tests : | ||
|
|
||
| ```bash | ||
| # Afficher plus de détails | ||
| ./phpunit.phar --verbose | ||
|
|
||
| # Arrêter au premier échec | ||
| ./phpunit.phar --stop-on-failure | ||
| ``` | ||
|
|
||
| ### Couverture de code | ||
|
|
||
| Pour générer un rapport de couverture de code : | ||
|
|
||
| ```bash | ||
| # Rapport texte | ||
| ./phpunit.phar --coverage-text | ||
|
|
||
| # Rapport HTML (nécessite l'extension xdebug ou pcov) | ||
| ./phpunit.phar --coverage-html coverage/ | ||
| ``` | ||
|
|
||
| ## GitHub Actions | ||
|
|
||
| Les tests sont automatiquement exécutés via GitHub Actions sur : | ||
| - Push sur toutes les branches | ||
| - Pull requests vers `master`, `beta`, et `alpha` | ||
| - Matrice de tests : PHP 7.4 et 8.2 | ||
|
|
||
| Le workflow télécharge automatiquement PHPUnit, initialise la configuration et configure les répertoires de cache nécessaires. | ||
|
|
||
| ## Informations techniques | ||
|
|
||
| ### Bootstrap des tests | ||
|
|
||
| Le fichier `tests/bootstrap.php` effectue automatiquement : | ||
|
|
||
| 1. **Configuration de la base de données** : | ||
| - Ajout du suffixe `_test` au nom de la base | ||
| - Suppression/recréation de la base de test | ||
| - Attribution des privilèges | ||
|
|
||
| 2. **Initialisation du schéma** : | ||
| - Exécution du script `install/database.php` | ||
| - Création des tables nécessaires | ||
|
|
||
| 3. **Utilisateur par défaut** : | ||
| - Création d'un utilisateur admin (login: `admin`, mot de passe: `admin`) | ||
|
|
||
| 4. **Configuration système** : | ||
| - Génération d'une clé API | ||
| - Chargement des classes core | ||
|
|
||
| ### Système de cache | ||
|
|
||
| Jeedom utilise un système de cache FileCache qui stocke les données dans `/tmp/jeedom/cache`. Si vous rencontrez des erreurs liées au cache, assurez-vous que : | ||
|
|
||
| - Le répertoire `/tmp/jeedom/cache` existe | ||
| - L'utilisateur a les permissions d'écriture (774 recommandé) | ||
| - Le répertoire parent `/tmp/jeedom` est accessible | ||
|
|
||
| ### Résolution des problèmes courants | ||
|
|
||
| **Erreur de connexion à la base de données :** | ||
| - Vérifiez que MySQL/MariaDB est démarré | ||
| - Vérifiez les paramètres dans `core/config/common.config.php` | ||
kwizer15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| **Erreur de cache :** | ||
| - Créez le répertoire de cache avec les bonnes permissions (voir section 4) | ||
|
|
||
| **Tests qui échouent :** | ||
| - Exécutez avec `--verbose` pour plus de détails | ||
| - Vérifiez les logs dans le répertoire `log/` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| <phpunit bootstrap="./tests/bootstrap.php" colors="true"> | ||
| <testsuite> | ||
| <directory suffix="Test.php">./tests</directory> | ||
| </testsuite> | ||
| <phpunit | ||
| bootstrap="./tests/bootstrap.php" | ||
| colors="true" | ||
| > | ||
| <testsuites> | ||
| <testsuite name="Legacy tests"> | ||
| <directory suffix="Test.php">./tests</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| <coverage processUncoveredFiles="true"> | ||
| <include> | ||
| <directory suffix=".php">core/class</directory> | ||
| </include> | ||
| </coverage> | ||
| </phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,36 @@ | ||
| <?php | ||
| require_once __DIR__ . "/../core/php/core.inc.php"; | ||
| echo "\nCommence le test Jeedom\n"; | ||
| ?> | ||
|
|
||
| require_once dirname(__DIR__) . '/core/config/common.config.php'; | ||
| global $CONFIG; | ||
|
|
||
| $CONFIG['db']['dbname'] .= '_test'; | ||
|
|
||
| $connection = new PDO(sprintf('mysql:host=%s;port=%u;charset=utf8', $CONFIG['db']['host'], $CONFIG['db']['port']), $CONFIG['db']['username'], $CONFIG['db']['password']); | ||
| $connection->query('DROP DATABASE IF EXISTS ' . $CONFIG['db']['dbname']); | ||
| $connection->query('CREATE DATABASE '.$CONFIG['db']['dbname']); | ||
| $connection->query('GRANT ALL PRIVILEGES ON '.$CONFIG['db']['dbname'].'.* TO "'.$CONFIG['db']['username'].'"@"%" IDENTIFIED BY "'.$CONFIG['db']['password'].'"'); | ||
| $connection->query('FLUSH PRIVILEGES'); | ||
|
|
||
| ob_start(); | ||
| require_once dirname(__DIR__).'/install/database.php'; | ||
|
|
||
| require_once dirname(__DIR__).'/core/class/config.class.php'; | ||
| config::save('api', config::genKey()); | ||
|
|
||
| $user = new user(); | ||
| $user->setLogin('admin'); | ||
| $user->setPassword(sha512('admin')); | ||
| $user->setProfils('admin'); | ||
| $user->save(); | ||
|
|
||
| ob_end_clean(); | ||
|
|
||
| require_once dirname(__DIR__).'/core/php/core.inc.php'; | ||
|
|
||
| function dd(...$vars) | ||
kwizer15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| foreach ($vars as $var) { | ||
| var_dump($var); | ||
| } | ||
| exit; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.