Skip to content
Extraits de code Groupes Projets
AddToRequireTest.php 2,88 ko
Newer Older
JamesRezo's avatar
JamesRezo a validé
<?php

namespace SpipLeague\Test\Composer\Switch\Operation;
JamesRezo's avatar
JamesRezo a validé

use Composer\Composer;
use Composer\Config;
use Composer\Config\JsonConfigSource;
use Composer\Json\JsonFile;
use Composer\Package\Link;
use Composer\Package\RootPackage;
use Composer\Semver\Constraint\Constraint;
use Composer\Util\Filesystem;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SpipLeague\Composer\Switch\Operation\AddToRequire;
use SpipLeague\Test\Composer\Fixtures\CollectionDummy;
JamesRezo's avatar
JamesRezo a validé

#[CoversClass(AddToRequire::class)]
class AddToRequireTest extends TestCase
{
    private Composer $composer;

    private string $tmpDir;

    private Filesystem $filesystem;

    protected function setUp(): void
    {
        $this->filesystem = new Filesystem();
        $this->tmpDir = \sys_get_temp_dir() . '/AddToRequireTest';
        $this->filesystem->emptyDirectory($this->tmpDir);

        $rootPackage = new RootPackage('test/test', '1', '1');
marcimat's avatar
marcimat a validé
        $rootPackage->setRequires(
            ['vendor/exist' => new Link('test/test', 'vendor/exist', new Constraint('>=', '1.0'))],
        );
JamesRezo's avatar
JamesRezo a validé

        $this->composer = new Composer();
        $this->composer->setConfig(new Config());
marcimat's avatar
marcimat a validé
        $this->composer->getConfig()
            ->setConfigSource(new JsonConfigSource(new JsonFile($this->tmpDir . '/AddToRequireTest.json')));
JamesRezo's avatar
JamesRezo a validé
        $this->composer->setPackage($rootPackage);
marcimat's avatar
marcimat a validé
        $this->composer->getConfig()
            ->getConfigSource()
            ->addLink('require', 'vendor/exist', '^1.0');
JamesRezo's avatar
JamesRezo a validé
    }

    protected function tearDown(): void
    {
        $this->filesystem->unlink($this->tmpDir . '/AddToRequireTest.json');
        $this->filesystem->rmdir($this->tmpDir);
    }

JamesRezo's avatar
JamesRezo a validé
    /**
     * @return array<string,mixed>
     */
    public static function dataDo(): array
JamesRezo's avatar
JamesRezo a validé
    {
        return [
            'exist' => [
                'expected' => 'require vendor/exist:^2.0 added.',
                'vendorName' => 'vendor/exist',
                'constraint' => '^2.0',
            ],
            'new' => [
                'expected' => 'require vendor/new:^1.0 added.',
                'vendorName' => 'vendor/new',
                'constraint' => '^1.0',
            ],
        ];
    }

    #[DataProvider('dataDo')]
JamesRezo's avatar
JamesRezo a validé
    public function testDo(string $expected, string $vendorName, string $constraint): void
JamesRezo's avatar
JamesRezo a validé
    {
        // Given
        $operation = new AddToRequire($vendorName, $constraint);

        // When
        $actual = $operation->do(new CollectionDummy(), $this->composer);
JamesRezo's avatar
JamesRezo a validé
        /** @var array{require:array<string,string>} $actualContent */
        $actualContent = \json_decode(\file_get_contents($this->tmpDir . '/AddToRequireTest.json') ?: '', \true);
JamesRezo's avatar
JamesRezo a validé
        $actualContent = $actualContent['require'][$vendorName] ?? \null;

        // Then
        $this->assertSame($expected, $actual);
        $this->assertSame($constraint, $actualContent);
    }
}