<commit_before><?php

/*
 * This file is part of Gush package.
 *
 * (c) 2013-2014 <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Gush\Tests\Helper;

use Gush\Helper\FilesystemHelper;
use Gush\Helper\GitHelper;
use Gush\Helper\ProcessHelper;
use Prophecy\Prophecy\ObjectProphecy;
use Prophecy\Prophet;

class GitHelperTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var GitHelper
     */
    protected $git;

    /**
     * @var GitHelper
     */
    protected $unitGit;

    /**
     * @var ProcessHelper|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $processHelper;

    /**
     * @var FilesystemHelper|ObjectProphecy
     */
    private $filesystemHelper;

    /**
     * @var Prophet
     */
    private $prophet;

    /**
     * @var FilesystemHelper
     */
    private $realFsHelper;

    public function setUp()
    {
        $this->prophet = new Prophet();

        $this->processHelper = $this->getMock('Gush\Helper\ProcessHelper');

        $this->filesystemHelper = $this->prophet->prophesize('Gush\Helper\FilesystemHelper');
        $this->filesystemHelper->getName()->willReturn('filesystem');

        $this->realFsHelper = new FilesystemHelper();

        $this->git = new GitHelper(new ProcessHelper(), $this->realFsHelper);
        $this->unitGit = new GitHelper($this->processHelper, $this->filesystemHelper->reveal());
    }

    public function tearDown()
    {
        $this->prophet->checkPredictions();
    }

    /**
     * @test
     */
    public function bears_the_right_helper_name()
    {
        $this->assertEquals('git', $this->git->getName());
    }

    /**
     * @test
     */
    public function gets_current_git_branch_name()
    {
        exec('git rev-parse --abbrev-ref HEAD', $output);

        if ('HEAD' === $output[0]) {
            $this->markTestSkipped('Unable to run this test in a detached HEAD state.');
        }

        $this->assertEquals($output[0], $this->git->getActiveBranchName());
    }

    /**
     * @test
     */
    public function gets_the_last_tag_on_current_branch()
    {
        exec('git describe --tags --abbrev=0 HEAD', $output);
        $this->assertEquals($output[0], $this->git->getLastTagOnBranch());
    }

    /**
     * @test
     * @dataProvider repoUrlProvider
     */
    public function gets_the_repository_name($repo)
    {
        $return = <<<EOT
* remote origin
  Fetch URL: {$repo}
  Push  URL: {$repo}
  HEAD branch: (not queried)
  Remote branches: (status not queried)
    master
  Local branches configured for 'git pull':
    master                             merges with remote master
  Local ref configured for 'git push' (status not queried):
    (matching) pushes to (matching)
EOT;

        $this->processHelper
            ->expects($this->any())
            ->method('runCommand')
            ->will($this->returnValue($return))
        ;

        $this->assertEquals('gush', $this->unitGit->getRepoName());
    }

    /**
     * @test
     * @dataProvider repoUrlProvider
     */
    public function gets_vendor_name_for_repository($repo)
    {
        $return = <<<EOT
* remote origin
  Fetch URL: {$repo}
  Push  URL: {$repo}
  HEAD branch: (not queried)
  Remote branches: (status not queried)
    master
  Local branches configured for 'git pull':
    master                             merges with remote master
  Local ref configured for 'git push' (status not queried):
    (matching) pushes to (matching)
EOT;

        $this->processHelper
            ->expects($this->any())
            ->method('runCommand')
            ->will($this->returnValue($return))
        ;

        $this->assertEquals(getenv('GIT_VENDOR_NAME'), $this->unitGit->getVendorName());
    }

    /**
     * @test
     */
    public function lists_files()
    {
        // Smoke test for a real listFiles
        $res = $this->git->listFiles();
        $this->assertGreaterThan(50, $res);
    }

    /**
     * @test
     */
    public function merges_remote_branch_in_clean_wc()
    {
        $base = 'master';
        $baseRemote = $sourceRemote = 'origin';
        $sourceBranch = 'amazing-feature';
        $tmpName = $this->realFsHelper->newTempFilename();
        $hash = '8ae59958a2632018275b8db9590e9a79331030cb';
        $message = "Black-box testing 123\n\n\nAah!";

        $processHelper = $this->prophet->prophesize('Gush\Helper\ProcessHelper');
        $this->unitGit = new GitHelper($processHelper->reveal(), $this->filesystemHelper->reveal());

        $this->filesystemHelper->newTempFilename()->willReturn($tmpName);

        $processHelper->runCommand('git config --local --get remote.origin.url', true)->willReturn(true);
        $processHelper->runCommand('git status --porcelain --untracked-files=no')->willReturn("\n");
        $processHelper->runCommands(
            [
                [
                    'line' => 'git remote update',
                    'allow_failures' => false,
                ],
                [
                    'line' => 'git checkout '.$base,
                    'allow_failures' => false,
                ],
                [
                    'line' => 'git pull --ff-only',
                    'allow_failures' => false,
                ],
                [
                    'line' => ['git', 'merge', '--no-ff', '--no-commit', $sourceRemote.'/'.$sourceBranch],<commit_msg>fix GitHelperTest<commit_after><?php

/*
 * This file is part of Gush package.
 *
 * (c) 2013-2014 <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Gush\Tests\Helper;

use Gush\Helper\FilesystemHelper;
use Gush\Helper\GitHelper;
use Gush\Helper\ProcessHelper;
use Prophecy\Prophecy\ObjectProphecy;
use Prophecy\Prophet;

class GitHelperTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var GitHelper
     */
    protected $git;

    /**
     * @var GitHelper
     */
    protected $unitGit;

    /**
     * @var ProcessHelper|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $processHelper;

    /**
     * @var FilesystemHelper|ObjectProphecy
     */
    private $filesystemHelper;

    /**
     * @var Prophet
     */
    private $prophet;

    /**
     * @var FilesystemHelper
     */
    private $realFsHelper;

    public function setUp()
    {
        $this->prophet = new Prophet();

        $this->processHelper = $this->getMock('Gush\Helper\ProcessHelper');

        $this->filesystemHelper = $this->prophet->prophesize('Gush\Helper\FilesystemHelper');
        $this->filesystemHelper->getName()->willReturn('filesystem');

        $this->realFsHelper = new FilesystemHelper();

        $this->git = new GitHelper(new ProcessHelper(), $this->realFsHelper);
        $this->unitGit = new GitHelper($this->processHelper, $this->filesystemHelper->reveal());
    }

    public function tearDown()
    {
        $this->prophet->checkPredictions();
    }

    /**
     * @test
     */
    public function bears_the_right_helper_name()
    {
        $this->assertEquals('git', $this->git->getName());
    }

    /**
     * @test
     */
    public function gets_current_git_branch_name()
    {
        exec('git rev-parse --abbrev-ref HEAD', $output);

        if ('HEAD' === $output[0]) {
            $this->markTestSkipped('Unable to run this test in a detached HEAD state.');
        }

        $this->assertEquals($output[0], $this->git->getActiveBranchName());
    }

    /**
     * @test
     */
    public function gets_the_last_tag_on_current_branch()
    {
        exec('git describe --tags --abbrev=0 HEAD', $output);
        $this->assertEquals($output[0], $this->git->getLastTagOnBranch());
    }

    /**
     * @test
     * @dataProvider repoUrlProvider
     */
    public function gets_the_repository_name($repo)
    {
        $return = <<<EOT
* remote origin
  Fetch URL: {$repo}
  Push  URL: {$repo}
  HEAD branch: (not queried)
  Remote branches: (status not queried)
    master
  Local branches configured for 'git pull':
    master                             merges with remote master
  Local ref configured for 'git push' (status not queried):
    (matching) pushes to (matching)
EOT;

        $this->processHelper
            ->expects($this->any())
            ->method('runCommand')
            ->will($this->returnValue($return))
        ;

        $this->assertEquals('gush', $this->unitGit->getRepoName());
    }

    /**
     * @test
     * @dataProvider repoUrlProvider
     */
    public function gets_vendor_name_for_repository($repo)
    {
        $return = <<<EOT
* remote origin
  Fetch URL: {$repo}
  Push  URL: {$repo}
  HEAD branch: (not queried)
  Remote branches: (status not queried)
    master
  Local branches configured for 'git pull':
    master                             merges with remote master
  Local ref configured for 'git push' (status not queried):
    (matching) pushes to (matching)
EOT;

        $this->processHelper
            ->expects($this->any())
            ->method('runCommand')
            ->will($this->returnValue($return))
        ;

        $this->assertEquals(getenv('GIT_VENDOR_NAME'), $this->unitGit->getVendorName());
    }

    /**
     * @test
     */
    public function lists_files()
    {
        // Smoke test for a real listFiles
        $res = $this->git->listFiles();
        $this->assertGreaterThan(50, $res);
    }

    /**
     * @test
     */
    public function merges_remote_branch_in_clean_wc()
    {
        $base = 'master';
        $baseRemote = $sourceRemote = 'origin';
        $sourceBranch = 'amazing-feature';
        $tmpName = $this->realFsHelper->newTempFilename();
        $hash = '8ae59958a2632018275b8db9590e9a79331030cb';
        $message = "Black-box testing 123\n\n\nAah!";

        $processHelper = $this->prophet->prophesize('Gush\Helper\ProcessHelper');
        $this->unitGit = new GitHelper($processHelper->reveal(), $this->filesystemHelper->reveal());

        $this->filesystemHelper->newTempFilename()->willReturn($tmpName);

        $processHelper->runCommand('git config --local --get remote.origin.url', true)->willReturn(true);
        $processHelper->runCommand('git status --porcelain --untracked-files=no')->willReturn("\n");
        $processHelper->runCommand('git rev-parse --abbrev-ref HEAD')->willReturn("master");
        $processHelper->runCommand(['git', 'checkout', 'master'])->shouldBeCalled();
        $processHelper->runCommands(
            [
                [
                    'line' => 'git remote update',
                    'allow_failures' => false,
                ],
                [
                    'line' => 'git checkout '.$base,
                    'allow_failures' => false,
                ],
                [
                    'line' => 'git pull --ff-only',
                    'allow_failures' => false,
                ],
                [
                    'line' => ['git', 'merge', '--no-ff', '--no-commit', $sourceRemote.'/'.$sourceBranch],
{'fraction_non_alphanumeric': 0.14393939393939395, 'fraction_numerical': 0.008477633477633478, 'mean_word_length': 2.2132715155027527, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 157, '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': '6515747', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3226, 'n_words': 667}