Skip to content

Commit 93f377b

Browse files
committed
* Error collection
* Extend readme
1 parent 1bb58df commit 93f377b

File tree

6 files changed

+126
-5
lines changed

6 files changed

+126
-5
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ The *GeneratorConfiguration* object offers the following methods to configure th
5353

5454
Method | Configuration | Example | Default
5555
--- | --- | --- | ---
56-
``` setNamespacePrefix(string $prefix) ``` | Configures a namespace prefix for all generated classes. The namespaces will be extended with the directory structure of the source directory. | ``` setNamespacePrefix('\\MyApp\\Model') ``` | Empty string so no namespace prefix will be used
57-
``` setImmutable(bool $immutable) ``` | If set to true the generated model classes will be delivered without setter methods for the object properties. | ``` setImmutable(true) ``` | false
56+
``` setNamespacePrefix(string $prefix) ``` | Configures a namespace prefix for all generated classes. The namespaces will be extended with the directory structure of the source directory. | ``` setNamespacePrefix('\MyApp\Model') ``` | Empty string so no namespace prefix will be used
57+
``` setImmutable(bool $immutable) ``` | If set to true the generated model classes will be delivered without setter methods for the object properties. | ``` setImmutable(false) ``` | true
58+
``` setCollectErrors(bool $collectErrors) ``` | By default the complete input is validated and in case of failing validations all error messages will be thrown in a single exception. If set to false the first failing validation will throw an exception. | ``` setCollectErrors(true) ``` | false
5859
``` setPrettyPrint(bool $prettyPrint) ``` | If set to false, the generated model classes won't follow coding gudelines (but the generation is faster). If enabled the package [Symplify/EasyCodingStandard](https://github.com/Symplify/EasyCodingStandard) will be used to clean up the generated code. | ``` setPrettyPrint(false) ``` | true
5960
``` setOutputEnabled(bool $prettyPrint) ``` | Enable or disable output of the generation process to STDOUT | ``` setOutputEnabled(false) ``` | true
61+
``` setErrorRegistryClass(string $exceptionClass) ``` | Define a custom exception implementing the ErrorRegistryExceptionInterface to decouple the generated code from the library (if you want to declare the library as a dev-dependency). The exception will be thrown if a validation fails error collection is **enabled** | ``` setErrorRegistryClass(CustomException::class) ``` | PHPModelGenerator\Exception\ErrorRegistryException::class
62+
``` setExceptionClass(bool $prettyPrint) ``` | Define a custom exception to decouple the generated code from the library (if you want to declare the library as a dev-dependency). The exception will be thrown if a validation fails error collection is **disabled** | ``` setExceptionClass(CustomException::class) ``` | PHPModelGenerator\Exception\InvalidArgumentException::class
6063

6164
## How the heck does this work? ##
6265

src/PropertyProcessor/PropertyCollectionProcessor.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class PropertyCollectionProcessor
2222
public function __construct(array $requiredAttributes = [])
2323
{
2424
$this->requiredAttributes = $requiredAttributes;
25-
return $this;
2625
}
2726

2827
/**

src/Templates/Model.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class {{ class }}
4747
) {
4848
{% if generatorConfiguration.collectErrors() %}
4949
if (!$errorRegistry) {
50-
$this->isInitialClass = false;
50+
$this->isInitialClass = true;
5151
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
5252
}
5353
{% endif%}

src/Utils/RenderHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function validationError(string $message): string
101101
$message = $this->escapeSingleQuotes($message);
102102

103103
if ($this->generatorConfiguration->collectErrors()) {
104-
104+
return "\$this->errorRegistry->addError('$message');";
105105
}
106106

107107
$exceptionClass = $this->getSimpleClassName($this->generatorConfiguration->getExceptionClass());

tests/Basic/ErrorCollectionTest.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Tests\Basic;
6+
7+
use PHPModelGenerator\Exception\ErrorRegistryException;
8+
use PHPModelGenerator\Model\GeneratorConfiguration;
9+
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
10+
use stdClass;
11+
12+
/**
13+
* Class ErrorCollectionTest
14+
*
15+
* @package PHPModelGenerator\Tests\Basic
16+
*/
17+
class ErrorCollectionTest extends AbstractPHPModelGeneratorTest
18+
{
19+
/**
20+
* @dataProvider validValuesForSinglePropertyDataProvider
21+
*
22+
* @param string $value
23+
*/
24+
public function testValidValuesForMultipleChecksForSingleProperty(string $value): void
25+
{
26+
$className = $this->generateClassFromFile(
27+
'MultipleChecksForSingleProperty.json',
28+
(new GeneratorConfiguration())->setCollectErrors(true)
29+
);
30+
31+
$object = new $className(['property' => $value]);
32+
$this->assertSame($value, $object->getProperty());
33+
}
34+
35+
public function validValuesForSinglePropertyDataProvider(): array
36+
{
37+
return [
38+
'numeric string' => ['10'],
39+
'letter string' => ['Ab'],
40+
'special chars' => ['+.'],
41+
];
42+
}
43+
/**
44+
* @dataProvider invalidValuesForSinglePropertyDataProvider
45+
*
46+
* @param string $value
47+
*/
48+
public function testInvalidValuesForMultipleChecksForSinglePropertyThrowsAnException(
49+
$value,
50+
array $messages
51+
): void {
52+
$this->expectExceptionObject($this->getErrorRegistryException($messages));
53+
54+
$className = $this->generateClassFromFile(
55+
'MultipleChecksForSingleProperty.json',
56+
(new GeneratorConfiguration())->setCollectErrors(true)
57+
);
58+
59+
new $className(['property' => $value]);
60+
}
61+
62+
public function invalidValuesForSinglePropertyDataProvider(): array
63+
{
64+
return [
65+
'pattern invalid' => [
66+
' ',
67+
['property doesn\'t match pattern ^[^\s]+$']
68+
],
69+
'length invalid' => [
70+
'a',
71+
['property must not be shorter than 2']
72+
],
73+
'pattern and length invalid' => [
74+
' ',
75+
[
76+
'property doesn\'t match pattern ^[^\s]+$',
77+
'property must not be shorter than 2'
78+
]
79+
],
80+
'null' => [null, ['invalid type for property']],
81+
'int' => [1, ['invalid type for property']],
82+
'float' => [0.92, ['invalid type for property']],
83+
'bool' => [true, ['invalid type for property']],
84+
'array' => [[], ['invalid type for property']],
85+
'object' => [new stdClass(), ['invalid type for property']],
86+
];
87+
}
88+
89+
/**
90+
* Set up an ErrorRegistryException containing the given messages
91+
*
92+
* @param array $messages
93+
*
94+
* @return ErrorRegistryException
95+
*/
96+
protected function getErrorRegistryException(array $messages): ErrorRegistryException
97+
{
98+
$errorRegistry = new ErrorRegistryException();
99+
100+
foreach ($messages as $message) {
101+
$errorRegistry->addError($message);
102+
}
103+
104+
return $errorRegistry;
105+
}
106+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"property": {
5+
"type": "string",
6+
"minLength": 2,
7+
"pattern": "^[^\\s]+$"
8+
}
9+
},
10+
"required": [
11+
"property"
12+
]
13+
}

0 commit comments

Comments
 (0)