Skip to content

Commit

Permalink
Merge branch 'hotfix/noGsFound'
Browse files Browse the repository at this point in the history
  • Loading branch information
heiglandreas committed Apr 5, 2017
2 parents e1c196f + 6254048 commit ecb147c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 16 deletions.
36 changes: 28 additions & 8 deletions src/Ghostscript.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ class Ghostscript
* @var array $supportedMimeTypes
*/
private static $supportedMimeTypes = [
'application/eps',
'application/pdf',
'application/ps',
];
'application/eps',
'application/pdf',
'application/ps',
];

/**
* This property contains the path to the Ghostscript-Application
Expand Down Expand Up @@ -267,15 +267,29 @@ public function __construct()
public static function setGsPath($path = null)
{
if (null === $path) {
$path = exec('which gs', $output);
exec('which gs', $output);
if (! $output) {
throw new \UnexpectedValueException('No Ghostscript-instance found or running on windows. Please provide Path to the Ghostscript-executable');
}
$path = $output[0];
}
if ($path) {
self::$PATH = $path;

if (! $path) {
throw new \UnexpectedValueException('No path found');
}

if (! is_executable($path)) {
throw new \InvalidArgumentException('The given file is not executable');
}

@exec($path . ' -v', $result);
$content = implode("\n", $result);
if (false === stripos($content, 'ghostscript')) {
throw new \InvalidArgumentException('No valid Ghostscript found');
}

self::$PATH = $path;

return self::$PATH;
}

Expand All @@ -286,6 +300,9 @@ public static function setGsPath($path = null)
*/
public static function getGsPath()
{
if (! self::$PATH) {
throw new \InvalidArgumentException('No GS-Path set');
}
return self::$PATH;
}

Expand Down Expand Up @@ -843,4 +860,7 @@ public function setPages($startPage, $endPage = null)
}
}

Ghostscript::setGsPath();
try {
Ghostscript::setGsPath();
} catch (\UnexpectedValueException $e) {
}
64 changes: 56 additions & 8 deletions tests/GhostscriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ public function testForNonEmptyGhostscriptPath()
$this -> assertNotEquals(null, $path);
}

public function testSettingOfGhostscriptPath()
{
Ghostscript::setGsPath();
$this -> assertEquals(exec('which gs'), Ghostscript::getGsPath());
Ghostscript::setGsPath('This/is/a/fake');
$this -> assertEquals('This/is/a/fake', Ghostscript::getGsPath());
}

/**
* @expectedException \InvalidArgumentException
*/
Expand Down Expand Up @@ -218,4 +210,60 @@ public function testSettingPages()
$this->assertAttributeEquals(4, 'pageEnd', $f);
$this->assertEquals(' -dFirstPage=3 -dLastPage=4', $f->getPageRangeString());
}

/**
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage No Ghostscript-instance found or running on windows. Please provide Path to the Ghostscript-executable
*/
public function testSettingDefaultGsPathFails()
{
exec('which gs', $output);
if ($output) {
$this->markTestSkipped('Can not test due to installed GS');
}

$this->setExpectedExceptionFromAnnotation();

Ghostscript::setGsPath();
}

public function testSettingDefaultGsPathWorks()
{
exec('which gs', $output);
if (! $output) {
$this->markTestSkipped('Can not test due to not installed GS');
}

Ghostscript::setGsPath();

$this->assertAttributeEquals($output[0], 'PATH', Ghostscript::class);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The given file is not executable
*/
public function testThatSettingPathToNonExecutableFails()
{
Ghostscript::setGsPath(__DIR__ . '/_assets/nonExecutable');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage No valid Ghostscript found
*/
public function testThatSettingPathToNonGsFails()
{
Ghostscript::setGsPath(__DIR__ . '/_assets/executableNonGs');
}

public function testThatSettingPathToSomethingGsLikeWorks()
{
Ghostscript::setGsPath(__DIR__ . '/_assets/executableGs');
$this->assertAttributeEquals(
__DIR__ . '/_assets/executableGs',
'PATH',
Ghostscript::class
);
}
}
3 changes: 3 additions & 0 deletions tests/_assets/executableGs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo 'Something with Ghostscript in it'
3 changes: 3 additions & 0 deletions tests/_assets/executableNonGs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo 'Foo'
Empty file added tests/_assets/nonExecutable
Empty file.

0 comments on commit ecb147c

Please sign in to comment.