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

[#97] Updated tests to use the application tester. #98

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use DrevOps\GitArtifact\Commands\ArtifactCommand;
use Symfony\Component\Console\Application;

// @codeCoverageIgnoreStart
$application = new Application();

$command = new ArtifactCommand();
$application->add($command);
$application->setDefaultCommand((string) $command->getName(), TRUE);

$application->run();
// @codeCoverageIgnoreEnd
159 changes: 36 additions & 123 deletions tests/phpunit/Functional/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
namespace DrevOps\GitArtifact\Tests\Functional;

use DrevOps\GitArtifact\Commands\ArtifactCommand;
use DrevOps\GitArtifact\Tests\Traits\ConsoleTrait;
use DrevOps\GitArtifact\Tests\Traits\FixtureTrait;
use DrevOps\GitArtifact\Tests\Traits\GitTrait;
use DrevOps\GitArtifact\Tests\Unit\AbstractUnitTestCase;
use DrevOps\GitArtifact\Traits\FilesystemTrait;
use PHPUnit\Framework\AssertionFailedError;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;

abstract class AbstractFunctionalTestCase extends AbstractUnitTestCase {

use ConsoleTrait;
use FilesystemTrait;
use FixtureTrait;
use GitTrait;
Expand All @@ -36,55 +34,31 @@ abstract class AbstractFunctionalTestCase extends AbstractUnitTestCase {
protected $dst;

/**
* Artifact command.
*/
protected ArtifactCommand $command;

/**
* Fixture directory.
*
* @var string
* Remote name.
*/
protected $fixtureDir;
protected string $remoteName;

/**
* Current branch.
*
* @var string
*/
protected $currentBranch;
protected string $currentBranch;

/**
* Artifact branch.
*
* @var string
*/
protected $artifactBranch;

/**
* Remote name.
*
* @var string
*/
protected $remoteName;
protected string $artifactBranch;

/**
* Mode in which the build will run.
*
* Passed as a value of the --mode option.
*
* @var string
* Mode in which the artifact application will run.
*/
protected $mode;
protected ?string $mode = NULL;

/**
* Current timestamp to run commands with.
*
* Used for generating internal tokens that could be based on time.
*
* @var int
*/
protected $now;
protected int $now;

/**
* {@inheritdoc}
Expand All @@ -94,7 +68,8 @@ protected function setUp(): void {

$this->fs = new Filesystem();

$this->fixtureDir = $this->fsGetAbsolutePath(sys_get_temp_dir() . DIRECTORY_SEPARATOR . date('U') . DIRECTORY_SEPARATOR . 'git_artifact');
$this->fixtureInit('git_artifact');
$this->fixtureDir = $this->fsGetAbsolutePath($this->fixtureDir);

$this->src = $this->fsGetAbsolutePath($this->fixtureDir . DIRECTORY_SEPARATOR . 'src');
$this->gitInitRepo($this->src);
Expand All @@ -109,6 +84,8 @@ protected function setUp(): void {
$this->currentBranch = $this->gitGetGlobalDefaultBranch();
$this->artifactBranch = $this->currentBranch . '-artifact';
$this->remoteName = 'dst';

$this->consoleInitApplicationTester(ArtifactCommand::class);
}

/**
Expand All @@ -133,9 +110,10 @@ protected function tearDown(): void {
* @return string
* Command output.
*/
protected function assertCommandSuccess(?array $args = [], string $branch = 'testbranch', string $commit = 'Deployment commit'): string {
protected function assertArtifactCommandSuccess(?array $args = [], string $branch = 'testbranch', string $commit = 'Deployment commit'): string {
$args += ['--branch' => 'testbranch'];
$output = $this->runCommand($args);

$output = $this->runArtifactCommand($args);

$this->assertStringNotContainsString('[error]', $output);
$this->assertStringContainsString(sprintf('Pushed branch "%s" with commit message "%s"', $branch, $commit), $output);
Expand All @@ -158,9 +136,10 @@ protected function assertCommandSuccess(?array $args = [], string $branch = 'tes
* @return string
* Command output.
*/
protected function assertCommandFailure(?array $args = [], string $commit = 'Deployment commit'): string {
protected function assertArtifactCommandFailure(?array $args = [], string $commit = 'Deployment commit'): string {
$args += ['--branch' => 'testbranch'];
$output = $this->runCommand($args, TRUE);

$output = $this->runArtifactCommand($args, TRUE);

$this->assertStringNotContainsString(sprintf('Pushed branch "%s" with commit message "%s"', $args['--branch'], $commit), $output);
$this->assertStringNotContainsString('Deployment finished successfully.', $output);
Expand All @@ -173,100 +152,34 @@ protected function assertCommandFailure(?array $args = [], string $commit = 'Dep
* Run artifact build.
*
* @param array $args
* Additional arguments or options as an associative array.
* Additional arguments or options as an associative array. If NULL, no
* additional arguments are passed.
* @param bool $expect_fail
* Expect on fail.
*
* @return string
* Output string.
*/
protected function runCommand(?array $args = [], bool $expect_fail = FALSE): string {
try {

if (is_null($args)) {
$input = [];
}
else {
$input = [
'--root' => $this->fixtureDir,
'--now' => $this->now,
'--src' => $this->src,
'remote' => $this->dst,
];

if ($this->mode) {
$input['--mode'] = $this->mode;
}

$input += $args;
}

$this->runExecute(ArtifactCommand::class, $input);
$output = $this->commandTester->getDisplay();

if ($this->commandTester->getStatusCode() !== 0) {
throw new \Exception(sprintf("Command exited with non-zero code.\nThe output was:\n%s\nThe error output was:\n%s", $this->commandTester->getDisplay(), $this->commandTester->getErrorOutput()));
}

if ($expect_fail) {
throw new AssertionFailedError(sprintf("Command exited successfully but should not.\nThe output was:\n%s\nThe error output was:\n%s", $this->commandTester->getDisplay(), $this->commandTester->getErrorOutput()));
}

}
catch (\RuntimeException $exception) {
if (!$expect_fail) {
throw new AssertionFailedError('Command exited with an error:' . PHP_EOL . $exception->getMessage());
}
$output = $exception->getMessage();
protected function runArtifactCommand(?array $args = [], bool $expect_fail = FALSE): string {
if (is_null($args)) {
$input = [];
}
catch (\Exception $exception) {
if (!$expect_fail) {
throw new AssertionFailedError('Command exited with an error:' . PHP_EOL . $exception->getMessage());
else {
$input = [
'--root' => $this->fixtureDir,
'--now' => $this->now,
'--src' => $this->src,
'remote' => $this->dst,
];

if ($this->mode) {
$input['--mode'] = $this->mode;
}
}

return $output;
}

/**
* CommandTester instance.
*
* @var \Symfony\Component\Console\Tester\CommandTester
*/
protected $commandTester;

/**
* Run main() with optional arguments.
*
* @param string|object $object_or_class
* Object or class name.
* @param array<string> $input
* Optional array of input arguments.
* @param array<string, string> $options
* Optional array of options. See CommandTester::execute() for details.
*/
protected function runExecute(string|object $object_or_class, array $input = [], array $options = []): void {
$application = new Application();
/** @var \Symfony\Component\Console\Command\Command $instance */
$instance = is_object($object_or_class) ? $object_or_class : new $object_or_class();
$application->add($instance);

$name = $instance->getName();
if (empty($name)) {
/** @var string $name */
$name = $this->getProtectedValue($instance, 'defaultName');
}

$command = $application->find($name);
$this->commandTester = new CommandTester($command);

$options['capture_stderr_separately'] = TRUE;
if (array_key_exists('-vvv', $input)) {
$options['verbosity'] = ConsoleOutput::VERBOSITY_DEBUG;
unset($input['-vvv']);
$input += $args;
}

$this->commandTester->execute($input, $options);
return $this->consoleApplicationRun($input, [], $expect_fail);
}

}
28 changes: 14 additions & 14 deletions tests/phpunit/Functional/BranchModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ class BranchModeTest extends AbstractFunctionalTestCase {
* {@inheritdoc}
*/
protected function setUp(): void {
$this->mode = 'branch';
$this->mode = ArtifactCommand::MODE_BRANCH;
parent::setUp();
}

public function testBuild(): void {
$this->gitCreateFixtureCommits(2);

$output = $this->assertCommandSuccess();
$output = $this->assertArtifactCommandSuccess();
$this->assertStringContainsString('WARNING! Provided branch name does not have a token', $output);
$this->assertStringContainsString('Mode: branch', $output);
$this->assertStringContainsString('Mode: ' . ArtifactCommand::MODE_BRANCH, $output);
$this->assertStringContainsString('Will push: Yes', $output);

$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']);
Expand All @@ -34,12 +34,12 @@ public function testBuild(): void {
public function testBuildMoreCommitsSameBranch(): void {
$this->gitCreateFixtureCommits(2);

$this->assertCommandSuccess();
$this->assertArtifactCommandSuccess();

$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']);

$this->gitCreateFixtureCommits(3, 2);
$this->assertCommandFailure();
$this->assertArtifactCommandFailure();

// Make sure that broken artifact was not pushed.
$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']);
Expand All @@ -50,7 +50,7 @@ public function testBuildMoreCommits(): void {

$this->now = time() - rand(1, 10 * 60);
$branch1 = 'testbranch-' . date('Y-m-d_H-i-s', $this->now);
$output = $this->assertCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch1);
$output = $this->assertArtifactCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch1);
$this->assertStringContainsString('Remote branch: ' . $branch1, $output);
$this->assertStringNotContainsString('WARNING! Provided branch name does not have a token', $output);

Expand All @@ -60,7 +60,7 @@ public function testBuildMoreCommits(): void {

$this->now = time() - rand(1, 10 * 60);
$branch2 = 'testbranch-' . date('Y-m-d_H-i-s', $this->now);
$output = $this->assertCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch2);
$output = $this->assertArtifactCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch2);
$this->assertStringContainsString('Remote branch: ' . $branch2, $output);
$this->gitAssertFixtureCommits(5, $this->dst, $branch2, ['Deployment commit']);

Expand All @@ -71,7 +71,7 @@ public function testBuildMoreCommits(): void {
public function testCleanupAfterSuccess(): void {
$this->gitCreateFixtureCommits(2);

$this->assertCommandSuccess();
$this->assertArtifactCommandSuccess();
$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']);

$this->gitAssertCurrentBranch($this->src, $this->currentBranch);
Expand All @@ -81,12 +81,12 @@ public function testCleanupAfterSuccess(): void {
public function testCleanupAfterFailure(): void {
$this->gitCreateFixtureCommits(2);

$this->assertCommandSuccess();
$this->assertArtifactCommandSuccess();
$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']);

$this->gitCreateFixtureCommits(3, 2);
// Trigger erroneous build by pushing to the same branch.
$this->assertCommandFailure();
$this->assertArtifactCommandFailure();

$this->gitAssertCurrentBranch($this->src, $this->currentBranch);
$this->gitAssertRemoteNotExists($this->src, $this->remoteName);
Expand All @@ -99,7 +99,7 @@ public function testGitignore(): void {

$this->now = time() - rand(1, 10 * 60);
$branch1 = 'testbranch-' . date('Y-m-d_H-i-s', $this->now);
$this->assertCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch1);
$this->assertArtifactCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch1);

$this->gitAssertFixtureCommits(2, $this->dst, $branch1, ['Deployment commit']);
$this->assertFileDoesNotExist($this->dst . DIRECTORY_SEPARATOR . 'f3');
Expand All @@ -109,7 +109,7 @@ public function testGitignore(): void {
$this->gitCommitAll($this->src, 'Commit number 3');
$this->now = time() - rand(1, 10 * 60);
$branch2 = 'testbranch-' . date('Y-m-d_H-i-s', $this->now);
$this->assertCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch2);
$this->assertArtifactCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch2);

$this->gitAssertFixtureCommits(3, $this->dst, $branch2, ['Deployment commit']);

Expand All @@ -125,7 +125,7 @@ public function testGitignoreCustom(): void {

$this->now = time() - rand(1, 10 * 60);
$branch1 = 'testbranch-' . date('Y-m-d_H-i-s', $this->now);
$this->assertCommandSuccess([
$this->assertArtifactCommandSuccess([
'--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]',
'--gitignore' => $this->src . DIRECTORY_SEPARATOR . 'mygitignore',
], $branch1);
Expand All @@ -139,7 +139,7 @@ public function testGitignoreCustom(): void {
$this->gitCommitAll($this->src, 'Commit number 3');
$this->now = time() - rand(1, 10 * 60);
$branch2 = 'testbranch-' . date('Y-m-d_H-i-s', $this->now);
$this->assertCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch2);
$this->assertArtifactCommandSuccess(['--branch' => 'testbranch-[timestamp:Y-m-d_H-i-s]'], $branch2);

$this->gitAssertFixtureCommits(3, $this->dst, $branch2, ['Deployment commit']);

Expand Down
Loading
Loading