Skip to content

Commit 202222f

Browse files
Merge pull request #413 from JLG-WOCFR-DEV/codex/add-phpunit-configuration-and-tests-setup
Add PHPUnit configuration and expand notification failure tests
2 parents 9992743 + ac7dd35 commit 202222f

File tree

6 files changed

+93
-2
lines changed

6 files changed

+93
-2
lines changed

.github/workflows/frontend-tests.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v4
13+
- name: Setup PHP
14+
uses: shivammathur/setup-php@v2
15+
with:
16+
php-version: '8.1'
17+
coverage: none
18+
- name: Install Composer dependencies
19+
run: composer install --no-ansi --no-interaction --no-progress
20+
- name: Run PHP unit tests
21+
run: composer test:php
1322
- uses: actions/setup-node@v4
1423
with:
1524
node-version: '20'

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
.playwright/
55
playwright-report/
66
test-results/
7+
build/
8+
phpunit.junit.xml
9+
phpunit.result.cache

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ Liens Morts Detector est une extension WordPress qui détecte les liens et image
5656

5757
## Tests automatisés
5858

59+
### PHP (PHPUnit)
60+
- Installer les dépendances PHP : `composer install`.
61+
- Lancer la suite unitaire : `composer test:php` (génère également un rapport JUnit `phpunit.junit.xml`).
62+
- ✅ Cette suite est exécutée dans la CI et doit réussir avant toute fusion.
63+
5964
### JavaScript (Jest)
6065
- `npm test` exécute l’intégralité de la suite Jest existante.
6166

@@ -81,7 +86,8 @@ Liens Morts Detector est une extension WordPress qui détecte les liens et image
8186
La charte d’interface détaille les styles, couleurs et attentes d’interaction à respecter : voir [`docs/charte-ux.md`](docs/charte-ux.md).
8287

8388
### Combinaison des suites
84-
- `npm run test:all` exécute successivement Jest puis Playwright.
89+
- `composer test:all` enchaîne les tests PHP, Jest puis Playwright.
90+
- `npm run test:all` reste disponible si seuls les tests JavaScript sont nécessaires.
8591

8692
## Détection des soft 404
8793

composer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@
99
}
1010
},
1111
"scripts": {
12-
"test:js": "npm test"
12+
"test:php": [
13+
"@php -r \"if (!is_dir('build')) { mkdir('build'); }\"",
14+
"@php -r \"if (!is_dir('build/logs')) { mkdir('build/logs'); }\"",
15+
"vendor/bin/phpunit"
16+
],
17+
"test:js": "npm test",
18+
"test:all": [
19+
"@test:php",
20+
"@test:js",
21+
"npm run test:e2e"
22+
]
1323
},
1424
"autoload": {
1525
"psr-4": {

phpunit.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
cacheResult="false"
8+
>
9+
<testsuites>
10+
<testsuite name="Liens Morts Detector Test Suite">
11+
<directory suffix="Test.php">tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
<logging>
15+
<junit outputFile="phpunit.junit.xml"/>
16+
</logging>
17+
</phpunit>

tests/NotificationManagerTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,52 @@ public function test_send_summary_notifications_records_history_and_throttles():
202202
$this->assertSame('throttled', $history[3]['status']);
203203
}
204204

205+
public function test_send_summary_notifications_records_failed_email_when_wp_mail_returns_false(): void
206+
{
207+
$testCase = $this;
208+
209+
Functions\when('wp_mail')->alias(static function ($recipients, $subject, $message) use ($testCase) {
210+
$testCase->sentEmails[] = array(
211+
'recipients' => $recipients,
212+
'subject' => $subject,
213+
'message' => $message,
214+
);
215+
216+
return false;
217+
});
218+
219+
$manager = new NotificationManager(static function () {
220+
return 1500;
221+
});
222+
223+
$summary = array(
224+
'subject' => 'Résumé',
225+
'message' => 'Contenu',
226+
'dataset_label' => 'Analyse des liens',
227+
);
228+
229+
$recipients = array('admin@example.test');
230+
$settings = array('url' => 'https://hooks.example.test', 'channel' => 'slack');
231+
232+
$result = $manager->sendSummaryNotifications('link', $summary, $recipients, array(
233+
'context' => 'scan',
234+
'webhook_settings' => $settings,
235+
));
236+
237+
$failureMessage = 'Échec de l’envoi de l’e-mail pour l’analyse Analyse des liens.';
238+
239+
$this->assertSame('failed', $result['email']['status']);
240+
$this->assertSame($failureMessage, $result['email']['error']);
241+
$this->assertCount(1, $this->errorLogs);
242+
$this->assertStringContainsString('Failed to send link summary email', $this->errorLogs[0]);
243+
244+
$history = $manager->getHistoryEntries();
245+
$this->assertCount(2, $history);
246+
$this->assertSame('failed', $history[0]['status']);
247+
$this->assertSame('email', $history[0]['channel']);
248+
$this->assertSame($failureMessage, $history[0]['error']);
249+
}
250+
205251
public function test_send_webhook_only_returns_wp_error_on_failure_and_logs_history(): void
206252
{
207253
$this->throttleWindow = 0;

0 commit comments

Comments
 (0)