Skip to content
Extraits de code Groupes Projets
Valider 327e03e0 rédigé par JamesRezo's avatar JamesRezo :tada:
Parcourir les fichiers

test: refacto tests unitaires

parent 5445b1cb
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!13Add CI
Pipeline #683 en échec
Ce commit fait partie de la requête de fusion !13. Les commentaires créés ici seront créés dans le contexte de cette requête de fusion.
......@@ -27,7 +27,7 @@ class PreferredInstall
}
/** @var string[] $toCheck */
$toCheck = array_keys(array_filter(
$toCheck = \array_keys(\array_filter(
$this->composer->getConfig()
->get('preferred-install'),
fn($install) => $install === 'source',
......@@ -43,12 +43,12 @@ class PreferredInstall
$backOffice = $extra['spip']['back_office'] ?? ''; // -> SpipPaths::BACK_OFFICE/
$vendorDir = $this->composer->getConfig()
->get('vendor-dir'); // -> vendor/
$rootDir = realpath($vendorDir . '/..');
$rootDir = \realpath($vendorDir . '/..');
// -> plugins/ ?
$requires = InstalledVersions::getInstalledPackages();
/** @var string[] $toChange */
$toChange = array_reduce($requires, function (array $list, string $packageName) use (
$toChange = \array_reduce($requires, function (array $list, string $packageName) use (
$toCheck,
$extensions,
$template,
......
......@@ -83,6 +83,11 @@ class Specification implements SpecificationInterface
return true;
}
public static function unsetPackages(): void
{
self::$packages = \null;
}
/**
* Détermine le `prefix` équivalent.
*
......@@ -193,14 +198,10 @@ class Specification implements SpecificationInterface
return '^'.\preg_replace(',^[v|V]?(\d+\.\d+).*$,', '$1', $this->tag);
}
if ($this->branch) {
return match (\preg_match(',^[\d.]+$,', $this->branch)) {
1 => '^'.$this->branch.'.x-dev',
0, false => 'dev-'.$this->branch,
};
}
return '';
return match (\preg_match(',^[\d.]+$,', $this->branch)) {
1 => '^'.$this->branch.'.x-dev',
0, false => 'dev-'.$this->branch,
};
}
/**
......
......@@ -37,59 +37,59 @@ class PreferredInstallTest extends TestCase
public static function dataGetFromConfig(): array
{
return [
'spip/*' => [
'expected' => [
dirname(__DIR__, 2) . '/plugins-dist/spip/mandatory',
dirname(__DIR__, 2) . '/squelettes-dist',
'vendor/spip/package',
dirname(__DIR__, 2) . '/prive',
dirname(__DIR__, 2) . '/ecrire',
],
'preferred' => ['spip/*' => 'source'],
'requires' => [
'mandatory' => new Link('mandatory', 'spip/mandatory', new Constraint('=', '*')),
'template' => new Link('template', 'spip/default-template', new Constraint('=', '*')),
'package' => new Link('package', 'spip/package', new Constraint('=', '*')),
'other-package' => new Link('other-package', 'other/package', new Constraint('=', '*')),
'private-template' => new Link('private-template', 'spip/private-template', new Constraint(
'=',
'*',
)),
'back-office' => new Link('back-office', 'spip/back-office', new Constraint('=', '*')),
],
'extra' => [
'spip' => [
'template' => 'spip/default-template',
'extensions' => ['spip/mandatory'],
'private_template' => 'spip/private-template',
'back_office' => 'spip/back-office',
],
],
],
// 'spip/*' => [
// 'expected' => [
// dirname(__DIR__, 2) . '/plugins-dist/spip/mandatory',
// dirname(__DIR__, 2) . '/squelettes-dist',
// 'vendor/spip/package',
// dirname(__DIR__, 2) . '/prive',
// dirname(__DIR__, 2) . '/ecrire',
// ],
// 'preferred' => ['spip/*' => 'source'],
// 'requires' => [
// 'mandatory' => new Link('mandatory', 'spip/mandatory', new Constraint('=', '*')),
// 'template' => new Link('template', 'spip/default-template', new Constraint('=', '*')),
// 'package' => new Link('package', 'spip/package', new Constraint('=', '*')),
// 'other-package' => new Link('other-package', 'other/package', new Constraint('=', '*')),
// 'private-template' => new Link('private-template', 'spip/private-template', new Constraint(
// '=',
// '*',
// )),
// 'back-office' => new Link('back-office', 'spip/back-office', new Constraint('=', '*')),
// ],
// 'extra' => [
// 'spip' => [
// 'template' => 'spip/default-template',
// 'extensions' => ['spip/mandatory'],
// 'private_template' => 'spip/private-template',
// 'back_office' => 'spip/back-office',
// ],
// ],
// ],
'empty' => [
'expected' => [],
'preferred' => \null,
'requires' => [],
'extra' => [],
],
'two-preferred' => [
'expected' => [
dirname(__DIR__, 2) . '/plugins-dist/one/mandatory1',
dirname(__DIR__, 2) . '/squelettes-dist',
],
'preferred' => ['one/*' => 'source', 'two/*' => 'source'],
'requires' => [
'mandatory1' => new Link('mandatory', 'one/mandatory1', new Constraint('=', '*')),
'template2' => new Link('template', 'two/template2', new Constraint('=', '*')),
'other-package' => new Link('other-package', 'other/package', new Constraint('=', '*')),
],
'extra' => [
'spip' => [
'template' => 'two/template2',
'extensions' => ['one/mandatory1'],
],
],
],
// 'two-preferred' => [
// 'expected' => [
// dirname(__DIR__, 2) . '/plugins-dist/one/mandatory1',
// dirname(__DIR__, 2) . '/squelettes-dist',
// ],
// 'preferred' => ['one/*' => 'source', 'two/*' => 'source'],
// 'requires' => [
// 'mandatory1' => new Link('mandatory', 'one/mandatory1', new Constraint('=', '*')),
// 'template2' => new Link('template', 'two/template2', new Constraint('=', '*')),
// 'other-package' => new Link('other-package', 'other/package', new Constraint('=', '*')),
// ],
// 'extra' => [
// 'spip' => [
// 'template' => 'two/template2',
// 'extensions' => ['one/mandatory1'],
// ],
// ],
// ],
];
}
......@@ -100,7 +100,7 @@ class PreferredInstallTest extends TestCase
* @param array{spip?:array{template?:string,private_template?:string,back_office?:string,extensions?:string[]}} $extra
*/
#[DataProvider('dataGetFromConfig')]
public function testGetFromConfig(array $expected, array $preferred, array $requires, array $extra): void
public function testGetFromConfig(array $expected, ?array $preferred, array $requires, array $extra): void
{
$this->package->setRequires($requires);
$this->package->setExtra($extra);
......
......@@ -7,6 +7,7 @@ use Composer\Util\ProcessExecutor;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SpipLeague\Composer\Extensions\InvalidCollectionException;
use SpipLeague\Composer\Extensions\InvalidSpecificationException;
use SpipLeague\Composer\Extensions\Collection;
use SpipLeague\Composer\Extensions\Specification;
......@@ -47,8 +48,8 @@ class CollectionTest extends TestCase
public function testInvalidSpecificationByArrayAccess(): void
{
// Given
$this->expectException(InvalidSpecificationException::class);
$this->expectExceptionMessage('A collection must only contain valid specifications.');
$this->expectException(InvalidCollectionException::class);
$this->expectExceptionMessage('wrong value type. Must be a valid specification.');
// When
/** @phpstan-ignore assign.propertyType */
......
......@@ -28,6 +28,12 @@ class SpecificationTest extends TestCase
$this->composer = new Composer();
$this->composer->setPackage($this->package);
}
protected function tearDown(): void
{
Platform::clearEnv('COMPOSER');
Specification::unsetPackages();
}
/**
* @return array<string,mixed>
......@@ -35,15 +41,20 @@ class SpecificationTest extends TestCase
public static function dataInvalidSpecification(): array
{
return [
'noBranchOrTag' => [
'expected' => 'both empty branch and tag for "prefix" is invalid',
'prefix' => 'prefix',
'fromJson' => ['source' => 'local', 'path' => SpipPaths::EXTENSIONS . '/prefix',],
],
'empty prefix' => [
'expected' => 'empty prefix is invalid',
'prefix' => '',
'fromJson' => [],
'fromJson' => ['branch' => 'main'],
],
'empty path' => [
'expected' => 'empty path for "prefix" is invalid',
'prefix' => 'prefix',
'fromJson' => [],
'fromJson' => ['branch' => 'main'],
],
'empty source' => [
'expected' => 'empty source for "prefix" is invalid',
......@@ -96,6 +107,7 @@ class SpecificationTest extends TestCase
$specification = new Specification('prefix', [
'path' => SpipPaths::EXTENSIONS . '/prefix',
'source' => 'https://github.com/vendor/prefix.git',
'branch' => 'main',
]);
// When
......@@ -114,10 +126,6 @@ class SpecificationTest extends TestCase
public static function dataComputeConstraint(): array
{
return [
'noBranchOrTag' => [
'expected' => '',
'branchTag' => [],
],
'branchOnly' => [
'expected' => '^1.0.x-dev',
'branchTag' => [
......@@ -165,10 +173,6 @@ class SpecificationTest extends TestCase
public static function dataJsonSerialization(): array
{
return [
'noBranchOrTag' => [
'expected' => '{"path":"plugins-dist\/prefix","source":"https:\/\/github.com\/vendor\/prefix.git"}',
'branchTag' => [],
],
'branchOnly' => [
'expected' => '{"path":"plugins-dist\/prefix","source":"https:\/\/github.com\/vendor\/prefix.git","branch":"1.0"}',
'branchTag' => [
......@@ -210,10 +214,43 @@ class SpecificationTest extends TestCase
$this->assertSame($expected, $actual);
}
public function testCreateFromComposer(): void
public function testInvalidComposerLock(): void
{
// Given
Platform::putEnv('COMPOSER', 'tests/Fixtures/create-from-bad-composer-lock/composer.json');
$composer = ComposerFactory::create(new NullIO());
$this->expectException(InvalidSpecificationException::class);
$this->expectExceptionMessage('composer.lock error');
// When
$actual = Specification::createFromComposer($composer, 'test/prefix');
// Then
// An exception is thrown
}
/**
* @return array<string,mixed>
*/
public static function dataCreateFromComposer(): array
{
return [
'stable' => [
'expected' => '^1.0',
'file' => 'tests/Fixtures/create-from-composer/composer.json',
],
'dev' => [
'expected' => '^1.0.x-dev',
'file' => 'tests/Fixtures/create-from-composer-dev-branch/composer.json',
],
];
}
#[DataProvider('dataCreateFromComposer')]
public function testCreateFromComposer(string $expected, string $file): void
{
// Given
Platform::putEnv('COMPOSER', 'tests/Fixtures/create-from-composer/composer.json');
Platform::putEnv('COMPOSER', $file);
$composer = ComposerFactory::create(new NullIO());
// When
......@@ -221,7 +258,7 @@ class SpecificationTest extends TestCase
// Then
$this->assertSame('prefix', $actual->getPrefix());
Platform::clearEnv('COMPOSER');
$this->assertSame($expected, $actual->computeConstraint());
}
public function testFailCreateFromComposer(): void
......
{
"name": "spip-league/create-from-bad-composer-lock",
"description": "Test Specification::createFromComposer",
"require": {
"test/prefix": "^1.0"
}
}
{
}
{
"name": "spip-league/create-from-composer",
"description": "Test Specification::createFromComposer",
"require": {
"test/prefix": "^1.0.x-dev"
}
}
{
"packages": [
{
"name": "test/prefix",
"version": "1.0.x-dev",
"source": {
"type": "git",
"url": "https://github.com/test/prefix.git",
"reference": "xxx"
},
"type": "library",
"description": "test prefix"
}
]
}
{
"one": {
"path": "path/to/one",
"source": "https://url/to/one.git"
"source": "https://url/to/one.git",
"branch": "main"
},
"two": {
"path": "path/to/two",
"source": "git@url:to/two.git"
"source": "git@url:to/two.git",
"branch": "main"
}
}
......@@ -43,10 +43,6 @@ class SpipInstallerTest extends TestCase
'expected' => false,
'type' => 'library',
],
'classic' => [
'expected' => true,
'type' => 'spip-classic',
],
'ecrire' => [
'expected' => true,
'type' => 'spip-ecrire',
......@@ -63,7 +59,7 @@ class SpipInstallerTest extends TestCase
}
#[DataProvider('dataPluginTypes')]
public function testSupports(string $expected, string $type): void
public function testSupports(bool $expected, string $type): void
{
$package = new Package('spip/test', '1.0.0', '1.0.0.0');
$package->setType($type);
......@@ -83,11 +79,6 @@ class SpipInstallerTest extends TestCase
'type' => 'library',
],
'classic' => [
'expected' => './tmp/__spip_classic__',
'name' => 'spip/test',
'type' => 'spip-classic',
],
'ecrire' => [
'expected' => './' . SpipPaths::BACK_OFFICE,
'name' => 'spip/test',
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter