<commit_before><?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace PhpCsFixer\Tests\Smoke;

use Keradus\CliExecutor\CommandExecutor;
use Keradus\CliExecutor\ScriptExecutor;
use PhpCsFixer\Tests\TestCase;

/**
 * @author <NAME> <<EMAIL>>
 *
 * @internal
 *
 * @requires OS Linux|Darwin
 * @coversNothing
 * @group covers-nothing
 * @large
 */
final class CiIntegrationTest extends TestCase
{
    public static $fixtureDir;

    public static function setUpBeforeClass()
    {
        parent::setUpBeforeClass();

        self::$fixtureDir = __DIR__.'/../Fixtures/ci-integration';

        try {
            self::executeScript(array(
                'rm -rf .git',
                'git init -q',
                'git config user.name test',
                'git config user.email test',
                'git add .',
                'git commit -m "init" -q',
            ));
        } catch (\RuntimeException $e) {
            self::markTestSkipped($e->getMessage());
        }
    }

    public static function tearDownAfterClass()
    {
        parent::tearDownAfterClass();

        self::executeCommand('rm -rf .git');
    }

    public function tearDown()
    {
        parent::tearDown();

        self::executeScript(array(
            'git reset . -q',
            'git checkout . -q',
            'git clean -fdq',
            'git checkout master -q',
        ));
    }

