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

refactor: Refactor the extract command to leverage the PharFactory service #974

Merged
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
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