From e723f808cb333858a7eaee475a2cb02644e905c5 Mon Sep 17 00:00:00 2001 From: matt Date: Sat, 24 Feb 2024 00:06:51 +0000 Subject: [PATCH] Continue processing after error --- src/Form/FormProcessor.php | 29 +++++++++++++-------- test/Form/FormProcessorTest.php | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/Form/FormProcessor.php b/src/Form/FormProcessor.php index e4b3ed7..cdc84d6 100644 --- a/src/Form/FormProcessor.php +++ b/src/Form/FormProcessor.php @@ -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; @@ -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); } /** @@ -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; diff --git a/test/Form/FormProcessorTest.php b/test/Form/FormProcessorTest.php index 8585967..aa9f35c 100644 --- a/test/Form/FormProcessorTest.php +++ b/test/Form/FormProcessorTest.php @@ -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; @@ -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; @@ -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([