Skip to content

Commit 775537c

Browse files
author
Enno Woortmann
committed
Add ReferenceProcessor test cases
Fix decorator functionality for array items
1 parent 8d1fdda commit 775537c

15 files changed

+314
-16
lines changed

src/Model/Property/Property.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function addDecorator(PropertyDecoratorInterface $decorator): PropertyInt
153153
public function resolveDecorator(string $input): string
154154
{
155155
foreach ($this->decorators as $decorator) {
156-
$input = $decorator->decorate($input);
156+
$input = $decorator->decorate($input, $this);
157157
}
158158

159159
return $input;

src/Model/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function addDefinition(string $key, SchemaDefinition $definition)
126126
*
127127
* @return SchemaDefinition
128128
*/
129-
public function getDefinition(string $key): SchemaDefinition
129+
public function getDefinition(string $key): ?SchemaDefinition
130130
{
131131
return $this->definitions[$key] ?? null;
132132
}

src/PropertyProcessor/Decorator/IntToFloatCastDecorator.php

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

55
namespace PHPModelGenerator\PropertyProcessor\Decorator;
66

7+
use PHPModelGenerator\Model\Property\PropertyInterface;
8+
79
/**
810
* Class IntToFloatCastDecorator
911
*
@@ -14,7 +16,7 @@ class IntToFloatCastDecorator implements PropertyDecoratorInterface
1416
/**
1517
* @inheritdoc
1618
*/
17-
public function decorate(string $input): string
19+
public function decorate(string $input, PropertyInterface $property): string
1820
{
1921
return "is_int($input) ? (float) $input : $input";
2022
}

src/PropertyProcessor/Decorator/ObjectInstantiationDecorator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPMicroTemplate\Render;
88
use PHPModelGenerator\Exception\InvalidArgumentException;
9+
use PHPModelGenerator\Model\Property\PropertyInterface;
910

1011
/**
1112
* Class ObjectInstantiationDecorator
@@ -38,13 +39,14 @@ public function __construct(string $className)
3839
/**
3940
* @inheritdoc
4041
*/
41-
public function decorate(string $input): string
42+
public function decorate(string $input, PropertyInterface $property): string
4243
{
4344
return static::$renderer->renderTemplate(
4445
DIRECTORY_SEPARATOR . 'Decorator' . DIRECTORY_SEPARATOR . 'ObjectInstantiationDecorator.phptpl',
4546
[
4647
'input' => $input,
47-
'className' => $this->className
48+
'className' => $this->className,
49+
'property' => $property,
4850
]
4951
);
5052
}

src/PropertyProcessor/Decorator/PropertyDecoratorInterface.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PHPModelGenerator\PropertyProcessor\Decorator;
66

7+
use PHPModelGenerator\Model\Property\PropertyInterface;
8+
79
/**
810
* Interface PropertyDecoratorInterface
911
*
@@ -14,11 +16,12 @@ interface PropertyDecoratorInterface
1416
/**
1517
* Decorate a given string
1618
*
17-
* @param string $input
19+
* @param string $input
20+
* @param PropertyInterface $property The property getting decorated
1821
*
1922
* @return string
2023
*/
21-
public function decorate(string $input): string;
24+
public function decorate(string $input, PropertyInterface $property): string;
2225

2326
/**
2427
* Return a list of all exception classes which may be thrown by the decorator

src/PropertyProcessor/Decorator/PropertyTransferDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(PropertyInterface $property)
3232
/**
3333
* @inheritdoc
3434
*/
35-
public function decorate(string $input): string
35+
public function decorate(string $input, PropertyInterface $property): string
3636
{
3737
return $this->property->resolveDecorator($input);
3838
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(is_array($param = {{ input }})
22
? new {{ className }}($param)
33
: (function() {
4-
throw new InvalidArgumentException('invalid type for property');
4+
throw new InvalidArgumentException('invalid type for {{ property.getName() }}');
55
})()
66
)

src/Templates/Validator/ArrayItem.phptpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
is_array($value) && (function ($items) {
1+
is_array($value) && (function (&$items) {
22
if (empty($items)) {
33
return false;
44
}
55

6-
foreach ($items as $value) {
6+
foreach ($items as &$value) {
77
{{ viewHelper.resolvePropertyDecorator(property) }}
88

99
{% foreach property.getOrderedValidators() as validator %}

tests/Objects/ArrayPropertyTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,15 @@ public function invalidItemAmountDataProvider(): array
205205
* @dataProvider validTypedArrayDataProvider
206206
*
207207
* @param string $type
208-
* @param $propertyValue
208+
* @param $propertyValue
209+
* @param $expectedValue
209210
*/
210-
public function testTypedArrayIsValid(string $type, $propertyValue): void
211+
public function testTypedArrayIsValid(string $type, $propertyValue, $expectedValue = null): void
211212
{
212213
$className = $this->generateObjectFromFileTemplate('ArrayPropertyTyped.json', [$type], null, false);
213214

214215
$object = new $className(['property' => $propertyValue]);
215-
$this->assertSame($propertyValue, $object->getProperty());
216+
$this->assertSame($expectedValue ?? $propertyValue, $object->getProperty());
216217
}
217218

218219
public function validTypedArrayDataProvider(): array
@@ -224,7 +225,8 @@ public function validTypedArrayDataProvider(): array
224225
'String array with null' => ['string', ['a', 'b', null]],
225226
'Int array' => ['int', [1, 2, 3]],
226227
'Int array with null' => ['int', [1, 2, 3, null]],
227-
'Number array' => ['number', [1, 1.1, 4.5, 6]],
228+
// Number array will cast int to float
229+
'Number array' => ['number', [1, 1.1, 4.5, 6], [1.0, 1.1, 4.5, 6.0]],
228230
'Boolean array' => ['boolean', [true, false, true]],
229231
'Null array' => ['null', [null, null]],
230232
'Nested array' => ['array","items":{"type":"int"},"injection":"yes we can', [[1, 2], [], [3], null]]

tests/Objects/ObjectPropertyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use stdClass;
1111

1212
/**
13-
* Class AnyPropertyTest
13+
* Class ObjectPropertyTest
1414
*
1515
* @package PHPModelGenerator\Tests\Objects
1616
*/

0 commit comments

Comments
 (0)