<?php

declare(strict_types=1);

namespace Codelicia\Xulieta\Test\Unit\AutoPlugin;

use Codelicia\Xulieta\AutoPlugin\Register;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Installer\PackageEvent;
use Composer\Package\PackageInterface;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

final class RegisterTest extends TestCase
{
    #[Test]
    public function itShouldRunOnlyOnDevEnvironment(): void
    {
        $event = $this->createMock(PackageEvent::class);
        $event->expects($this->once())->method('isDevMode')->willReturn(false);
        $event->expects($this->never())->method('getOperation');

        Register::scan($event);

        self::assertTrue(true);
    }

    #[Test]
    public function itShouldFailWhenGetOperationHasNoValidResult(): void
    {
        $event = $this->createMock(PackageEvent::class);
        $event->expects($this->once())->method('isDevMode')->willReturn(true);

        $updateOperation = $this->createMock(UpdateOperation::class);
        $event->expects($this->once())->method('getOperation')->willReturn($updateOperation);

        $this->expectExceptionMessage('assert($operation instanceof InstallOperation)');

        Register::scan($event);
    }

    #[Test]
    public function itShouldDoNothingWhenNoExtraKeyIsConfigured(): void
    {
        $event = $this->createMock(PackageEvent::class);
        $event->expects($this->once())->method('isDevMode')->willReturn(true);

        $installOperation = $this->createMock(InstallOperation::class);
        $package          = $this->createMock(PackageInterface::class);

        $event->expects($this->once())->method('getOperation')->willReturn($installOperation);
        $installOperation->expects($this->once())->method('getPackage')->willReturn($package);

        $package->expects($this->once())->method('getExtra')->willReturn([]);

        $event->expects($this->never())->method('getIO');
        $event->expects($this->never())->method('getComposer');

        Register::scan($event);
    }
}
