-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
197 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the box project. | ||
* | ||
* (c) Kevin Herrera <[email protected]> | ||
* Théo Fidry <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace KevinGH\Box\Console\Command\Info; | ||
|
||
use Fidry\Console\Command\Command; | ||
use Fidry\Console\Command\Configuration; | ||
use Fidry\Console\ExitCode; | ||
use Fidry\Console\IO; | ||
use KevinGH\Box\Phar\PharInfo; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Filesystem\Path; | ||
use function realpath; | ||
use function sprintf; | ||
|
||
/** | ||
* @private | ||
*/ | ||
final class Signature implements Command | ||
{ | ||
private const PHAR_ARG = 'phar'; | ||
|
||
public function getConfiguration(): Configuration | ||
{ | ||
return new Configuration( | ||
'info:signature', | ||
'🔍 Displays the hash of the signature', | ||
<<<'HELP' | ||
The <info>%command.name%</info> command will display the hash of the signature. This may | ||
be useful for some external tools that wishes to act base on the signature, e.g. to invalidate | ||
some cache or compare signatures. | ||
HELP, | ||
[ | ||
new InputArgument( | ||
self::PHAR_ARG, | ||
InputArgument::REQUIRED, | ||
'The PHAR file.', | ||
), | ||
], | ||
); | ||
} | ||
|
||
public function execute(IO $io): int | ||
{ | ||
$file = $io->getTypedArgument(self::PHAR_ARG)->asNonEmptyString(); | ||
|
||
$file = Path::canonicalize($file); | ||
$fileRealPath = realpath($file); | ||
|
||
$pharInfo = new PharInfo($file); | ||
$signature = $pharInfo->getSignature(); | ||
|
||
if (null === $signature) { | ||
$io->error( | ||
sprintf( | ||
'The file "%s" is not a PHAR.', | ||
$file, | ||
), | ||
); | ||
|
||
return ExitCode::FAILURE; | ||
} | ||
|
||
$io->writeln($signature['hash']); | ||
|
||
return ExitCode::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the box project. | ||
* | ||
* (c) Kevin Herrera <[email protected]> | ||
* Théo Fidry <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace KevinGH\Box\Console\Command\Info; | ||
|
||
use Fidry\Console\Command\Command; | ||
use Fidry\Console\ExitCode; | ||
use Fidry\Console\Test\OutputAssertions; | ||
use KevinGH\Box\Phar\InvalidPhar; | ||
use KevinGH\Box\Test\CommandTestCase; | ||
use Symfony\Component\Filesystem\Path; | ||
|
||
/** | ||
* @covers \KevinGH\Box\Console\Command\Info\Signature | ||
* | ||
* @internal | ||
*/ | ||
class SignatureTest extends CommandTestCase | ||
{ | ||
private const FIXTURES = __DIR__.'/../../../../fixtures/phar'; | ||
|
||
protected function getCommand(): Command | ||
{ | ||
return new Signature(); | ||
} | ||
|
||
public function test_it_provides_the_phar_signature(): void | ||
{ | ||
$pharPath = self::FIXTURES.'/simple-phar.phar'; | ||
|
||
$this->commandTester->execute([ | ||
'command' => 'info:signature', | ||
'phar' => $pharPath, | ||
]); | ||
|
||
OutputAssertions::assertSameOutput( | ||
<<<'EOF' | ||
55AE0CCD6D3A74BE41E19CD070A655A73FEAEF8342084A0801954943FBF219ED | ||
|
||
EOF, | ||
ExitCode::SUCCESS, | ||
$this->commandTester, | ||
); | ||
} | ||
|
||
public function test_it_cannot_provide_info_about_a_non_existent_phar(): void | ||
{ | ||
$file = self::FIXTURES.'/foo'; | ||
|
||
$this->expectException(InvalidPhar::class); | ||
|
||
$this->commandTester->execute( | ||
[ | ||
'command' => 'info:signature', | ||
'phar' => $file, | ||
], | ||
); | ||
} | ||
|
||
public function test_it_cannot_provide_info_about_an_non_phar_archive(): void | ||
{ | ||
$file = self::FIXTURES.'/simple.zip'; | ||
$expectedFilePath = Path::canonicalize($file); | ||
|
||
$this->commandTester->execute( | ||
[ | ||
'command' => 'info:signature', | ||
'phar' => $file, | ||
], | ||
); | ||
|
||
OutputAssertions::assertSameOutput( | ||
<<<EOF | ||
[ERROR] The file "{$expectedFilePath}" is | ||
not a PHAR. | ||
EOF, | ||
ExitCode::FAILURE, | ||
$this->commandTester, | ||
); | ||
} | ||
|
||
public function test_it_cannot_provide_info_about_an_invalid_phar(): void | ||
{ | ||
$file = self::FIXTURES.'/empty-pdf.pdf'; | ||
|
||
$this->expectException(InvalidPhar::class); | ||
|
||
$this->commandTester->execute( | ||
[ | ||
'command' => 'info:signature', | ||
'phar' => $file, | ||
], | ||
); | ||
} | ||
} |