Skip to content

Commit

Permalink
refactor: Refactor the extract command to leverage the PharFactory se…
Browse files Browse the repository at this point in the history
…rvice (#974)
  • Loading branch information
theofidry authored Apr 1, 2023
1 parent 72e5cef commit 3d0b441
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 55 deletions.
Binary file removed fixtures/extract/corrupted.phar
Binary file not shown.
Binary file removed fixtures/extract/incorrect-key-openssl.phar
Binary file not shown.
Empty file.
Empty file removed fixtures/extract/invalid
Empty file.
Empty file removed fixtures/extract/invalid.phar
Empty file.
Binary file removed fixtures/extract/openssl-no-pubkey.phar
Binary file not shown.
15 changes: 2 additions & 13 deletions src/Console/Command/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
use Fidry\Console\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use KevinGH\Box\Pharaoh\InvalidPhar;
use KevinGH\Box\Phar\PharFactory;
use KevinGH\Box\Pharaoh\Pharaoh;
use ParagonIE\ConstantTime\Hex;
use Phar;
use PharData;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Throwable;
use UnexpectedValueException;
use function file_exists;
use function KevinGH\Box\check_php_settings;
use function KevinGH\Box\FileSystem\copy;
Expand Down Expand Up @@ -145,7 +143,7 @@ private static function dumpPhar(string $file, string $tmpDir): string
copy($pubkey, $tmpPubkey, true);
}

$phar = self::createPhar($file, $tmpFile);
$phar = PharFactory::create($tmpFile);

$phar->extractTo($tmpDir);
} catch (Throwable $throwable) {
Expand Down Expand Up @@ -186,13 +184,4 @@ private static function getExtension(string $file): string

return '' === $extension ? '.phar' : $extension;
}

private static function createPhar(string $file, string $tmpFile): Phar|PharData
{
try {
return new Phar($tmpFile);
} catch (UnexpectedValueException $cannotCreatePhar) {
throw InvalidPhar::forPhar($file, $cannotCreatePhar);
}
}
}
46 changes: 4 additions & 42 deletions tests/Console/Command/ExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,8 @@ public static function confirmationQuestionProvider(): iterable
/**
* @dataProvider invalidPharPath
*/
public function test_it_cannot_extract_an_invalid_phar(
string $pharPath,
string $exceptionClassName,
string $expectedExceptionMessage,
): void {
public function test_it_cannot_extract_an_invalid_phar(string $pharPath): void
{
try {
$this->commandTester->execute(
[
Expand All @@ -228,48 +225,13 @@ public function test_it_cannot_extract_an_invalid_phar(
// Continue
}

self::assertSame(
$exceptionClassName,
$exception::class,
);
self::assertMatchesRegularExpression(
$expectedExceptionMessage,
$exception->getMessage(),
);

self::assertSame([], $this->collectExtractedFiles());
}

public static function invalidPharPath(): iterable
{
yield 'not a valid PHAR with the PHAR extension' => [
self::FIXTURES.'/invalid.phar',
InvalidPhar::class,
'/^Could not create a Phar instance for the file/',
];

yield 'not a valid PHAR without the PHAR extension' => [
self::FIXTURES.'/invalid',
InvalidPhar::class,
'/^Could not create a Phar instance for the file/',
];

yield 'corrupted PHAR (was valid; got tempered with)' => [
self::FIXTURES.'/corrupted.phar',
InvalidPhar::class,
'/^Could not create a Phar instance for the file/',
];

yield 'OpenSSL signed PHAR without a pubkey' => [
self::FIXTURES.'/openssl-no-pubkey.phar',
InvalidPhar::class,
'/^Could not create a Phar instance for the file/',
];

yield 'OpenSSL signed PHAR with incorrect pubkey' => [
self::FIXTURES.'/incorrect-key-openssl.phar',
InvalidPhar::class,
'/^Could not create a Phar instance for the file/',
yield 'not a valid PHAR' => [
self::FIXTURES.'/../phar/empty-pdf.pdf',
];
}

Expand Down

0 comments on commit 3d0b441

Please sign in to comment.