Skip to content

Commit

Permalink
Merge pull request #20 from kynx/continue-after-error
Browse files Browse the repository at this point in the history
Continue processing after error
  • Loading branch information
kynx authored Feb 24, 2024
2 parents 2057142 + e723f80 commit 207ad58
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Form/FormProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Kynx\Laminas\FormShape\Form;

use Kynx\Laminas\FormShape\ExceptionInterface;
use Kynx\Laminas\FormShape\InputFilter\ImportType;
use Kynx\Laminas\FormShape\InputFilter\InputVisitorException;
use Kynx\Laminas\FormShape\Locator\FormFile;
use Kynx\Laminas\FormShape\Locator\FormLocatorInterface;
use Kynx\Laminas\FormShape\Writer\FileWriterInterface;
Expand Down Expand Up @@ -104,17 +104,16 @@ private function processForm(
): void {
try {
$union = $this->formVisitor->visit($formFile->form, $types);
} catch (InputVisitorException $e) {
$this->fileWriter->write($formFile->reflection, $union, $types, $removeGetDataReturn);
$listener->success($formFile->reflection);
} catch (ExceptionInterface $e) {
$listener->error(sprintf(
"Error processing %s: %s ",
$formFile->reflection->getFileName(),
"Error processing %s: %s",
$formFile->reflection->getName(),
$e->getMessage()
));
return;
}

$this->fileWriter->write($formFile->reflection, $union, $types, $removeGetDataReturn);
$listener->success($formFile->reflection);
}

/**
Expand All @@ -127,9 +126,19 @@ private function processFieldset(
ProgressListenerInterface $listener
): array {
$reflection = new ReflectionClass($fieldset);
$union = $this->fieldsetVisitor->visit($fieldset, $types);
$typeName = $this->fileWriter->write($reflection, $union, $types);
$listener->success($reflection);

try {
$union = $this->fieldsetVisitor->visit($fieldset, $types);
$typeName = $this->fileWriter->write($reflection, $union, $types);
$listener->success($reflection);
} catch (ExceptionInterface $e) {
$listener->error(sprintf(
"Error processing %s: %s",
$reflection->getName(),
$e->getMessage()
));
return [];
}

$types[$fieldset::class] = new ImportType(new TTypeAlias($reflection->getName(), $typeName), $union);
return $types;
Expand Down
45 changes: 45 additions & 0 deletions test/Form/FormProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Kynx\Laminas\FormShape\InputFilter\ImportType;
use Kynx\Laminas\FormShape\InputFilter\InputFilterVisitor;
use Kynx\Laminas\FormShape\InputFilter\InputVisitor;
use Kynx\Laminas\FormShape\InputFilter\InputVisitorException;
use Kynx\Laminas\FormShape\InputVisitorInterface;
use Kynx\Laminas\FormShape\Locator\FormFile;
use Kynx\Laminas\FormShape\Locator\FormLocatorInterface;
use KynxTest\Laminas\FormShape\Form\Asset\ChildFieldset;
Expand All @@ -19,6 +21,7 @@
use Laminas\Form\Element\Text;
use Laminas\Form\Fieldset;
use Laminas\Form\Form;
use Laminas\InputFilter\Input;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -63,6 +66,48 @@ public function testProcessNoFilesReturnsEarly(): void
self::assertSame(0, $this->listener->processed);
}

public function testProcessContinuesAfterError(): void
{
$errors = [
"Error processing " . TestFieldset::class . ": Cannot get type for 'bar'",
"Error processing " . Form::class . ": Cannot get type for 'bar'",
];

$first = new Form();
$fieldset = new TestFieldset('foo');
$fieldset->add(new Text('bar'));
$first->add($fieldset);

$second = new Form();
$second->add(new Text('foo'));

$this->formLocator->method('locate')
->willReturn([
new FormFile(new ReflectionClass($first), $first),
new FormFile(new ReflectionClass($second), $second),
]);

$inputVisitor = self::createStub(InputVisitorInterface::class);
$inputVisitor->method('visit')
->willReturnCallback(static function (Input $input): Union {
if ($input->getName() === 'bar') {
throw InputVisitorException::cannotGetInputType($input);
}
return new Union([new TString()]);
});

$processor = new FormProcessor(
$this->formLocator,
new FormVisitor(new InputFilterVisitor([$inputVisitor])),
$this->fileWriter
);

$processor->process(['foo'], $this->listener);

self::assertSame($errors, $this->listener->errors);
self::assertCount(1, $this->listener->success);
}

public function testProcessFieldsetWritesAndAddsType(): void
{
$fieldsetUnion = new Union([
Expand Down

0 comments on commit 207ad58

Please sign in to comment.