Skip to content

Commit 6665608

Browse files
committed
Add FormatNotFoundException exception
1 parent 78e796b commit 6665608

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CodeLts\CliTools\Exceptions;
6+
7+
class FormatNotFoundException extends \Exception
8+
{
9+
10+
public function __construct(string $formatName)
11+
{
12+
parent::__construct(sprintf('The format "%s" is not implemented.', $formatName));
13+
}
14+
15+
public function getTip(): ?string
16+
{
17+
return null;
18+
}
19+
20+
}

src/OutputFormat.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeLts\CliTools\ErrorFormatter\RawTextErrorFormatter;
1515
use CodeLts\CliTools\ErrorFormatter\TableErrorFormatter;
1616
use CodeLts\CliTools\ErrorFormatter\TeamcityErrorFormatter;
17+
use CodeLts\CliTools\Exceptions\FormatNotFoundException;
1718
use CodeLts\CliTools\File\FuzzyRelativePathHelper;
1819
use CodeLts\CliTools\File\NullRelativePathHelper;
1920
use CodeLts\CliTools\File\RelativePathHelper;
@@ -52,14 +53,14 @@ class OutputFormat
5253
* @param string $outputFormat The format to check
5354
* @return true
5455
*
55-
* @throws Exception
56+
* @throws FormatNotFoundException if the format does not exist
5657
*/
5758
public static function checkOutputFormatIsValid(string $outputFormat): bool
5859
{
5960
if (in_array($outputFormat, OutputFormat::VALID_OUTPUT_FORMATS)) {
6061
return true;
6162
}
62-
throw new Exception(
63+
throw new FormatNotFoundException(
6364
sprintf(
6465
'Error formatter "%s" not found. Available error formatters are: %s',
6566
$outputFormat,
@@ -97,7 +98,7 @@ public static function displayUserChoiceFormat(
9798
/**
9899
* get an ErrorFormatter for the given format
99100
*
100-
* @throws Exception if the format is not implemented
101+
* @throws FormatNotFoundException if the format is not implemented
101102
*/
102103
public static function getFormatterForChoice(
103104
string $outputFormat,
@@ -143,6 +144,6 @@ public static function getFormatterForChoice(
143144
return new TeamcityErrorFormatter($pathHelper);
144145
}
145146

146-
throw new Exception('Not implemented format');
147+
throw new FormatNotFoundException($outputFormat);
147148
}
148149
}

tests/OutputFormatTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace CodeLts\CliTools\Tests;
66

77
use CodeLts\CliTools\ErrorFormatter\ErrorFormatter;
8+
use CodeLts\CliTools\Exceptions\FormatNotFoundException;
89
use CodeLts\CliTools\File\NullRelativePathHelper;
910
use CodeLts\CliTools\OutputFormat;
10-
use Exception;
1111

1212
class OutputFormatTest extends ErrorFormatterTestCase
1313
{
@@ -38,7 +38,7 @@ public function testValidFormats(string $formatName): void
3838
public function testInValidFormats(string $formatName): void
3939
{
4040
$formatName = 'foo' . $formatName;
41-
$this->expectException(Exception::class);
41+
$this->expectException(FormatNotFoundException::class);
4242
$this->expectExceptionMessage(
4343
'Error formatter "' . $formatName . '" not found.'
4444
. ' Available error formatters are: raw, rawtext, table, checkstyle, json, junit, prettyJson, gitlab, github, teamcity'
@@ -54,6 +54,19 @@ public function testGetFormatterForChoice(string $formatName): void
5454
$this->assertInstanceOf(ErrorFormatter::class, OutputFormat::getFormatterForChoice($formatName, new NullRelativePathHelper()));
5555
}
5656

57+
/**
58+
* @dataProvider dataProviderFormatsNames
59+
*/
60+
public function testGetFormatterForChoiceInvalid(string $formatName): void
61+
{
62+
$formatName = 'foo' . $formatName;
63+
$this->expectException(FormatNotFoundException::class);
64+
$this->expectExceptionMessage(
65+
'The format "' . $formatName . '" is not implemented.'
66+
);
67+
OutputFormat::getFormatterForChoice($formatName, new NullRelativePathHelper());
68+
}
69+
5770
/**
5871
* @dataProvider dataProviderFormatsNames
5972
*/

0 commit comments

Comments
 (0)