<?php

declare(strict_types=1);

namespace Aeon\Automation\Tests\Integration\Console\Command;

use Aeon\Automation\Console\AeonApplication;
use Aeon\Automation\Console\Command\ChangelogGet;
use Aeon\Automation\Tests\Double\HttpRequestStub;
use Aeon\Automation\Tests\Integration\Console\CommandTestCase;
use Aeon\Automation\Tests\Mother\ResponseMother;
use Github\Client;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Tester\CommandTester;

final class ChangelogGetTest extends CommandTestCase
{
    public function test_changelog_get() : void
    {
        $client = Client::createWithHttpClient($httpClient = $this->httpClient(
            new HttpRequestStub('GET', '/repos/aeon-php/automation/contents/CHANGELOG.md', ResponseMother::jsonSuccess([
                'content' => \base64_encode($changelog = <<<'CHANGELOG'
## [Unreleased] - 2021-01-01

### Changed
- [#3](http://api.github.com) - **Release 1.1.0 Title - 3** - [@user_login](http//github.com/user_login)
- [#2](http://api.github.com) - **Release 1.1.0 Title - 2** - [@user_login](http//github.com/user_login)
- [#1](http://api.github.com) - **Release 1.1.0 Title - 1** - [@user_login](http//github.com/user_login)

Generated by [Automation](https://github.com/aeon-php/automation)
CHANGELOG),
            ])),
        ));

        $command = new ChangelogGet(\getenv('AUTOMATION_ROOT_DIR'));
        $command->setGithub($client);
        $command->setHttpCache(new ArrayAdapter());
        $command->setGitHubCache(new ArrayAdapter());

        $application = new AeonApplication();
        $application->add($command);

        $commandTester = new CommandTester($application->get(ChangelogGet::getDefaultName()));

        $commandTester->setInputs(['verbosity' => ConsoleOutput::VERBOSITY_VERY_VERBOSE]);

        $commandTester->execute(
            ['project' => 'aeon-php/automation'],
            ['verbosity' => ConsoleOutput::VERBOSITY_VERBOSE]
        );

        $this->assertStringContainsString('Changelog - Get', $commandTester->getDisplay());

        $this->assertStringContainsString($changelog, $commandTester->getDisplay());

        $this->assertSame(0, $commandTester->getStatusCode());
    }

    public function test_changelog_sha1_hash() : void
    {
        $client = Client::createWithHttpClient($httpClient = $this->httpClient(
            new HttpRequestStub('GET', '/repos/aeon-php/automation/contents/CHANGELOG.md', ResponseMother::jsonSuccess([
                'content' => \base64_encode($changelog = <<<'CHANGELOG'
## [Unreleased] - 2021-01-01

### Changed
- [#3](http://api.github.com) - **Release 1.1.0 Title - 3** - [@user_login](http//github.com/user_login)
- [#2](http://api.github.com) - **Release 1.1.0 Title - 2** - [@user_login](http//github.com/user_login)
- [#1](http://api.github.com) - **Release 1.1.0 Title - 1** - [@user_login](http//github.com/user_login)

Generated by [Automation](https://github.com/aeon-php/automation)
CHANGELOG),
            ])),
        ));

        $command = new ChangelogGet(\getenv('AUTOMATION_ROOT_DIR'));
        $command->setGithub($client);
        $command->setHttpCache(new ArrayAdapter());
        $command->setGitHubCache(new ArrayAdapter());

        $application = new AeonApplication();
        $application->add($command);

        $commandTester = new CommandTester($application->get(ChangelogGet::getDefaultName()));

        $commandTester->setInputs(['verbosity' => ConsoleOutput::VERBOSITY_VERY_VERBOSE]);

        $commandTester->execute(
            ['project' => 'aeon-php/automation', '--sha1-hash' => true],
            ['verbosity' => ConsoleOutput::VERBOSITY_VERBOSE]
        );

        $this->assertStringContainsString('Changelog - Get', $commandTester->getDisplay());

        $this->assertStringContainsString(\sha1($changelog), $commandTester->getDisplay());

        $this->assertSame(0, $commandTester->getStatusCode());
    }
}
