Skip to content

Commit

Permalink
Faker converter: better exception message when a formatter is not def…
Browse files Browse the repository at this point in the history
…ined
  • Loading branch information
guvra committed Feb 20, 2024
1 parent 4333243 commit a43b809
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/Converter/Proxy/Faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Smile\GdprDump\Converter\Proxy;

use Exception;
use Smile\GdprDump\Converter\ConverterInterface;
use Smile\GdprDump\Converter\Parameters\Parameter;
use Smile\GdprDump\Converter\Parameters\ParameterProcessor;
use Smile\GdprDump\Converter\Parameters\ValidationException;
use Smile\GdprDump\Faker\FakerService;

class Faker implements ConverterInterface
Expand Down Expand Up @@ -36,11 +38,16 @@ public function setParameters(array $parameters): void

// Create the formatter now to ensure that errors related to undefined formatters
// are triggered before the start of the dump process
$formatter = $input->get('formatter');
// @phpstan-ignore-next-line getFormatter function always returns an array with 2 items
[$this->provider, $this->method] = $this->fakerService
->getGenerator()
->getFormatter($formatter);
try {
$formatter = $input->get('formatter');
// @phpstan-ignore-next-line getFormatter function always returns an array with 2 items
[$this->provider, $this->method] = $this->fakerService
->getGenerator()
->getFormatter($formatter);
} catch (Exception $e) {
// Wrap the original exception to make it more clear that the error is due to a faker formatter
throw new ValidationException(sprintf('Faker formatter error: %s', $e->getMessage()), $e);
}

$this->arguments = $input->get('arguments') ?? [];
foreach ($this->arguments as $name => $value) {
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/Converter/Proxy/FakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Smile\GdprDump\Tests\Unit\Converter\Proxy;

use InvalidArgumentException;
use Smile\GdprDump\Converter\Parameters\ValidationException;
use Smile\GdprDump\Tests\Unit\Converter\TestCase;

Expand Down Expand Up @@ -52,7 +51,7 @@ public function testFormatterNotSet(): void
*/
public function testInvalidFormatter(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(ValidationException::class);
$this->createFakerConverter([
'formatter' => 'doesNotExist',
'arguments' => [1, 1],
Expand Down

0 comments on commit a43b809

Please sign in to comment.