CollectionTest.php 5,66 Kio
<?php
namespace SpipLeague\Test\Composer\Extensions;
use Composer\IO\NullIO;
use Composer\Util\ProcessExecutor;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SpipLeague\Composer\Extensions\InvalidSpecificationException;
use SpipLeague\Composer\Extensions\Collection;
use SpipLeague\Composer\Extensions\Specification;
use SpipLeague\Composer\Git\RemoteUrls;
use SpipLeague\Test\Composer\Fixtures\SpecificationStub;
#[CoversClass(Collection::class)]
#[CoversClass(Specification::class)]
#[CoversClass(RemoteUrls::class)]
class CollectionTest extends TestCase
{
private SpecificationStub $validStub;
private Collection $testCollection;
private RemoteUrls $changer;
protected function setUp(): void
{
$this->validStub = new SpecificationStub('valid', 'stub/valid', '1.0.0');
$this->testCollection = new Collection([$this->validStub], 'dummy.json');
$this->changer = new RemoteUrls(new ProcessExecutor(new NullIO()));
}
public function testInvalidSpecification(): void
{
// Given
$this->expectException(InvalidSpecificationException::class);
$this->expectExceptionMessage('A collection must contain at least one valid specification.');
// When
new Collection([new SpecificationStub()], 'dummy.json');
// Then
// An exception is thrown
}
public function testInvalidSpecificationByArrayAccess(): void
{
// Given
$this->expectException(InvalidSpecificationException::class);
$this->expectExceptionMessage('A collection must only contain valid specifications.');
// When
/** @phpstan-ignore assign.propertyType */
$this->testCollection[] = 'invalid'; /** @phpstan-ignore offsetAssign.valueType */
// Then
// An exception is thrown
}
/**
* @return array<string,mixed>
*/
public static function dataGetByArrayAccess(): array
{
return [
'notexists' => [
'expected' => null,
'prefix' => 'notexists',
],
'exists' => [
'expected' => 'stub/valid',
'prefix' => 'valid',
],
];
}
#[DataProvider('dataGetByArrayAccess')]
public function testGetByArrayAccess(?string $expected, string $prefix): void
{
// Given
// When
$actual = $this->testCollection[$prefix];
// Then
$this->assertEquals($expected, $actual?->computeVendorName($this->changer));
}
/**
* @return array<string,mixed>
*/
public static function dataUnsetByArrayAccess(): array
{
return [
'notexists' => [
'expected' => 1,
'prefix' => 'notexists',
],
'exists' => [
'expected' => 0,
'prefix' => 'valid',
],
];
}
#[DataProvider('dataUnsetByArrayAccess')]
public function testUnsetByArrayAccess(int $expected, string $prefix): void
{
// Given
// When
unset($this->testCollection[$prefix]);
// Then
$this->assertCount($expected, $this->testCollection);
}
/**
* @return array<string,mixed>
*/
public static function dataSetByArrayAccess(): array
{
return [
'new' => [
'expected' => 2,
'prefix' => 'test',
],
'existing' => [
'expected' => 1,
'prefix' => 'valid',
],
];
}
#[DataProvider('dataSetByArrayAccess')]
public function testSetByArrayAccess(int $expected, string $prefix): void
{
// Given
// When
$this->testCollection[] = new SpecificationStub($prefix);
// Then
$this->assertCount($expected, $this->testCollection);
$this->assertNotNull($this->testCollection[$prefix]);
}
public function testJsonSerialization(): void
{
// Given
// When
$this->testCollection[] = new SpecificationStub('test');
$actual = \json_encode($this->testCollection);
// Then
$this->assertSame(
'{"valid":{"path":"path","source":"source"},"test":{"path":"path","source":"source"}}',
$actual,
);
}
/**
* @return array<string,mixed>
*/
public static function dataFromJsonFile(): array
{
return [
'two' => [
'expected' => 2,
'file' => __DIR__ . '/../Fixtures/two.json',
'prefix' => 'one',
],
];
}
#[DataProvider('dataFromJsonFile')]
public function testFromJsonFile(int $expected, string $file, string $prefix): void
{
// Given
// When
$actual = Collection::fromJsonFile($file);
// Then
$this->assertCount($expected, $actual);
$this->assertTrue(isset($actual[$prefix]));
}
public function todo_testMissingFromJsonFile(): void
{
// Given
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches(',File "plugins-dist.json" is missing,');
// When
Collection::fromJsonFile();
// Then
// An exception is thrown
}
public function testMalformedFromJsonFile(): void
{
// Given
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches(',Fixtures/malformed.json" malformed,');
// When
Collection::fromJsonFile(__DIR__ . '/../Fixtures/malformed.json');
// Then
// An exception is thrown
}
}