Skip to content

Commit

Permalink
Merge pull request #141 from ThomasWeinert/feature/improved-bat-phar-…
Browse files Browse the repository at this point in the history
…activator

Improved BatPharActivator
  • Loading branch information
theseer authored May 5, 2018
2 parents ed9356e + cd9173f commit a1a4a31
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/shared/file/BatPharActivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ public function __construct($template) {
* @param Filename $linkDestination
*
* @return Filename
* @throws FileNotWritableException
*/
public function activate(Filename $pharLocation, Filename $linkDestination) {
$template = str_replace(self::PHAR_PLACEHOLDER, $pharLocation->asString(), $this->template);
$linkFilename = new Filename($linkDestination->asString() . '.bat');
if (!$linkDestination->getDirectory()->isWritable()) {
throw new FileNotWritableException(sprintf('File %s is not writable.', $linkFilename->asString()));
}
if ((string)$pharLocation->getDirectory() === (string)$linkFilename->getDirectory()) {
$pathToPhar = '%~dp0' . $pharLocation->getRelativePathTo($linkFilename->getDirectory())->asString();
} else {
$pathToPhar = $pharLocation->asString();
}
$template = str_replace(self::PHAR_PLACEHOLDER, $pathToPhar, $this->template);
file_put_contents($linkFilename, $template);

return $linkFilename;
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/shared/file/BatPharActivatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace PharIo\Phive;

use PharIo\FileSystem\Directory;
use PharIo\FileSystem\Filename;
use PHPUnit\Framework\TestCase;

Expand All @@ -16,6 +17,38 @@ public function testCreatesExpectedBatFile() {
$this->assertEquals('foo some.phar bar', file_get_contents($createdFile));
}

public function testCreatesExpectedBatFileWithPath() {
$activator = new BatPharActivator('foo ##PHAR_FILENAME## bar');
$createdFile = $activator->activate(
new Filename(__DIR__ . '/path/some.phar'),
new Filename(__DIR__ . '/some')
);

$this->assertEquals('foo ' . __DIR__ . '/path/some.phar bar', file_get_contents($createdFile));
}

public function testCreatesExpectedBatFileWithPathPlaceholder() {
$activator = new BatPharActivator('foo ##PHAR_FILENAME## bar');
$createdFile = $activator->activate(
new Filename(__DIR__ . '/some.phar'),
new Filename(__DIR__ . '/some')
);

$this->assertEquals('foo %~dp0some.phar bar', file_get_contents($createdFile));
}

public function testDestinationNotWritableExpectingException() {
$directory = $this->createMock(Directory::class);
$directory->expects($this->any())->method('isWritable')->willReturn(FALSE);
$destination = $this->createMock(Filename::class);
$destination->expects($this->any())->method('getDirectory')->willReturn($directory);

$this->expectException(FileNotWritableException::class);

$activator = new BatPharActivator('foo ##PHAR_FILENAME## bar');
$activator->activate(new Filename('some.phar'), $destination);
}

protected function tearDown() {
if (file_exists(__DIR__ . '/some.bat')) {
unlink(__DIR__ . '/some.bat');
Expand Down

0 comments on commit a1a4a31

Please sign in to comment.