Skip to content

Commit 590e8ca

Browse files
committed
Additional test cases
1 parent b065fd9 commit 590e8ca

File tree

13 files changed

+255
-11
lines changed

13 files changed

+255
-11
lines changed

src/Model/Property/Property.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function getExceptionClasses(): array
261261
protected function processAttributeName(string $name): string
262262
{
263263
$name = preg_replace_callback(
264-
'/([a-z])([A-Z])/',
264+
'/([a-z][a-z0-9]*)([A-Z])/',
265265
function ($matches) {
266266
return "{$matches[1]}-{$matches[2]}";
267267
},
@@ -272,7 +272,7 @@ function ($matches) {
272272
function ($element) {
273273
return ucfirst(strtolower($element));
274274
},
275-
preg_split('/[^a-z]/i', $name)
275+
preg_split('/[^a-z0-9]/i', $name)
276276
);
277277

278278
return lcfirst(join('', $elements));

src/Model/Validator/PropertyTemplateValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PHPModelGenerator\Exception\RenderException;
1010

1111
/**
12-
* Class CallbackPropertyValidator
12+
* Class PropertyTemplateValidator
1313
*
1414
* @package PHPModelGenerator\Model\Validator
1515
*/

src/PropertyProcessor/ComposedValue/IfProcessor.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class IfProcessor extends AbstractValueProcessor implements ComposedPropertiesIn
2929
*/
3030
protected function generateValidators(PropertyInterface $property, array $propertyData): void
3131
{
32-
echo print_r($propertyData, true);
3332
if (!isset($propertyData['propertyData']['then']) && !isset($propertyData['propertyData']['else'])) {
3433
throw new SchemaException('Incomplete conditional composition');
3534
}

src/PropertyProcessor/Property/BaseProcessor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use PHPModelGenerator\Exception\SchemaException;
99
use PHPModelGenerator\Model\Property\Property;
1010
use PHPModelGenerator\Model\Property\PropertyInterface;
11+
use PHPModelGenerator\Model\Validator;
1112
use PHPModelGenerator\Model\Validator\AbstractComposedPropertyValidator;
13+
use PHPModelGenerator\Model\Validator\PropertyTemplateValidator;
1214
use PHPModelGenerator\Model\Validator\PropertyValidator;
1315
use PHPModelGenerator\PropertyProcessor\ComposedValue\ComposedPropertiesInterface;
1416
use PHPModelGenerator\PropertyProcessor\PropertyCollectionProcessor;
@@ -169,8 +171,8 @@ protected function transferComposedPropertiesToSchema(PropertyInterface $propert
169171
$this->schema->addProperty(
170172
(clone $property)
171173
->setRequired(false)
172-
->filterValidators(function () {
173-
return false;
174+
->filterValidators(function (Validator $validator) {
175+
return is_a($validator->getValidator(), PropertyTemplateValidator::class);
174176
})
175177
);
176178

tests/Basic/BasicSchemaGenerationTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public function testGetterAndSetterAreGenerated(): void
1818
{
1919
$className = $this->generateClassFromFile('BasicSchema.json');
2020

21-
$object = new $className([]);
21+
$object = new $className(['property' => 'Hello']);
2222

2323
$this->assertTrue(is_callable([$object, 'getProperty']));
2424
$this->assertTrue(is_callable([$object, 'setProperty']));
25-
$this->assertNull($object->getProperty());
25+
$this->assertSame('Hello', $object->getProperty());
2626
}
2727

2828
public function testImmutableGeneratorDoesntGenerateSetter(): void
@@ -39,6 +39,33 @@ public function testImmutableGeneratorDoesntGenerateSetter(): void
3939
$this->assertNull($object->getProperty());
4040
}
4141

42+
public function testSetterChangeTheInternalState(): void
43+
{
44+
$className = $this->generateClassFromFile('BasicSchema.json');
45+
46+
$object = new $className(['property' => 'Hello']);
47+
48+
$this->assertSame('Hello', $object->getProperty());
49+
$this->assertSame($object, $object->setProperty('ChangedPropertyValue'));
50+
$this->assertSame('ChangedPropertyValue', $object->getProperty());
51+
}
52+
53+
public function testPropertyNamesAreNormalized(): void
54+
{
55+
$className = $this->generateClassFromFile('NameNormalization.json');
56+
$object = new $className([
57+
'underscore_property' => '___',
58+
'minus-property' => '---',
59+
'space property' => ' ',
60+
'numeric42' => 13,
61+
]);
62+
63+
$this->assertSame('___', $object->getUnderscoreProperty());
64+
$this->assertSame('---', $object->getMinusProperty());
65+
$this->assertSame(' ', $object->getSpaceProperty());
66+
$this->assertSame(13, $object->getNumeric42());
67+
}
68+
4269
public function testNamespacePrefix(): void
4370
{
4471
$className = '\\My\\Prefix\\' . $this->generateClassFromFile(

tests/ComposedValue/ComposedAllOfTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,30 @@ public function validComposedObjectWithRequiredPropertiesDataProvider(): array
317317
'only string property with additional property' => [['stringProperty' => 'B', 'test' => 1234], 'B', null],
318318
];
319319
}
320+
321+
/**
322+
* @dataProvider nestedObjectDataProvider
323+
*
324+
* @param string $schema
325+
*/
326+
public function testObjectLevelCompositionArrayWithNestedObject(string $schema)
327+
{
328+
$className = $this->generateClassFromFile($schema);
329+
330+
$object = new $className(['name' => 'Hannes', 'cars' => [['ps' => 112]]]);
331+
332+
$this->assertSame('Hannes', $object->getName());
333+
$this->assertIsArray($object->getCars());
334+
$this->assertCount(1, $object->getCars());
335+
$this->assertIsObject($object->getCars()[0]);
336+
$this->assertSame(112, $object->getCars()[0]->getPs());
337+
}
338+
339+
public function nestedObjectDataProvider()
340+
{
341+
return [
342+
['ObjectLevelCompositionNestedObject.json'],
343+
['ObjectLevelNestedCompositionNestedObject.json'],
344+
];
345+
}
320346
}

tests/ComposedValue/ComposedAnyOfTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function annotationDataProvider(): array
9494
{
9595
return [
9696
'Multiple scalar types' => ['AnyOfType.json', '/string\|int\|bool/'],
97-
'Object with scalar type' => ['ReferencedObjectSchema.json', '/string\|ComposedAnyOfTest[\w]*_Merged_[\w]*/'],
97+
'Object with scalar type' => ['ReferencedObjectSchema.json', '/string\|Composed[\w]*_Merged_[\w]*/'],
9898
'Multiple objects' => ['ReferencedObjectSchema2.json', '/ComposedAnyOfTest[\w]*_Merged_[\w]*/']
9999
];
100100
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace PHPModelGenerator\Tests\ComposedValue;
4+
5+
use PHPModelGenerator\Exception\InvalidArgumentException;
6+
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
7+
8+
/**
9+
* Class ComposedIfTest
10+
*
11+
* @package PHPModelGenerator\Tests\ComposedValue
12+
*/
13+
class ComposedIfTest extends AbstractPHPModelGeneratorTest
14+
{
15+
/**
16+
* @dataProvider validConditionalObjectPropertyDataProvider
17+
*
18+
* @param string $streetAddress
19+
* @param string $country
20+
* @param string $postalCode
21+
*/
22+
public function testConditionalObjectProperty(?string $streetAddress, ?string $country, ?string $postalCode): void
23+
{
24+
$className = $this->generateClassFromFile('ConditionalObjectProperty.json');
25+
26+
$object = new $className([
27+
'street_address' => $streetAddress,
28+
'country' => $country,
29+
'postal_code' => $postalCode,
30+
]);
31+
32+
$this->assertSame($streetAddress, $object->getStreetAddress());
33+
$this->assertSame($country, $object->getCountry());
34+
$this->assertSame($postalCode, $object->getPostalCode());
35+
}
36+
37+
public function validConditionalObjectPropertyDataProvider(): array
38+
{
39+
return [
40+
'not provided postal code' => ['1600 Pennsylvania Avenue NW', 'USA', null],
41+
'USA postal code' => ['1600 Pennsylvania Avenue NW', 'USA', '20500'],
42+
'Canada postal code' => ['24 Sussex Drive', 'Canada', 'K1M 1M4'],
43+
];
44+
}
45+
46+
/**
47+
* @dataProvider invalidConditionalObjectPropertyDataProvider
48+
*
49+
* @param string $streetAddress
50+
* @param string $country
51+
* @param string $postalCode
52+
*/
53+
public function testInvalidConditionalObjectPropertyThrowsAnException(
54+
?string $streetAddress,
55+
?string $country,
56+
?string $postalCode
57+
): void {
58+
$this->expectException(InvalidArgumentException::class);
59+
$this->expectExceptionMessageRegExp('/postal_code doesn\'t match pattern .*/');
60+
61+
$className = $this->generateClassFromFile('ConditionalObjectProperty.json');
62+
63+
new $className([
64+
'street_address' => $streetAddress,
65+
'country' => $country,
66+
'postal_code' => $postalCode,
67+
]);
68+
}
69+
70+
public function invalidConditionalObjectPropertyDataProvider(): array
71+
{
72+
return [
73+
'empty provided postal code' => ['1600 Pennsylvania Avenue NW', 'USA', ''],
74+
'Canadian postal code for USA' => ['1600 Pennsylvania Avenue NW', 'USA', 'K1M 1M4'],
75+
'USA postal code for Canada' => ['24 Sussex Drive', 'Canada', '20500'],
76+
'Unmatching postal code for both' => ['24 Sussex Drive', 'Canada', 'djqwWDJId8juw9duq9'],
77+
];
78+
}
79+
}

tests/ComposedValue/ComposedOneOfTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public function annotationDataProvider(): array
9292
{
9393
return [
9494
'Multiple scalar types' => ['OneOfType.json', '/string\|int\|bool/'],
95-
'Object with scalar type' => ['ReferencedObjectSchema.json', '/ComposedOneOfTest[\w]*\|string/'],
96-
'Multiple objects' => ['ReferencedObjectSchema2.json', '/ComposedOneOfTest[\w]*\|ComposedOneOfTest[\w]*/']
95+
'Object with scalar type' => ['ReferencedObjectSchema.json', '/Composed[\w]*\|string/'],
96+
'Multiple objects' => ['ReferencedObjectSchema2.json', '/Composed[\w]*\|Composed[\w]*/']
9797
];
9898
}
9999

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"underscore_property": {
5+
"type": "string"
6+
},
7+
"CAPS": {
8+
"type": "string"
9+
},
10+
"minus-property": {
11+
"type": "string"
12+
},
13+
"space property": {
14+
"type": "string"
15+
},
16+
"numeric42": {
17+
"type": "integer"
18+
}
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"allOf": [
3+
{
4+
"type": "object",
5+
"id": "Owner",
6+
"properties": {
7+
"name": {
8+
"type": "string"
9+
},
10+
"cars" : {
11+
"type": "array",
12+
"items": {
13+
"type": "object",
14+
"id": "Car",
15+
"properties": {
16+
"ps": {
17+
"type": "integer"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
]
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"allOf": [
3+
{
4+
"allOf": [
5+
{
6+
"type": "object",
7+
"id": "Owner",
8+
"properties": {
9+
"name": {
10+
"type": "string"
11+
},
12+
"cars" : {
13+
"type": "array",
14+
"items": {
15+
"type": "object",
16+
"id": "Car",
17+
"properties": {
18+
"ps": {
19+
"type": "integer"
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}
26+
]
27+
}
28+
]
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"street_address": {
5+
"type": "string"
6+
},
7+
"country": {
8+
"enum": [
9+
"USA",
10+
"Canada"
11+
]
12+
}
13+
},
14+
"if": {
15+
"properties": {
16+
"country": {
17+
"const": "USA"
18+
}
19+
}
20+
},
21+
"then": {
22+
"properties": {
23+
"postal_code": {
24+
"type": "string",
25+
"pattern": "[0-9]{5}(-[0-9]{4})?"
26+
}
27+
}
28+
},
29+
"else": {
30+
"properties": {
31+
"postal_code": {
32+
"type": "string",
33+
"pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]"
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)