<?php

declare(strict_types=1);

namespace Tests\Codeception\Task\Splitter;

use Codeception\Task\Splitter\TestFileSplitterTask;
use Codeception\Task\Splitter\TestsSplitterTrait;
use Consolidation\Log\Logger;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Finder\Finder;
use const Tests\Codeception\Task\TEST_PATH;

/**
 * Class TestFileSplitterTaskTest
 *
 * @coversDefaultClass \Codeception\Task\Splitter\TestFileSplitterTask
 */
final class TestFileSplitterTaskTest extends TestCase
{
    use TestsSplitterTrait;

    public function providerTestFilesCanBeSplit(): array
    {
        return [
            [
                'groups' => 1,
                'from' => 'tests/fixtures/DependencyResolutionExampleTests/',
                'to' => TEST_PATH . '/result/group_',
                'expected' => 1,
            ],
            [
                'groups' => 2,
                'from' => 'tests/fixtures/DependencyResolutionExampleTests/',
                'to' => TEST_PATH . '/result/group_',
                'expected' => 2,
            ],
            [
                'groups' => 3,
                'from' => 'tests/fixtures/DependencyResolutionExampleTests/',
                'to' => TEST_PATH . '/result/group_',
                'expected' => 3,
            ],
            [
                'groups' => 4,
                'from' => 'tests/fixtures/DependencyResolutionExampleTests/',
                'to' => TEST_PATH . '/result/group_',
                'expected' => 3,
            ],
            [
                'groups' => 4,
                'from' => 'tests/fixtures/DependencyResolutionExampleTests',
                'to' => TEST_PATH . '/result/group_',
                'expected' => 4,
            ],
        ];
    }

    /**
     * @covers ::run
     * @dataProvider providerTestFilesCanBeSplit
     */
    public function testFilesCanBeSplitted(
        int $groups,
        string $from,
        string $groupTo,
        int $expected
    ): void {
        $task = new TestFileSplitterTask($groups);
        $task->setLogger(new Logger(new NullOutput()));
        $task->testsFrom($from)
            ->groupsTo($groupTo)
            ->run();

        for ($i = 1; $i <= $expected; ++$i) {
            $this->assertFileExists($groupTo . $i);
        }

        $this->assertFileDoesNotExist($groupTo . ($expected + 1));
    }

    /**
     * @covers ::run
     */
    public function testPatternNotFound(): void
    {
        $task = new TestFileSplitterTask(2);
        $task->setLogger(new Logger(new NullOutput()));
        $task
            ->setPattern(['*Cest.php'])
            ->testsFrom('tests/fixtures/DependencyResolutionExampleTests/')
            ->groupsTo(TEST_PATH . '/result/group_')
            ->run();

        $this->assertFileDoesNotExist(TEST_PATH . '/result/group_1');
        $this->assertFileDoesNotExist(TEST_PATH . '/result/group_2');
    }

    protected function tearDown(): void
    {
        parent::tearDown(); // TODO: Change the autogenerated stub
        $finder = Finder::create()
            ->files()
            ->name('group_*');

        foreach ($finder->in(TEST_PATH . '/result') as $file) {
            unlink($file->getPathname());
        }
    }
}
