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

Allow to build an empty PHAR #232

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
6 changes: 6 additions & 0 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public function endBuffering(bool $dumpAutoload): void
$tmp = make_tmp_dir('box', __CLASS__);
chdir($tmp);

if ([] === $this->bufferedFiles) {
$this->bufferedFiles = [
'.box_empty' => 'A PHAR cannot be empty so Box adds this file to ensure the PHAR is created still.',
];
}

try {
foreach ($this->bufferedFiles as $file => $contents) {
dump_file($file, $contents);
Expand Down
76 changes: 76 additions & 0 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,82 @@ public function test_it_can_build_a_PHAR_without_a_main_script(): void
);
}

public function test_it_can_build_an_empty_PHAR(): void
{
mirror(self::FIXTURES_DIR.'/dir004', $this->tmp);

dump_file(
'box.json',
<<<'JSON'
{
"stub": "stub.php",
"main": false
}
JSON

);

$commandTester = $this->getCommandTester();
$commandTester->execute(
['command' => 'compile'],
[
'interactive' => false,
'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
]
);

$expected = <<<OUTPUT

____
/ __ )____ _ __
/ __ / __ \| |/_/
/ /_/ / /_/ /> <
/_____/\____/_/|_|


Box (repo)


// Loading the configuration file "/path/to/box.json.dist".

* Building the PHAR "/path/to/tmp/test.phar"
? No compactor to register
? No main script path configured
? Adding binary files
> No file found
? Adding files
> No file found
? Using stub file: /path/to/tmp/stub.php
? No compression
* Done.

// PHAR: 1 file (100B)
// You can inspect the generated PHAR with the "info" command.

// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s


OUTPUT;

$actual = $this->normalizeDisplay($commandTester->getDisplay(true));

$this->assertSame($expected, $actual);

$this->assertSame(
'Hello!',
exec('php test.phar'),
'Expected PHAR to be executable'
);

$expectedFiles = [
'/.box_empty',
];

$actualFiles = $this->retrievePharFiles(new Phar('test.phar'));

$this->assertSame($expectedFiles, $actualFiles);
}

public function test_it_can_build_a_PHAR_with_compressed_code(): void
{
mirror(self::FIXTURES_DIR.'/dir006', $this->tmp);
Expand Down