Skip to content

Commit f6da899

Browse files
committed
Fix rollback in PopulatePostProcessor with error collection disabled
1 parent be74d7d commit f6da899

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/SchemaProcessor/PostProcessor/Templates/Populate.phptpl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
*/
1111
public function populate(array $modelData): self
1212
{
13+
$rollbackValues = [];
14+
1315
{% if generatorConfiguration.collectErrors() %}
1416
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
17+
{% else %}
18+
try {
1519
{% endif %}
16-
$rollbackValues = [];
1720

1821
{% if schema.getBaseValidators() %}
1922
$this->executeBaseValidators($modelData);
@@ -34,6 +37,14 @@ public function populate(array $modelData): self
3437

3538
throw $this->errorRegistry;
3639
}
40+
{% else %}
41+
} catch (ValidationException $exception) {
42+
foreach ($rollbackValues as $property => $value) {
43+
$this->{$property} = $value;
44+
}
45+
46+
throw $exception;
47+
}
3748
{% endif %}
3849

3950
$this->rawModelDataInput = array_merge($this->rawModelDataInput, $modelData);

tests/PostProcessor/PopulatePostProcessorTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use PHPModelGenerator\Exception\ErrorRegistryException;
7+
use PHPModelGenerator\Exception\Generic\InvalidTypeException;
78
use PHPModelGenerator\Exception\Object\InvalidAdditionalPropertiesException;
89
use PHPModelGenerator\Exception\Object\InvalidPropertyNamesException;
910
use PHPModelGenerator\Exception\Object\MaxPropertiesException;
@@ -75,7 +76,7 @@ public function testInvalidPopulateThrowsAnException(
7576

7677
$className = $this->generateClassFromFile(
7778
'BasicSchema.json',
78-
(new GeneratorConfiguration())->setCollectErrors($collectErrors)
79+
(new GeneratorConfiguration())->setCollectErrors($collectErrors)->setSerialization(true)
7980
);
8081
$object = new $className(['name' => 'Albert', 'age' => 30]);
8182

@@ -85,6 +86,7 @@ public function testInvalidPopulateThrowsAnException(
8586
} catch (Exception $exception) {
8687
// test if the internal state hasn't been changed
8788
$this->assertSame(['name' => 'Albert', 'age' => 30], $object->getRawModelDataInput());
89+
$this->assertSame(['name' => 'Albert', 'age' => 30], $object->toArray());
8890

8991
throw $exception;
9092
}
@@ -99,6 +101,18 @@ public function invalidPopulateDataProvider(): array
99101
PatternException::class,
100102
"Value for name doesn't match pattern ^[a-zA-Z]*$"
101103
],
104+
'No error collection - first value ok second invalid' => [
105+
['name' => 'Hannes', 'age' => false],
106+
false,
107+
InvalidTypeException::class,
108+
"Invalid type for age. Requires int, got boolean"
109+
],
110+
'Error collection - first value ok second invalid' => [
111+
['name' => 'Hannes', 'age' => false],
112+
true,
113+
ErrorRegistryException::class,
114+
"Invalid type for age. Requires int, got boolean"
115+
],
102116
'Error collection - multiple violations' => [
103117
['name' => 'Anne-Marie', 'age' => false],
104118
true,

0 commit comments

Comments
 (0)