<commit_before><?php
/*
 * This file is part of PHPUnit.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PHPUnit\Runner;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;

/**
 * @group test-reorder
 */
class TestSuiteSorterTest extends TestCase
{
    /**
     * Constants to improve clarity of @dataprovider
     */
    private const IGNORE_DEPENDENCIES  = false;

    private const RESOLVE_DEPENDENCIES = true;

    public function testThrowsExceptionWhenUsingInvalidOrderOption()
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        $this->expectException(Exception::class);
        $this->expectExceptionMessage('$order must be one of TestSuiteSorter::ORDER_DEFAULT, TestSuiteSorter::ORDER_REVERSED, or TestSuiteSorter::ORDER_RANDOMIZED');
        $sorter->reorderTestsInSuite($suite, -1, false, TestSuiteSorter::ORDER_DEFAULT);
    }

    public function testThrowsExceptionWhenUsingInvalidOrderDefectsOption()
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        $this->expectException(Exception::class);
        $this->expectExceptionMessage('$orderDefects must be one of TestSuiteSorter::ORDER_DEFAULT, TestSuiteSorter::ORDER_DEFECTS_FIRST');
        $sorter->reorderTestsInSuite($suite, TestSuiteSorter::ORDER_DEFAULT, false, -1);
    }

    /**
     * @dataProvider suiteSorterOptionPermutationsProvider
     */
    public function testShouldNotAffectEmptyTestSuite(int $order, bool $resolveDependencies, int $orderDefects)
    {
        $sorter = new TestSuiteSorter;
        $suite  = new TestSuite;

        $this->assertSame([], $suite->tests());

        $sorter->reorderTestsInSuite($suite, $order, $resolveDependencies, $orderDefects);

        $this->assertSame([], $suite->tests());
    }

    /**
     * @dataProvider commonSorterOptionsProvider
     */
    public function testBasicExecutionOrderOptions(int $order, bool $resolveDependencies, array $expected)
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        $sorter->reorderTestsInSuite($suite, $order, $resolveDependencies, TestSuiteSorter::ORDER_DEFAULT);

        $this->assertSame($expected, $this->getTestExecutionOrder($suite));
    }

    public function testCanSetRandomizationWithASeed()
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        \mt_srand(54321);
        $sorter->reorderTestsInSuite($suite, TestSuiteSorter::ORDER_RANDOMIZED, false, TestSuiteSorter::ORDER_DEFAULT);

        $this->assertSame(['testTwo', 'testFour', 'testFive', 'testThree', 'testOne'], $this->getTestExecutionOrder($suite));
    }

    public function testCanSetRandomizationWithASeedAndResolveDependencies()
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        \mt_srand(54321);
        $sorter->reorderTestsInSuite($suite, TestSuiteSorter::ORDER_RANDOMIZED, true, TestSuiteSorter::ORDER_DEFAULT);

        $this->assertSame(['testTwo', 'testFive', 'testOne', 'testThree', 'testFour'], $this->getTestExecutionOrder($suite));
    }

    /**
     * @dataProvider defectsSorterOptionsProvider
     */
    public function testSuiteSorterDefectsOptions(int $order, bool $resolveDependencies, array $runState, array $expected)
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);

        $cache = new TestResultCache();

        foreach ($runState as $testName => $data) {
            $cache->setState($testName, $data['state']);
            $cache->setTime($testName, $data['time']);
        }

        $sorter  = new TestSuiteSorter($cache);
        $sorter->reorderTestsInSuite($suite, $order, $resolveDependencies, TestSuiteSorter::ORDER_DEFECTS_FIRST);

        $this->assertSame($expected, $this->getTestExecutionOrder($suite));
    }

    /**
     * A @dataprovider for basic execution reordering options based on MultiDependencyTest
     * This class has the following relevant properties:
     * - it has five tests 'testOne' ... 'testFive'
     * - 'testThree' @depends on both 'testOne' and 'testTwo'
     * - 'testFour' @depends on 'MultiDependencyTest::testThree' to test FQN @depends
     * - 'testFive' has no dependencies
     */
    public function commonSorterOptionsProvider(): array
    {
        return [
            'default' => [
                TestSuiteSorter::ORDER_DEFAULT,
                self::IGNORE_DEPENDENCIES,
                ['testOne', 'testTwo', 'testThree', 'testFour', 'testFive']],

            // Activating dependency resolution should have no effect under normal circumstances
            'resolve default' => [
                TestSuiteSorter::ORDER_DEFAULT,
                self::IGNORE_DEPENDENCIES,
                ['testOne', 'testTwo', 'testThree', 'testFour', 'testFive']],

            // Reversing without checks should give a simple reverse order
            'reverse' => [
                TestSuiteSorter::ORDER_REVERSED,
                self::IGNORE_DEPENDENCIES,
                ['testFive', 'testFour', 'testThree', 'testTwo', 'testOne']],

            // Reversing with resolution still allows testFive to move to front, testTwo before testOne
            'resolve reverse' => [
                TestSuiteSorter::ORDER_REVERSED,
                self::RESOLVE_DEPENDENCIES,
                ['testFive', 'testTwo', 'testOne', 'testThree', 'testFour']],
        ];<commit_msg>Fix: Actually resolve dependencies<commit_after><?php
/*
 * This file is part of PHPUnit.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PHPUnit\Runner;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;

/**
 * @group test-reorder
 */
class TestSuiteSorterTest extends TestCase
{
    /**
     * Constants to improve clarity of @dataprovider
     */
    private const IGNORE_DEPENDENCIES  = false;

    private const RESOLVE_DEPENDENCIES = true;

    public function testThrowsExceptionWhenUsingInvalidOrderOption()
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        $this->expectException(Exception::class);
        $this->expectExceptionMessage('$order must be one of TestSuiteSorter::ORDER_DEFAULT, TestSuiteSorter::ORDER_REVERSED, or TestSuiteSorter::ORDER_RANDOMIZED');
        $sorter->reorderTestsInSuite($suite, -1, false, TestSuiteSorter::ORDER_DEFAULT);
    }

    public function testThrowsExceptionWhenUsingInvalidOrderDefectsOption()
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        $this->expectException(Exception::class);
        $this->expectExceptionMessage('$orderDefects must be one of TestSuiteSorter::ORDER_DEFAULT, TestSuiteSorter::ORDER_DEFECTS_FIRST');
        $sorter->reorderTestsInSuite($suite, TestSuiteSorter::ORDER_DEFAULT, false, -1);
    }

    /**
     * @dataProvider suiteSorterOptionPermutationsProvider
     */
    public function testShouldNotAffectEmptyTestSuite(int $order, bool $resolveDependencies, int $orderDefects)
    {
        $sorter = new TestSuiteSorter;
        $suite  = new TestSuite;

        $this->assertSame([], $suite->tests());

        $sorter->reorderTestsInSuite($suite, $order, $resolveDependencies, $orderDefects);

        $this->assertSame([], $suite->tests());
    }

    /**
     * @dataProvider commonSorterOptionsProvider
     */
    public function testBasicExecutionOrderOptions(int $order, bool $resolveDependencies, array $expected)
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        $sorter->reorderTestsInSuite($suite, $order, $resolveDependencies, TestSuiteSorter::ORDER_DEFAULT);

        $this->assertSame($expected, $this->getTestExecutionOrder($suite));
    }

    public function testCanSetRandomizationWithASeed()
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        \mt_srand(54321);
        $sorter->reorderTestsInSuite($suite, TestSuiteSorter::ORDER_RANDOMIZED, false, TestSuiteSorter::ORDER_DEFAULT);

        $this->assertSame(['testTwo', 'testFour', 'testFive', 'testThree', 'testOne'], $this->getTestExecutionOrder($suite));
    }

    public function testCanSetRandomizationWithASeedAndResolveDependencies()
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);
        $sorter = new TestSuiteSorter();

        \mt_srand(54321);
        $sorter->reorderTestsInSuite($suite, TestSuiteSorter::ORDER_RANDOMIZED, true, TestSuiteSorter::ORDER_DEFAULT);

        $this->assertSame(['testTwo', 'testFive', 'testOne', 'testThree', 'testFour'], $this->getTestExecutionOrder($suite));
    }

    /**
     * @dataProvider defectsSorterOptionsProvider
     */
    public function testSuiteSorterDefectsOptions(int $order, bool $resolveDependencies, array $runState, array $expected)
    {
        $suite = new TestSuite;
        $suite->addTestSuite(\MultiDependencyTest::class);

        $cache = new TestResultCache();

        foreach ($runState as $testName => $data) {
            $cache->setState($testName, $data['state']);
            $cache->setTime($testName, $data['time']);
        }

        $sorter  = new TestSuiteSorter($cache);
        $sorter->reorderTestsInSuite($suite, $order, $resolveDependencies, TestSuiteSorter::ORDER_DEFECTS_FIRST);

        $this->assertSame($expected, $this->getTestExecutionOrder($suite));
    }

    /**
     * A @dataprovider for basic execution reordering options based on MultiDependencyTest
     * This class has the following relevant properties:
     * - it has five tests 'testOne' ... 'testFive'
     * - 'testThree' @depends on both 'testOne' and 'testTwo'
     * - 'testFour' @depends on 'MultiDependencyTest::testThree' to test FQN @depends
     * - 'testFive' has no dependencies
     */
    public function commonSorterOptionsProvider(): array
    {
        return [
            'default' => [
                TestSuiteSorter::ORDER_DEFAULT,
                self::IGNORE_DEPENDENCIES,
                ['testOne', 'testTwo', 'testThree', 'testFour', 'testFive']],

            // Activating dependency resolution should have no effect under normal circumstances
            'resolve default' => [
                TestSuiteSorter::ORDER_DEFAULT,
                self::RESOLVE_DEPENDENCIES,
                ['testOne', 'testTwo', 'testThree', 'testFour', 'testFive']],

            // Reversing without checks should give a simple reverse order
            'reverse' => [
                TestSuiteSorter::ORDER_REVERSED,
                self::IGNORE_DEPENDENCIES,
                ['testFive', 'testFour', 'testThree', 'testTwo', 'testOne']],

            // Reversing with resolution still allows testFive to move to front, testTwo before testOne
            'resolve reverse' => [
                TestSuiteSorter::ORDER_REVERSED,
                self::RESOLVE_DEPENDENCIES,
                ['testFive', 'testTwo', 'testOne', 'testThree', 'testFour']],
        ];
{'fraction_non_alphanumeric': 0.12030396174863388, 'fraction_numerical': 0.0020491803278688526, 'mean_word_length': 3.2133093525179857, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4927326', 'n_tokens_mistral': 3393, 'n_tokens_neox': 3249, 'n_words': 683}