Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 181f712

Browse files
committed
Fix #162 - WIP
1 parent fbaa3bc commit 181f712

File tree

8 files changed

+121
-1
lines changed

8 files changed

+121
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
/.php_cs.cache
55
/.env
66
/.phpunit.result.cache
7+
8+
/.idea

src/lib/AttributeResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ protected function resolveProperty(
225225
->setReadOnly($property->isReadonly())
226226
->setDefault($property->guessDefault())
227227
->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE))
228+
// ->setXFaker($property->getAttr(CustomSpecAttr::FAKER))
228229
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
229230
->setNullable($nullableValue)
230231
->setIsPrimary($property->isPrimaryKey())

src/lib/FakerStubResolver.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,24 @@ public function __construct(Attribute $attribute, PropertySchema $property, ?Con
4444

4545
public function resolve():?string
4646
{
47+
// if ($this->property->getName() === 'invoice') {
48+
// var_dump($this->property->xFaker); die();
49+
// }
50+
if ($this->property->xFaker === false) {
51+
$this->attribute->setFakerStub(null);
52+
return null;
53+
}
4754
if ($this->property->hasAttr(CustomSpecAttr::FAKER)) {
4855
return $this->property->getAttr(CustomSpecAttr::FAKER);
4956
}
57+
// if ($this->property->hasAttr(CustomSpecAttr::FAKER) || $this->property->xFaker) {
58+
// $fakerValueInYml = $this->property->getAttr(CustomSpecAttr::FAKER) || $this->property->xFaker;
59+
// if ($fakerValueInYml === false) {
60+
// $this->attribute->setFakerStub(null);
61+
// return null;
62+
// }
63+
// return $fakerValueInYml;
64+
// }
5065
if ($this->attribute->isReadOnly() && $this->attribute->isVirtual()) {
5166
return null;
5267
}

src/lib/items/Attribute.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class Attribute extends BaseObject
7171
*/
7272
public $xDbType;
7373

74+
// public $xFaker;
75+
7476
/**
7577
* nullable
7678
* bool | null
@@ -154,6 +156,12 @@ public function setXDbType($xDbType):Attribute
154156
return $this;
155157
}
156158

159+
// public function setXFaker($xFaker):Attribute
160+
// {
161+
// $this->xFaker = $xFaker;
162+
// return $this;
163+
// }
164+
157165
public function setXDbDefaultExpression($xDbDefaultExpression): Attribute
158166
{
159167
// first priority is given to `default` and then to `x-db-default-expression`

src/lib/openapi/PropertySchema.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class PropertySchema
4646
*/
4747
public $fkColName;
4848

49+
public $xFaker;
50+
4951
/**
5052
* @var \cebe\openapi\SpecObjectInterface
5153
*/
@@ -93,7 +95,7 @@ public function __construct(SpecObjectInterface $property, string $name, Compone
9395
$this->schema = $schema;
9496
$this->isPk = $name === $schema->getPkName();
9597

96-
$onUpdate = $onDelete = $reference = $fkColName = null;
98+
$onUpdate = $onDelete = $xFaker = $reference = $fkColName = null;
9799

98100
foreach ($property->allOf ?? [] as $element) {
99101
// x-fk-on-delete | x-fk-on-update
@@ -103,6 +105,14 @@ public function __construct(SpecObjectInterface $property, string $name, Compone
103105
if (!empty($element->{CustomSpecAttr::FK_ON_DELETE})) {
104106
$onDelete = $element->{CustomSpecAttr::FK_ON_DELETE};
105107
}
108+
109+
if ($element->{CustomSpecAttr::FAKER} !== null) {
110+
$xFaker = $element->{CustomSpecAttr::FAKER};
111+
}
112+
// if (property_exists($element, CustomSpecAttr::FAKER)) {
113+
// $xFaker = $element->{CustomSpecAttr::FAKER};
114+
// }
115+
106116
if ($element instanceof Reference) {
107117
$reference = $element;
108118
}
@@ -128,6 +138,10 @@ public function __construct(SpecObjectInterface $property, string $name, Compone
128138
$this->fkColName = $fkColName;
129139
$this->property = $reference;
130140
$property = $this->property;
141+
} elseif($xFaker !== null && $reference instanceof Reference) {
142+
$this->xFaker = $xFaker;
143+
$this->property = $reference;
144+
$property = $this->property;
131145
}
132146

133147
if ($property instanceof Reference) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/162_bug_dollarref_with_x_faker/162_bug_dollarref_with_x_faker.yaml',
5+
'generateUrls' => false,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => true,
13+
];
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
openapi: 3.0.3
2+
# Edit this schema and start your project
3+
# This is sample schema
4+
# To generate code which is based on this schema
5+
# run commands mentioned Development section in README.md file
6+
info:
7+
title: 'Proxy-Service'
8+
description: ""
9+
version: 1.0.0
10+
contact:
11+
name: '...'
12+
email: you@example.com
13+
paths:
14+
/:
15+
get:
16+
summary: List
17+
operationId: list
18+
responses:
19+
'200':
20+
description: The information
21+
servers:
22+
- url: 'http://localhost:9937'
23+
description: 'Local Dev API'
24+
security:
25+
- BasicAuth: []
26+
components:
27+
securitySchemes:
28+
BasicAuth:
29+
type: http
30+
scheme: basic
31+
schemas:
32+
33+
Invoice:
34+
type: object
35+
required:
36+
- id
37+
properties:
38+
id:
39+
type: integer
40+
41+
Order:
42+
type: object
43+
required:
44+
- id
45+
properties:
46+
id:
47+
type: integer
48+
invoice:
49+
# type: integer
50+
# x-faker: 5
51+
allOf:
52+
- $ref: '#/components/schemas/Invoice'
53+
- x-faker: false
54+

tests/unit/IssueFixTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,17 @@ public function testNullableFalseInRequired()
272272
]);
273273
$this->checkFiles($actualFiles, $expectedFiles);
274274
}
275+
276+
public function test162BugDollarrefWithXFaker()
277+
{
278+
$testFile = Yii::getAlias("@specs/issue_fix/162_bug_dollarref_with_x_faker/162_bug_dollarref_with_x_faker.php");
279+
$this->runGenerator($testFile, 'mysql');
280+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
281+
'recursive' => true,
282+
]);
283+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/153_nullable_false_in_required/app"), [
284+
// 'recursive' => true,
285+
// ]);
286+
// $this->checkFiles($actualFiles, $expectedFiles);
287+
}
275288
}

0 commit comments

Comments
 (0)