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

Add tests for Phar file #515

Merged
merged 4 commits into from
Mar 5, 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
7 changes: 7 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,12 @@ jobs:
if: ${{ matrix.dependencies == 'locked' }}
run: "composer install --no-interaction --no-progress --no-suggest"

- name: "Compile PHAR"
run: |
php -d phar.readonly=0 vendor/bin/phing phar-build
# We re-run 'composer install' here as phing runs 'composer install --no-dev'.
# Using 'install' (not 'update' with flags) here is correct as the previous update re-wrote wrote the lock file.
composer --no-interaction install --no-progress

- name: "Tests"
run: "vendor/bin/phpunit --coverage-clover=clover.xml --coverage-text"
96 changes: 96 additions & 0 deletions test/ComposerRequireCheckerTest/PharTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace ComposerRequireCheckerTest;

use PHPUnit\Framework\TestCase;

use function chdir;
use function dirname;
use function escapeshellarg;
use function exec;
use function getcwd;
use function implode;
use function realpath;
use function sprintf;

use const DIRECTORY_SEPARATOR;
use const PHP_BINARY;

final class PharTest extends TestCase
{
private string $bin;
private string $oldWorkingDirectory;

protected function setUp(): void
{
$phar = realpath(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'build' . DIRECTORY_SEPARATOR . 'composer-require-checker.phar');

if ($phar === false) {
$this->markTestSkipped('Compiled PHAR not found');
}

$this->oldWorkingDirectory = getcwd();
$this->bin = PHP_BINARY . ' ' . escapeshellarg($phar);
}

protected function tearDown(): void
{
if ($this->oldWorkingDirectory === getcwd()) {
return;
}

chdir($this->oldWorkingDirectory);
}

public function testVersion(): void
{
$command = $this->bin . ' --version';
exec($command, $output, $return);
$this->assertStringContainsString('ComposerRequireChecker', implode("\n", $output));
$this->assertSame(0, $return);
}

public function testSuccess(): void
{
exec($this->bin, $output, $return);
$this->assertSame(0, $return);
}

public function testUnknownSymbols(): void
{
$path = __DIR__ . '/../fixtures/unknownSymbols/composer.json';
exec(sprintf('%s check %s 2>&1', $this->bin, $path), $output, $return);
$this->assertStringContainsString('The following 6 unknown symbols were found', implode("\n", $output));
$this->assertNotEquals(0, $return);
}

public function testInvalidConfiguration(): void
{
$path = __DIR__ . '/../fixtures/validJson.json';
exec(sprintf('%s check %s 2>&1', $this->bin, $path), $output, $return);
$this->assertStringContainsString('dependencies have not been installed', implode("\n", $output));
$this->assertNotEquals(0, $return);
}

public function testDefaultConfiguration(): void
{
chdir(__DIR__ . '/../fixtures/defaultConfigPath');
exec(sprintf('%s check 2>&1', $this->bin), $output, $return);
$output = implode("\n", $output);

$this->assertMatchesRegularExpression('/The following [12] unknown symbols were found/', $output);
$this->assertMatchesRegularExpression('/Composer\\\\InstalledVersions/', $output);
$this->assertNotEquals(0, $return);
}

public function testMissingCustomConfiguration(): void
{
chdir(__DIR__ . '/../fixtures/noUnknownComposerSymbol');
exec(sprintf('%s check --config-file=%s 2>&1', $this->bin, 'no-such-file'), $output, $return);

$this->assertStringContainsString('Configuration file [no-such-file] does not exist.', implode("\n", $output));
$this->assertNotEquals(0, $return);
}
}
Loading