Skip to content

Commit

Permalink
Throw a better exception if binary is not found (and catch it in test)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Mar 5, 2019
1 parent b5c434f commit ff5696b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ before_install: |
make
sudo make install
sudo ldconfig /usr/local/lib
which magick
find /usr/local -iname "*magick*"
cd ..
mkdir -p build/logs
Expand Down
6 changes: 1 addition & 5 deletions Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ public static function findMagickBinaryPath(?string $magickBinaryPath): string
// Add a proper directory separator at the end if path is not empty.
// If it's empty, then it's set in the global path.
if ($magickBinaryPath && !\is_file($magickBinaryPath)) {
throw new \InvalidArgumentException(\sprintf(
'The specified path (%s) is not a file.'."\n".
'You must set the "magickBinaryPath" parameter as the main "magick" binary installed by ImageMagick.',
$magickBinaryPath
));
throw new MagickBinaryNotFoundException($magickBinaryPath);
}

if (!\is_executable($magickBinaryPath)) {
Expand Down
28 changes: 28 additions & 0 deletions MagickBinaryNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/*
* This file is part of the OrbitaleImageMagickPHP package.
*
* (c) Alexandre Rock Ancelet <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Orbitale\Component\ImageMagick;

use Exception;

class MagickBinaryNotFoundException extends Exception
{
public function __construct(string $magickBinaryPath)
{
parent::__construct(\sprintf(
'The specified path (%s) is not a file.'."\n".
'You must set the "magickBinaryPath" parameter as the main "magick" binary installed by ImageMagick.',
$magickBinaryPath
));
}
}
14 changes: 9 additions & 5 deletions Tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

use Orbitale\Component\ImageMagick\Command;
use Orbitale\Component\ImageMagick\MagickBinaryNotFoundException;

$file = __DIR__ . '/../vendor/autoload.php';
if (!file_exists($file)) {
Expand All @@ -36,12 +37,15 @@
// Could happen if "getenv()" returns false or empty string.
continue;
}
$path = Command::findMagickBinaryPath($path);
echo 'Check "'.$path.'" binary'."\n";
exec($path.' -version', $o, $code);
if (0 === $code) {
define('IMAGEMAGICK_DIR', $path);
break;
try {
$path = Command::findMagickBinaryPath($path);
exec($path.' -version', $o, $code);
if (0 === $code) {
define('IMAGEMAGICK_DIR', $path);
break;
}
} catch (MagickBinaryNotFoundException $e) {
}
}

Expand Down

0 comments on commit ff5696b

Please sign in to comment.