Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Add tests for console application #615

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions tests/src/Console/ApplicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Composer package "cpsit/frontend-asset-handler".
*
* Copyright (C) 2024 Elias Häußler <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace CPSIT\FrontendAssetHandler\Tests\Console;

use CPSIT\FrontendAssetHandler as Src;
use Generator;
use PHPUnit\Framework;
use Symfony\Component\Console\Tester\ApplicationTester;

use function dirname;

/**
* ApplicationTest.
*
* @author Elias Häußler <[email protected]>
* @license GPL-3.0-or-later
*/
#[Framework\Attributes\CoversClass(Src\Console\Application::class)]
final class ApplicationTest extends Framework\TestCase
{
private ApplicationTester $applicationTester;

public function setUp(): void
{
$application = new Src\Console\Application();
$application->setAutoExit(false);

$this->applicationTester = new ApplicationTester($application);
}

#[Framework\Attributes\Test]
#[Framework\Attributes\DataProvider('applicationAddsConfigOptionToAllAvailableCommandsDataProvider')]
public function applicationAddsConfigOptionToAllAvailableCommands(string $command): void
{
$this->applicationTester->run([$command, '--help']);

self::assertStringContainsString('--config', $this->applicationTester->getDisplay());
}

#[Framework\Attributes\Test]
public function applicationAddsCurrentVersionOutputToAssetCommands(): void
{
$this->applicationTester->run([
'command' => 'config',
'--config' => dirname(__DIR__).'/Fixtures/JsonFiles/assets.json',
]);

self::assertStringContainsString(
'Running Frontend Asset Handler dev-',
$this->applicationTester->getDisplay(),
);
}

#[Framework\Attributes\Test]
public function applicationAddsClearCacheCommand(): void
{
$this->applicationTester->run(['clear-cache']);

self::assertStringContainsString(
'Container cache was successfully flushed.',
$this->applicationTester->getDisplay(),
);
}

#[Framework\Attributes\Test]
public function applicationAddsAssetCommands(): void
{
$this->applicationTester->run([
'command' => 'config',
'--config' => dirname(__DIR__).'/Fixtures/JsonFiles/assets.json',
]);

self::assertSame(2, $this->applicationTester->getStatusCode());
self::assertStringContainsString(
'The configuration path must not be empty.',
$this->applicationTester->getDisplay(),
);
}

#[Framework\Attributes\Test]
public function listCommandImplicitlyAddsAssetCommands(): void
{
$this->applicationTester->run(['list']);

$output = $this->applicationTester->getDisplay();

self::assertStringContainsString('clear-cache', $output);
self::assertStringContainsString('config', $output);
self::assertStringContainsString('fetch', $output);
self::assertStringContainsString('init', $output);
self::assertStringContainsString('inspect', $output);
}

#[Framework\Attributes\Test]
public function commandHelpImplicitlyAddsCommand(): void
{
$this->applicationTester->run(['fetch', '--help']);

self::assertStringContainsString('fetch [options] [--] [<branch>]', $this->applicationTester->getDisplay());
}

/**
* @return Generator<string, array{string}>
*/
public static function applicationAddsConfigOptionToAllAvailableCommandsDataProvider(): Generator
{
yield 'clear-cache' => ['clear-cache'];
yield 'config' => ['config'];
yield 'fetch' => ['fetch'];
yield 'init' => ['init'];
yield 'inspect' => ['inspect'];
}
}