Skip to content
Extraits de code Groupes Projets
AddSpecificationTest.php 3,25 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\Util\Filesystem;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SpipLeague\Composer\Switch\Operation\AddSpecification;
use SpipLeague\Composer\Switch\Operation\OperationInterface;
use SpipLeague\Test\Composer\Fixtures\CollectionMock;
use SpipLeague\Test\Composer\Fixtures\SpecificationMock;
use SpipLeague\Test\Composer\Fixtures\SpecificationStub;
JamesRezo's avatar
JamesRezo a validé

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

    private string $tmpDir;

    private Filesystem $filesystem;

    private CollectionMock $collection;

    protected function setUp(): void
    {
        $this->composer = new Composer();
        $this->filesystem = new Filesystem();
        $this->tmpDir = \sys_get_temp_dir() . '/AddSpecificationTest';
        $this->filesystem->emptyDirectory($this->tmpDir);
        $this->collection = new CollectionMock($this->tmpDir . '/AddSpecificationTest.json');
        $this->collection[] = new SpecificationMock('exist');
marcimat's avatar
marcimat a validé
        \file_put_contents(
            $this->tmpDir . '/AddSpecificationTest.json',
            '{"exist":{"path":"path","source":"https://git-server/vendor/exist.git","branch":"1.0","tag":"v1.0.0"}}',
        );
JamesRezo's avatar
JamesRezo a validé
    }

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

JamesRezo's avatar
JamesRezo a validé
    /**
     * @return array<string,mixed>
     */
    public static function dataMark(): array
JamesRezo's avatar
JamesRezo a validé
    {
        return [
            'not-exist' => [
                'expected' => OperationInterface::class,
                'prefix' => 'not-exist',
            ],
            'exist' => [
                'expected' => \null,
                'prefix' => 'exist',
            ],
        ];
    }

JamesRezo's avatar
JamesRezo a validé
    /**
     * @param ?class-string<object> $expected
     */
JamesRezo's avatar
JamesRezo a validé
    #[DataProvider('dataMark')]
JamesRezo's avatar
JamesRezo a validé
    public function testMark(?string $expected, string $prefix): void
JamesRezo's avatar
JamesRezo a validé
    {
        // Given
        $operation = new AddSpecification(new SpecificationStub($prefix));

        // When
        $actual = $operation->mark($this->collection, $this->composer);

        // Then
marcimat's avatar
marcimat a validé
        if ($expected === null) {
JamesRezo's avatar
JamesRezo a validé
            $this->assertNull($actual);
        } else {
            $this->assertInstanceOf($expected, $actual);
        }
    }

JamesRezo's avatar
JamesRezo a validé
    /**
     * @return array<string,mixed>
     */
    public static function dataDo(): array
JamesRezo's avatar
JamesRezo a validé
    {
        return [
            'exist' => [
                'expected' => 'exist added to ',
                'prefix' => 'exist',
            ],
            'not-exist' => [
                'expected' => 'not-exist added to ',
                'prefix' => 'not-exist',
            ],
        ];
    }

JamesRezo's avatar
JamesRezo a validé
    /**
     * @param non-empty-string $expected
     */
JamesRezo's avatar
JamesRezo a validé
    #[DataProvider('dataDo')]
JamesRezo's avatar
JamesRezo a validé
    public function testDo(string $expected, string $prefix): void
JamesRezo's avatar
JamesRezo a validé
    {
        // Given
        $operation = new AddSpecification(new SpecificationStub($prefix));

        // When
        $actual = $operation->do($this->collection, $this->composer);

        // Then
        $this->assertStringStartsWith($expected, $actual);
    }
}