Skip to content

Commit

Permalink
test: Refactor ExtractTest (#963)
Browse files Browse the repository at this point in the history
Refactor `ExtractTest` to better accommodate the upcoming changes.
  • Loading branch information
theofidry authored Mar 30, 2023
1 parent 65d957a commit c0c7e88
Showing 1 changed file with 37 additions and 90 deletions.
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

0 comments on commit c0c7e88

Please sign in to comment.