    /**
     * @param string   $branchName
     * @param string[] $caseCommands
     * @param string[] $expectedResult1Lines
     * @param string[] $expectedResult2Lines
     * @param string   $expectedResult3Files
     *
     * @dataProvider provideIntegrationCases
     */
    public function testIntegration(
        $branchName,
        array $caseCommands,
        array $expectedResult1Lines,
        array $expectedResult2Lines,
        $expectedResult3Files
    ) {
        self::executeScript(array_merge(
            array(
                "git checkout -b ${branchName} -q",
            ),
            $caseCommands
        ));

        $integrationScript = explode("\n", str_replace('vendor/bin/', './../../../', file_get_contents(__DIR__.'/../../dev-tools/ci-integration.sh')));
        $steps = array(
            "COMMIT_RANGE=\"master..${branchName}\"",
            "{$integrationScript[3]}\n{$integrationScript[4]}",
            $integrationScript[5],
            $integrationScript[6],
            $integrationScript[7],
        );

        $result1 = self::executeScript(array(
            $steps[0],
            $steps[1],
            $steps[2],
            'echo "$CHANGED_FILES"',
        ));

        $this->assertSame(implode("\n", $expectedResult1Lines)."\n", $result1->getOutput());

        $result2 = self::executeScript(array(
            $steps[0],
            $steps[1],
            $steps[2],
            $steps[3],
            'echo "${EXTRA_ARGS}"',
        ));

        $this->assertSame(implode("\n", $expectedResult2Lines), $result2->getOutput());

        $result3 = self::executeScript(array(
            $steps[0],
            $steps[1],
            $steps[2],
            $steps[3],
            $steps[4],
        ));

        $optionalIncompatibilityWarning = 'PHP needs to be a minimum version of PHP 5.3.6 and maximum version of PHP 7.2.*.
Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.
';

        $optionalXdebugWarning = 'You are running PHP CS Fixer with xdebug enabled. This has a major impact on runtime performance.
If you need help while solving warnings, ask at https://gitter.im/PHP-CS-Fixer, we will help you!
';

        $executionDetails = "Loaded config default from \".php_cs.dist\".
${expectedResult3Files}
Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error";

        $this->assertRegExp(
            sprintf(
                '/^(%s)?(%s)?%s$/',
                preg_quote($optionalIncompatibilityWarning, '/'),
                preg_quote($optionalXdebugWarning, '/'),
                preg_quote($executionDetails, '/')
            ),
            $result3->getError()
        );

        $this->assertRegExp(<commit_msg>minor #3660 Fix  do not rely on order of fixing in CiIntegrationTest (kubawerlos)<commit_after><?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace PhpCsFixer\Tests\Smoke;

use Keradus\CliExecutor\CommandExecutor;
use Keradus\CliExecutor\ScriptExecutor;
use PhpCsFixer\Tests\TestCase;

/**
 * @author <NAME> <<EMAIL>>
 *
 * @internal
 *
 * @requires OS Linux|Darwin
 * @coversNothing
 * @group covers-nothing
 * @large
 */
final class CiIntegrationTest extends TestCase
{
    public static $fixtureDir;

    public static function setUpBeforeClass()
    {
        parent::setUpBeforeClass();

        self::$fixtureDir = __DIR__.'/../Fixtures/ci-integration';

        try {
            self::executeScript(array(
                'rm -rf .git',
                'git init -q',
                'git config user.name test',
                'git config user.email test',
                'git add .',
                'git commit -m "init" -q',
            ));
        } catch (\RuntimeException $e) {
            self::markTestSkipped($e->getMessage());
        }
    }

    public static function tearDownAfterClass()
    {
        parent::tearDownAfterClass();

        self::executeCommand('rm -rf .git');
    }

    public function tearDown()
    {
        parent::tearDown();

        self::executeScript(array(
            'git reset . -q',
            'git checkout . -q',
            'git clean -fdq',
            'git checkout master -q',
        ));
    }

    /**
     * @param string   $branchName
     * @param string[] $caseCommands
     * @param string[] $expectedResult1Lines
     * @param string[] $expectedResult2Lines
     * @param string   $expectedResult3Files
     *
     * @dataProvider provideIntegrationCases
     */
    public function testIntegration(
        $branchName,
        array $caseCommands,
        array $expectedResult1Lines,
        array $expectedResult2Lines,
        $expectedResult3Files
    ) {
        self::executeScript(array_merge(
            array(
                "git checkout -b ${branchName} -q",
            ),
            $caseCommands
        ));

        $integrationScript = explode("\n", str_replace('vendor/bin/', './../../../', file_get_contents(__DIR__.'/../../dev-tools/ci-integration.sh')));
        $steps = array(
            "COMMIT_RANGE=\"master..${branchName}\"",
            "{$integrationScript[3]}\n{$integrationScript[4]}",
            $integrationScript[5],
            $integrationScript[6],
            $integrationScript[7],
        );

        $result1 = self::executeScript(array(
            $steps[0],
            $steps[1],
            $steps[2],
            'echo "$CHANGED_FILES"',
        ));

        $this->assertSame(implode("\n", $expectedResult1Lines)."\n", $result1->getOutput());

        $result2 = self::executeScript(array(
            $steps[0],
            $steps[1],
            $steps[2],
            $steps[3],
            'echo "${EXTRA_ARGS}"',
        ));

        $this->assertSame(implode("\n", $expectedResult2Lines), $result2->getOutput());

        $result3 = self::executeScript(array(
            $steps[0],
            $steps[1],
            $steps[2],
            $steps[3],
            $steps[4],
        ));

        $optionalIncompatibilityWarning = 'PHP needs to be a minimum version of PHP 5.3.6 and maximum version of PHP 7.2.*.
Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.
';

        $optionalXdebugWarning = 'You are running PHP CS Fixer with xdebug enabled. This has a major impact on runtime performance.
If you need help while solving warnings, ask at https://gitter.im/PHP-CS-Fixer, we will help you!
';

        $pattern = sprintf(
            '/^(?:%s)?(?:%s)?%s\n([\.S]{%d})\n%s$/',
            preg_quote($optionalIncompatibilityWarning, '/'),
            preg_quote($optionalXdebugWarning, '/'),
            preg_quote('Loaded config default from ".php_cs.dist".', '/'),
            strlen($expectedResult3Files),
            preg_quote('Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error', '/')
        );

        $this->assertRegExp($pattern, $result3->getError());

        preg_match($pattern, $result3->getError(), $matches);

        $this->assertArrayHasKey(1, $matches);
        $this->assertSame(substr_count($expectedResult3Files, '.'), substr_count($matches[1], '.'));
        $this->assertSame(substr_count($expectedResult3Files, 'S'), substr_count($matches[1], 'S'));

        $this->assertRegExp(
{'fraction_non_alphanumeric': 0.14288866821140903, 'fraction_numerical': 0.009268454154253558, 'mean_word_length': 2.1009237085186454, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 41, 'https://': 2, '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': '6795062', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2595, 'n_words': 587}