Skip to content

Commit b3c660b

Browse files
committed
Implement oneOf and refactor - WIP 3
1 parent 9f83e78 commit b3c660b

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

src/lib/FakerStubResolver.php

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ private function fakeForInt(?int $min, ?int $max): ?string
207207
$fakerVariable = 'uniqueFaker';
208208
}
209209
if ($min !== null && $max !== null) {
210-
return "\$$fakerVariable->numberBetween($min, $max)";
210+
return "\${$fakerVariable}->numberBetween($min, $max)";
211211
}
212212

213213
if ($min !== null) {
214-
return "\$$fakerVariable->numberBetween($min, " . self::MAX_INT . ")";
214+
return "\${$fakerVariable}->numberBetween($min, " . self::MAX_INT . ")";
215215
}
216216

217217
if ($max !== null) {
218-
return "\$$fakerVariable->numberBetween(0, $max)";
218+
return "\${$fakerVariable}->numberBetween(0, $max)";
219219
}
220220

221221
$patterns = [
@@ -229,7 +229,7 @@ private function fakeForInt(?int $min, ?int $max): ?string
229229
return $fake;
230230
}
231231
}
232-
return "\$$fakerVariable->numberBetween(0, " . self::MAX_INT . ")";
232+
return "\${$fakerVariable}->numberBetween(0, " . self::MAX_INT . ")";
233233
}
234234

235235
private function fakeForFloat(?int $min, ?int $max): ?string
@@ -247,6 +247,7 @@ private function fakeForFloat(?int $min, ?int $max): ?string
247247
}
248248

249249
/**
250+
* @param int $count let's set a number to default number of elements
250251
* @throws InvalidConfigException
251252
* @throws TypeErrorException
252253
* @throws UnresolvableReferenceException
@@ -255,8 +256,6 @@ private function fakeForFloat(?int $min, ?int $max): ?string
255256
private function fakeForArray(SpecObjectInterface $property, int $count = 4): string
256257
{
257258
$uniqueItems = false;
258-
$arbitrary = false;
259-
$type = null;
260259
if ($property->minItems) {
261260
$count = $property->minItems;
262261
}
@@ -272,41 +271,26 @@ private function fakeForArray(SpecObjectInterface $property, int $count = 4): st
272271

273272
// TODO consider example of OpenAPI spec
274273

275-
// $count = 4; # let's set a number to default number of elements
276-
277274
/** @var Schema|Reference|null $items */
278-
$items = $property->items ?? $property; # later is used in `oneOf`
275+
$items = $property->items; # later is used only in `oneOf`
279276

280-
$aElementData = Json::decode(Json::encode($this->property->getProperty()->getSerializableData()));
281-
$compoSchemaArr = [
282-
'properties' => [
283-
'unnamedProp' => $aElementData['items']
284-
]
285-
];
277+
if (!$items) {
278+
return $this->arbitraryArray();
279+
}
286280

287-
if ($items) {
288-
if ($items instanceof Reference) {
289-
$class = str_replace('#/components/schemas/', '', $items->getReference());
290-
$class .= 'Faker';
291-
return $this->wrapAsArray('(new ' . $class . ')->generateModel()->attributes', false, $count);
292-
} elseif (!empty($items->oneOf)) {
293-
return $this->handleOneOf($items, $count);
294-
} else {
295-
$type = $items->type;
296-
if ($type === null) {
297-
$arbitrary = true;
298-
}
299-
$cs = new ComponentSchema(new Schema($compoSchemaArr), 'UnnamedCompo');
300-
$dbModels = (new AttributeResolver('UnnamedCompo', $cs, new JunctionSchemas([])))->resolve();
301-
$aElementFaker = (new static($dbModels->attributes['unnamedProp'], $cs->getProperty('unnamedProp')))->resolve();
302-
}
303-
} else {
304-
$arbitrary = true;
281+
if ($items instanceof Reference) {
282+
$class = str_replace('#/components/schemas/', '', $items->getReference());
283+
$class .= 'Faker';
284+
return $this->wrapAsArray('(new ' . $class . ')->generateModel()->attributes', false, $count);
285+
} elseif (!empty($items->oneOf)) {
286+
return $this->handleOneOf($items, $count);
305287
}
306288

307-
if ($arbitrary) {
308-
return '$faker->words()';
289+
$type = $items->type;
290+
if ($type === null) {
291+
return $this->arbitraryArray();
309292
}
293+
$aElementFaker = $this->aElementFaker();
310294

311295
if (in_array($type, ['string', 'number', 'integer', 'boolean'])) {
312296
return $this->wrapAsArray($aElementFaker, $uniqueItems, $count);
@@ -390,7 +374,8 @@ public function handleOneOf($items, $count): string
390374
foreach ($items->oneOf as $key => $aDataType) {
391375
/** @var Schema|Reference $aDataType */
392376

393-
$a1 = $this->fakeForArray($aDataType, 1);
377+
// $a1 = $this->fakeForArray($aDataType, 1);
378+
$a1 = $this->aElementFaker();
394379
$result .= '$dataType' . $key . ' = ' . $a1 . ';';
395380
}
396381
$ct = count($items->oneOf) - 1;
@@ -405,4 +390,22 @@ public function wrapAsArray($aElementFaker, $uniqueItems, $count): string
405390
return ' . ($uniqueItems ? str_replace('$faker->', '$uniqueFaker->', $aElementFaker) : $aElementFaker) . ';
406391
}, range(1, ' . $count . '))';
407392
}
393+
394+
public function arbitraryArray(): string
395+
{
396+
return '$faker->words()';
397+
}
398+
399+
public function aElementFaker(): ?string
400+
{
401+
$aElementData = Json::decode(Json::encode($this->property->getProperty()->getSerializableData()));
402+
$compoSchemaData = [
403+
'properties' => [
404+
'unnamedProp' => $aElementData['items']
405+
]
406+
];
407+
$cs = new ComponentSchema(new Schema($compoSchemaData), 'UnnamedCompo');
408+
$dbModels = (new AttributeResolver('UnnamedCompo', $cs, new JunctionSchemas([])))->resolve();
409+
return (new static($dbModels->attributes['unnamedProp'], $cs->getProperty('unnamedProp')))->resolve();
410+
}
408411
}

tests/specs/issue_fix/20_consider_openapi_spec_examples_in_faker_code_generation/index.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ components:
155155
maxItems: 8
156156
items:
157157
oneOf:
158-
- type: string
159158
- type: integer
159+
- type: string
160160

161161

162162
# oneOf

0 commit comments

Comments
 (0)