Skip to content

Commit

Permalink
Fix adding a binary file with a different content that the existing f…
Browse files Browse the repository at this point in the history
…ile (#138)

When adding a binary file with a given content, the content was wrongly discarded in favour of the
file contents if the file existed.
  • Loading branch information
theofidry authored Apr 22, 2018
1 parent 205641c commit 2a3534e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,7 @@ public function addFile(string $file, string $contents = null, bool $binary = fa
}

if ($binary) {
true === file_exists($file)
? $this->phar->addFile($file, $local)
: $this->phar->addFromString($local, $contents)
;
$this->phar->addFromString($local, $contents);
} else {
$processedContents = self::compactContents(
$this->compactors,
Expand Down
40 changes: 39 additions & 1 deletion tests/BoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function test_it_can_add_a_file_to_the_phar(): void
$this->assertSame($expectedContents, $actualContents);
}

public function test_it_can_add_a_non_exitent_file_with_contents_to_the_phar(): void
public function test_it_can_add_a_non_existent_file_with_contents_to_the_phar(): void
{
$file = 'foo';
$contents = 'test';
Expand All @@ -126,6 +126,25 @@ public function test_it_can_add_a_non_exitent_file_with_contents_to_the_phar():
$this->assertSame($expectedContents, $actualContents);
}

public function test_it_can_add_an_existent_file_with_contents_to_the_phar(): void
{
$file = 'foo';
$contents = 'test';

file_put_contents($file, 'tset');

$this->box->addFile($file, $contents);

$expectedContents = $contents;
$expectedPharPath = 'phar://test.phar/'.$file;

$this->assertFileExists($expectedPharPath);

$actualContents = file_get_contents($expectedPharPath);

$this->assertSame($expectedContents, $actualContents);
}

public function test_it_can_add_a_non_existent_bin_file_with_contents_to_the_phar(): void
{
$file = 'foo';
Expand All @@ -143,6 +162,25 @@ public function test_it_can_add_a_non_existent_bin_file_with_contents_to_the_pha
$this->assertSame($expectedContents, $actualContents);
}

public function test_it_can_add_an_existent_bin_file_with_contents_to_the_phar(): void
{
$file = 'foo';
$contents = 'test';

file_put_contents($file, 'tset');

$this->box->addFile($file, $contents, true);

$expectedContents = $contents;
$expectedPharPath = 'phar://test.phar/'.$file;

$this->assertFileExists($expectedPharPath);

$actualContents = file_get_contents($expectedPharPath);

$this->assertSame($expectedContents, $actualContents);
}

public function test_it_can_add_a_file_with_absolute_path_to_the_phar(): void
{
$relativePath = 'path-to/foo';
Expand Down

0 comments on commit 2a3534e

Please sign in to comment.