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

test: Refactor ExtractTest #963

Merged
merged 2 commits into from
Mar 30, 2023
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
127 changes: 37 additions & 90 deletions tests/Console/Command/ExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use KevinGH\Box\Pharaoh\InvalidPhar;
use KevinGH\Box\Test\CommandTestCase;
use KevinGH\Box\Test\RequiresPharReadonlyOff;
use Phar;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -52,10 +51,13 @@ protected function getCommand(): Command
return new Extract();
}

public function test_it_can_extract_a_phar(): void
{
$pharPath = self::FIXTURES.'/simple-phar.phar';

/**
* @dataProvider pharProvider
*/
public function test_it_can_extract_a_phar(
string $pharPath,
array $expectedFiles,
): void {
$this->commandTester->execute(
[
'command' => 'extract',
Expand All @@ -64,39 +66,30 @@ public function test_it_can_extract_a_phar(): void
],
);

$expectedFiles = [
'.hidden' => 'baz',
'foo' => 'bar',
];

$actualFiles = $this->collectExtractedFiles();

self::assertEqualsCanonicalizing($expectedFiles, $actualFiles);

$this->assertSameOutput('', ExitCode::SUCCESS);
self::assertEqualsCanonicalizing($expectedFiles, $actualFiles);
}

public function test_it_can_extract_a_phar_without_the_phar_extension(): void
private static function pharProvider(): iterable
{
$pharPath = self::FIXTURES.'/simple-phar';

$this->commandTester->execute(
[
'command' => 'extract',
'phar' => $pharPath,
'output' => $this->tmp,
],
);
$pharPath = self::FIXTURES.'/simple-phar.phar';

$expectedFiles = [
$expectedSimplePharFiles = [
'.hidden' => 'baz',
'foo' => 'bar',
];

$actualFiles = $this->collectExtractedFiles();
yield 'simple PHAR' => [
$pharPath,
$expectedSimplePharFiles,
];

$this->assertSameOutput('', ExitCode::SUCCESS);
self::assertEqualsCanonicalizing($expectedFiles, $actualFiles);
yield 'simple PHAR without the PHAR extension' => [
self::FIXTURES.'/simple-phar',
$expectedSimplePharFiles,
];
}

public function test_it_can_extract_a_compressed_phar(): void
Expand All @@ -122,10 +115,12 @@ public function test_it_can_extract_a_compressed_phar(): void
self::assertEqualsCanonicalizing($expectedFiles, $actualFiles);
}

public function test_it_cannot_extract_an_invalid_phar(): void
{
$pharPath = self::FIXTURES.'/invalid.phar';

/**
* @dataProvider invalidPharPath
*/
public function test_it_cannot_extract_an_invalid_phar(
string $pharPath,
): void {
$this->commandTester->execute(
[
'command' => 'extract',
Expand All @@ -134,12 +129,6 @@ public function test_it_cannot_extract_an_invalid_phar(): void
],
);

$expectedFiles = [];

$actualFiles = $this->collectExtractedFiles();

self::assertEqualsCanonicalizing($expectedFiles, $actualFiles);

$expectedOutput = <<<'OUTPUT'

[ERROR] The given file is not a valid PHAR.
Expand All @@ -148,6 +137,18 @@ public function test_it_cannot_extract_an_invalid_phar(): void
OUTPUT;

$this->assertSameOutput($expectedOutput, ExitCode::FAILURE);
self::assertSame([], $this->collectExtractedFiles());
}

public static function invalidPharPath(): iterable
{
yield 'not a valid PHAR with the PHAR extension' => [
self::FIXTURES.'/invalid.phar',
];

yield 'not a valid PHAR without the PHAR extension' => [
self::FIXTURES.'/invalid.phar',
];
}

public function test_it_provides_the_original_exception_in_debug_mode_when_cannot_extract_an_invalid_phar(): void
Expand Down Expand Up @@ -179,60 +180,6 @@ public function test_it_provides_the_original_exception_in_debug_mode_when_canno
}
}

public function test_it_cannot_extract_an_invalid_phar_without_extension(): void
{
$pharPath = self::FIXTURES.'/invalid';

$this->commandTester->execute(
[
'command' => 'extract',
'phar' => $pharPath,
'output' => $this->tmp,
],
);

$expectedFiles = [];

$actualFiles = $this->collectExtractedFiles();

self::assertSame($expectedFiles, $actualFiles);

$expectedOutput = <<<'OUTPUT'

[ERROR] The given file is not a valid PHAR.


OUTPUT;

$this->assertSameOutput($expectedOutput, ExitCode::FAILURE);
}

public function test_it_cannot_extract_an_unknown_file(): void
{
$this->commandTester->execute(
[
'command' => 'extract',
'phar' => '/unknown',
'output' => $this->tmp,
],
);

$expectedFiles = [];

$actualFiles = $this->collectExtractedFiles();

self::assertSame($expectedFiles, $actualFiles);

$expectedOutput = <<<'OUTPUT'

[ERROR] The file "/unknown" could not be found.


OUTPUT;

$this->assertSameOutput($expectedOutput, ExitCode::FAILURE);
}

/**
* @return array<string,string>
*/
Expand Down