From e635615c539f239c1d587d616f27af742a904f6f Mon Sep 17 00:00:00 2001 From: Andreas Heigl Date: Wed, 5 Apr 2017 22:41:01 +0200 Subject: [PATCH 1/2] Fixes an issue with setting the GS-Path --- src/Ghostscript.php | 33 +++++++++++++----- tests/GhostscriptTest.php | 64 ++++++++++++++++++++++++++++++----- tests/_assets/executableGs | 3 ++ tests/_assets/executableNonGs | 3 ++ tests/_assets/nonExecutable | 0 5 files changed, 87 insertions(+), 16 deletions(-) create mode 100755 tests/_assets/executableGs create mode 100755 tests/_assets/executableNonGs create mode 100644 tests/_assets/nonExecutable diff --git a/src/Ghostscript.php b/src/Ghostscript.php index f41c71e..3964dc9 100644 --- a/src/Ghostscript.php +++ b/src/Ghostscript.php @@ -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 @@ -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; } @@ -843,4 +857,7 @@ public function setPages($startPage, $endPage = null) } } -Ghostscript::setGsPath(); +try { + Ghostscript::setGsPath(); +} catch (\UnexpectedValueException $e) { +} diff --git a/tests/GhostscriptTest.php b/tests/GhostscriptTest.php index a2c39bd..8d4f599 100644 --- a/tests/GhostscriptTest.php +++ b/tests/GhostscriptTest.php @@ -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 */ @@ -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 + ); + } } diff --git a/tests/_assets/executableGs b/tests/_assets/executableGs new file mode 100755 index 0000000..dbdc2c1 --- /dev/null +++ b/tests/_assets/executableGs @@ -0,0 +1,3 @@ +#!/bin/bash + +echo 'Something with Ghostscript in it' diff --git a/tests/_assets/executableNonGs b/tests/_assets/executableNonGs new file mode 100755 index 0000000..117a80c --- /dev/null +++ b/tests/_assets/executableNonGs @@ -0,0 +1,3 @@ +#!/bin/bash + +echo 'Foo' diff --git a/tests/_assets/nonExecutable b/tests/_assets/nonExecutable new file mode 100644 index 0000000..e69de29 From 6254048dde49c388df7b96d9b9df5b4e43f6dde4 Mon Sep 17 00:00:00 2001 From: Andreas Heigl Date: Wed, 5 Apr 2017 22:46:11 +0200 Subject: [PATCH 2/2] Adds Exception for unset PATH --- src/Ghostscript.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Ghostscript.php b/src/Ghostscript.php index 3964dc9..10e07ae 100644 --- a/src/Ghostscript.php +++ b/src/Ghostscript.php @@ -300,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; }