Skip to content

Tests unitaires du plugin vs la suite de test complète de SPIP

On a donc depuis (très) longtemps une suite de test de SPIP https://git.spip.net/spip/tests qui fonctionne maintenant avec phpunit et prends en charge les problématique de "charger SPIP avant les tests unitaires" et "jouer les tests unitaires de chaque plugin" ceux-ci étant aussi basés sur phpunit

Exemple de Textwheel https://git.spip.net/spip/textwheel/src/branch/master/tests/TextwheelPropreTest.php

Dans le cas des tests unitaires de archiviste, ils ont été écrits pour être lancés depuis le plugin et tourner de manière autonome (de fait, ces tests sont indépendants du reste de SPIP et peuvent s'en passer).

Mais du coup quand on lance tous les tests depuis tests/ les tests de archiviste plantent parce qu'il lui manque des classes

 ~/Sites/localhost/fraich40/tests/ [master*] vendor/bin/phpunit ../plugins-dist/archiviste/tests                                                                                                                           (master|✚2…1⚑1)
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

EESSS

Time: 00:00.010, Memory: 10.00 MB

There were 2 errors:

1) Spip\Archiver\Tests\AbstractArchiverTest::testConstructor
PHPUnit\Framework\MockObject\UnknownClassException: Class "Spip\Archiver\AbstractArchiver" does not exist

/Users/cedric/Sites/localhost/fraich40/plugins-dist/archiviste/tests/AbstractArchiverTest.php:20
/Users/cedric/Sites/localhost/fraich40/tests/vendor/phpunit/phpunit/phpunit:92

2) Spip\Archiver\Tests\SpipArchiverTest::testInformer
Error: Class 'Spip\Archiver\TarArchive' not found in /Users/cedric/Sites/localhost/fraich40/plugins-dist/archiviste/tests/SpipArchiverTest.php:34
Stack trace:
#0 /Users/cedric/Sites/localhost/fraich40/tests/vendor/phpunit/phpunit/src/Framework/TestSuite.php(622): Spip\Archiver\Tests\SpipArchiverTest::setUpBeforeClass()
#1 /Users/cedric/Sites/localhost/fraich40/tests/vendor/phpunit/phpunit/src/Framework/TestSuite.php(678): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#2 /Users/cedric/Sites/localhost/fraich40/tests/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(670): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#3 /Users/cedric/Sites/localhost/fraich40/tests/vendor/phpunit/phpunit/src/TextUI/Command.php(143): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\Framework\TestSuite), Array, Array, true)
#4 /Users/cedric/Sites/localhost/fraich40/tests/vendor/phpunit/phpunit/src/TextUI/Command.php(96): PHPUnit\TextUI\Command->run(Array, true)
#5 /Users/cedric/Sites/localhost/fraich40/tests/vendor/phpunit/phpunit/phpunit(92): PHPUnit\TextUI\Command::main()
#6 {main}
ERRORS!
Tests: 20, Assertions: 0, Errors: 2, Skipped: 3.

De fait on appel pas l'autoloader généré par composer dans le plugin archiviste.

On peut patcher facilement avec

~/Sites/localhost/fraich40/plugins-dist/archiviste/ [master*] git diff tests/                                                                                                                                                 (master|✚3)
diff --git a/tests/AbstractArchiverTest.php b/tests/AbstractArchiverTest.php
index 541f61d..0645aeb 100644
--- a/tests/AbstractArchiverTest.php
+++ b/tests/AbstractArchiverTest.php
@@ -5,6 +5,8 @@ namespace Spip\Archiver\Tests;
 use PHPUnit\Framework\TestCase;
 use Spip\Archiver\AbstractArchiver;

+include_spip('inc/archives');
+
 /**
  * @covers \Spip\Archiver\AbstractArchiver
  *
diff --git a/tests/SpipArchiverTest.php b/tests/SpipArchiverTest.php
index e27e496..5da01e2 100644
--- a/tests/SpipArchiverTest.php
+++ b/tests/SpipArchiverTest.php
@@ -5,6 +5,8 @@ namespace Spip\Archiver\Tests;
 use PHPUnit\Framework\TestCase;
 use Spip\Archiver\SpipArchiver;

+include_spip('inc/archives');
+
 /**
  * @covers \Spip\Archiver\AbstractArchiver
  * @covers \Spip\Archiver\SpipArchiver

Mais alors les tests unitaires ne s'executent plus quand on les lance depuis le plugin car SPIP n'est pas chargé et include_spip n'est pas connue.