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

Commit ac4ae33

Browse files
committed
added tests for generating relations
1 parent 427533f commit ac4ae33

File tree

15 files changed

+171
-3
lines changed

15 files changed

+171
-3
lines changed

src/generator/ApiGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ public function generate()
692692
'description' => $model->description,
693693
'attributes' => $model->attributes,
694694
'relations' => $model->relations,
695+
'relationNamespace' => $this->modelNamespace,
695696
])
696697
);
697698
if ($this->generateModelFaker) {

src/generator/default/dbmodel.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@
77
* <?= str_replace("\n", "\n * ", trim($description)) ?>
88

99
*
10-
<?php foreach ($attributes as $attribute): ?>
10+
<?php foreach ($attributes as $attribute):
11+
if (\yii\helpers\StringHelper::endsWith($attribute['name'], '_id') && isset($relations[substr($attribute['name'], 0, -3)])) {
12+
// TODO this change should be made in SchemaToDatabase class
13+
$attribute['type'] = 'int';
14+
}
15+
?>
1116
* @property <?= $attribute['type'] ?? 'mixed' ?> $<?= str_replace("\n", "\n * ", rtrim($attribute['name'] . ' ' . $attribute['description'])) ?>
1217

18+
<?php endforeach; ?>
19+
*
20+
<?php foreach ($relations as $relationName => $relation): ?>
21+
* @property \<?= trim($relationNamespace, '\\') ?>\<?= $relation['class'] ?> $<?= $relationName ?>
22+
1323
<?php endforeach; ?>
1424
*/
1525
abstract class <?= $className ?> extends \yii\db\ActiveRecord
@@ -32,6 +42,9 @@ public function rules()
3242
if ($attribute['readOnly']) {
3343
continue;
3444
}
45+
if (\yii\helpers\StringHelper::endsWith($attribute['name'], '_id') && isset($relations[substr($attribute['name'], 0, -3)])) {
46+
continue;
47+
}
3548
if ($attribute['required']) {
3649
$requiredAttributes[$attribute['name']] = $attribute['name'];
3750
}
@@ -72,7 +85,7 @@ public function rules()
7285
<?php foreach ($relations as $relationName => $relation): ?>
7386
public function get<?= ucfirst($relationName) ?>()
7487
{
75-
return $this-><?= $relation['method'] ?>(<?= $relation['class'] ?>::class, <?php
88+
return $this-><?= $relation['method'] ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation['class'] ?>::class, <?php
7689
echo str_replace(
7790
[',', '=>', ', ]'],
7891
[', ', ' => ', ']'],

tests/GeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testGenerate($testFile)
6363
$actualFile = str_replace('@app', Yii::getAlias('@app'), $file);
6464
$this->assertFileExists($expectedFile);
6565
$this->assertFileExists($actualFile);
66-
$this->assertFileEquals($expectedFile, $actualFile);
66+
$this->assertFileEquals($expectedFile, $actualFile, "Failed asserting that file contents of\n$actualFile\nare equal to file contents of\n$expectedFile");
6767
}
6868
}
6969
}

tests/specs/petstore.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,23 @@ components:
140140
readOnly: True
141141
name:
142142
type: string
143+
store:
144+
$ref: '#/components/schemas/Store'
143145
tag:
144146
type: string
145147
x-faker: "$faker->randomElement(['one', 'two', 'three', 'four'])"
148+
Store:
149+
description: A store's description
150+
required:
151+
- id
152+
- name
153+
properties:
154+
id:
155+
type: integer
156+
format: int64
157+
readOnly: True
158+
name:
159+
type: string
146160
Pets:
147161
type: array
148162
items:

tests/specs/petstore/models/Store.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Store extends \app\models\base\Store
6+
{
7+
8+
9+
}
10+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
use Faker\Factory as FakerFactory;
6+
use Faker\UniqueGenerator;
7+
8+
/**
9+
* Fake data generator for Store
10+
*/
11+
class StoreFaker
12+
{
13+
public function generateModel()
14+
{
15+
$faker = FakerFactory::create(\Yii::$app->language);
16+
$uniqueFaker = new UniqueGenerator($faker);
17+
$model = new Store;
18+
$model->id = $uniqueFaker->numberBetween(0, 2147483647);
19+
$model->name = $faker->sentence;
20+
return $model;
21+
}
22+
}

tests/specs/petstore/models/base/Pet.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
*
88
* @property int $id
99
* @property string $name
10+
* @property int $store_id A store's description
1011
* @property string $tag
12+
*
13+
* @property \app\models\Store $store
1114
*/
1215
abstract class Pet extends \yii\db\ActiveRecord
1316
{
@@ -25,4 +28,9 @@ public function rules()
2528
];
2629
}
2730

31+
public function getStore()
32+
{
33+
return $this->hasOne(\app\models\Store::class, ['id' => 'store_id']);
34+
}
35+
2836
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace app\models\base;
4+
5+
/**
6+
* A store's description
7+
*
8+
* @property int $id
9+
* @property string $name
10+
*
11+
*/
12+
abstract class Store extends \yii\db\ActiveRecord
13+
{
14+
public static function tableName()
15+
{
16+
return '{{%stores}}';
17+
}
18+
19+
public function rules()
20+
{
21+
return [
22+
[['name'], 'trim'],
23+
[['name'], 'required'],
24+
[['name'], 'string'],
25+
];
26+
}
27+
28+
}

tests/specs/petstore_arrayref/models/base/Pet.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @property int $id
99
* @property string $name
1010
* @property string $tag
11+
*
1112
*/
1213
abstract class Pet extends \yii\db\ActiveRecord
1314
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\mymodels;
4+
5+
class Store extends \app\mymodels\base\Store
6+
{
7+
8+
9+
}
10+

0 commit comments

Comments
 (0)