Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SRWieZ committed Nov 1, 2023
1 parent 210a255 commit 1a14265
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
6 changes: 1 addition & 5 deletions bin/svgtinyps
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/usr/bin/env php
<?php

if (file_exists(__DIR__.'/../../../autoload.php')) {
require __DIR__.'/../../../autoload.php';
} else {
require __DIR__.'/../vendor/autoload.php';
}
include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';

require __DIR__.'/../src/svgtinyps.php';
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"require": {
"php": "^8.2",
"ext-dom": "*",
"srwiez/php-svg-ps-converter": "^1.1"
"srwiez/php-svg-ps-converter": "^1.1",
"composer-runtime-api": "^2.2"
},
"require-dev": {
"pestphp/pest": "^2.24",
Expand Down
50 changes: 32 additions & 18 deletions src/svgtinyps.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,46 @@ function showHelp()
exit;
}

$inputFile = $argv[1] ?? null;
function checkInputFile($inputFile)
{
if (! $inputFile || ! file_exists($inputFile)) {
echo "Error: Input file not provided or doesn't exist.".PHP_EOL;
exit(1);
}
}

function checkOutputFile($outputFile)
{
$outputDir = $outputFile ? dirname($outputFile) : null;
if (empty($outputFile)) {
echo 'Error: Output file not provided for conversion.'.PHP_EOL;
exit(1);
}

if (! $inputFile || ! file_exists($inputFile)) {
echo "Error: Input file not provided or doesn't exist.".PHP_EOL;
exit(1);
if ($outputDir && (! is_dir($outputDir) || ! is_writable($outputDir))) {
echo "Error: The output directory either does not exist or is not writeable.\n";
exit(1);
}
}

$outputFile = $argv[2] ?? null;
switch ($command) {
case 'convert':
$inputFile = $argv[1] ?? null;
$outputFile = $argv[2] ?? null;

// Checking output directory
$outputDir = $outputFile ? dirname($outputFile) : null;

if (empty($outputFile)) {
echo 'Error: Output file not provided for conversion.'.PHP_EOL;
exit(1);
}

if ($outputDir && (! is_dir($outputDir) || ! is_writable($outputDir))) {
echo "Error: The output directory either does not exist or is not writeable.\n";
exit(1);
}
checkInputFile($inputFile);
checkOutputFile($outputFile);

verboseLog('Starting SVG conversion', $isVerbose);
convertSvg($inputFile, $outputFile, $isVerbose);
break;
case 'issues':

$inputFile = $argv[1] ?? null;
$outputFile = $argv[2] ?? null;

checkInputFile($inputFile);

verboseLog('Checking for SVG issues', $isVerbose);
checkIssues($inputFile, $isVerbose);
break;
Expand All @@ -104,8 +116,10 @@ function showHelp()
// minifySvg($inputFile, $isVerbose);
// break;
default:
echo 'Invalid command.'.PHP_EOL;
echo 'Invalid command!'.PHP_EOL;
echo PHP_EOL;
showHelp();
exit(1);
break;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@
expect($output)->toContain('Error: Output file not provided for conversion')
->and($exitCode)->toBe(1);
});

test('it output an error if command is not provided or invalid', function () {
exec('php src/svgtinyps.php assets/twitter.svg', $output, $exitCode);
$output = implode("\n", $output); // Combine array elements into a single string
expect($output)->toContain('Invalid command')
->and($exitCode)->toBe(1);
});

0 comments on commit 1a14265

Please sign in to comment.