Skip to content

Commit

Permalink
Merge pull request #515 from fredden/test-compiled-phar
Browse files Browse the repository at this point in the history
Add tests for Phar file
  • Loading branch information
Ocramius authored Mar 5, 2024
2 parents 522ba47 + 036708d commit 4ad3678
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
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);
}
}

0 comments on commit 4ad3678

Please sign in to comment.