Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/generator/default/faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function generateModel($attributes = [])
{
$faker = $this->faker;
$uniqueFaker = $this->uniqueFaker;
$model = new <?= $model->getClassName() ?>();
$model = new <?= $modelClass ?>();
<?php foreach ($model->attributes as $attribute):
if (!$attribute->fakerStub || $attribute->isReference()) {
if (!$attribute->fakerStub /*|| $attribute->isReference()*/) {
continue;
}
?>
Expand All @@ -62,4 +62,17 @@ public function generateModel($attributes = [])
}
return $model;
}
<?php if ($model->hasOneRelations):?>

public static function dependentOn()
{
return [
// just model class names
<?php foreach ($model->hasOneRelations as $key => $hasOneRelation): ?>
<?php echo \yii\helpers\VarDumper::export($model->hasOneRelations[$key]->getClassName()).','.PHP_EOL ?>
<?php endforeach; ?>

];
}
<?php endif;?>
}
13 changes: 9 additions & 4 deletions src/lib/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace cebe\yii2openapi\lib;

use cebe\yii2openapi\lib\Config;
use cebe\yii2openapi\lib\CustomSpecAttr;
use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException;
use cebe\yii2openapi\lib\items\Attribute;
Expand Down Expand Up @@ -66,20 +67,24 @@ class AttributeResolver
*/
private $junctions;

/**@var bool */
/** @var bool */
private $isJunctionSchema;

/**@var bool */
/** @var bool */
private $hasMany2Many;

public function __construct(string $schemaName, ComponentSchema $schema, JunctionSchemas $junctions)
/** @var Config */
private $config;

public function __construct(string $schemaName, ComponentSchema $schema, JunctionSchemas $junctions, ?Config $config = null)
{
$this->schemaName = $schemaName;
$this->schema = $schema;
$this->tableName = $schema->resolveTableName($schemaName);
$this->junctions = $junctions;
$this->isJunctionSchema = $junctions->isJunctionSchema($schemaName);
$this->hasMany2Many = $junctions->hasMany2Many($schemaName);
$this->config = $config;
}

/**
Expand Down Expand Up @@ -391,7 +396,7 @@ protected function catchManyToMany(
*/
protected function guessFakerStub(Attribute $attribute, PropertySchema $property):?string
{
$resolver = Yii::createObject(['class' => FakerStubResolver::class], [$attribute, $property]);
$resolver = Yii::createObject(['class' => FakerStubResolver::class], [$attribute, $property, $this->config]);
return $resolver->resolve();
}

Expand Down
1 change: 0 additions & 1 deletion src/lib/CustomSpecAttr.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ class CustomSpecAttr
*/
public const FK_ON_DELETE = 'x-fk-on-delete';
public const FK_ON_UPDATE = 'x-fk-on-update';

}
14 changes: 13 additions & 1 deletion src/lib/FakerStubResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ class FakerStubResolver
*/
private $property;

public function __construct(Attribute $attribute, PropertySchema $property)
/** @var Config */
private $config;

public function __construct(Attribute $attribute, PropertySchema $property, ?Config $config = null)
{
$this->attribute = $attribute;
$this->property = $property;
$this->config = $config;
}

public function resolve():?string
Expand All @@ -46,6 +50,14 @@ public function resolve():?string
if ($this->attribute->isReadOnly() && $this->attribute->isVirtual()) {
return null;
}

// column name ends with `_id`
if (substr($this->attribute->columnName, -strlen('_id'))==='_id') {
return '$faker->randomElement(\\'.$this->config->modelNamespace
. ($this->config->modelNamespace ? '\\' : '')
. ucfirst($this->attribute->reference).'::find()->select("id")->column())';
}

$limits = $this->attribute->limits;
switch ($this->attribute->phpType) {
case 'bool':
Expand Down
2 changes: 1 addition & 1 deletion src/lib/SchemaToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function prepareModels():array
$schemaName = $junctions->trimPrefix($schemaName);
}
/**@var \cebe\yii2openapi\lib\AttributeResolver $resolver */
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions]);
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config]);
$models[$schemaName] = $resolver->resolve();
}
foreach ($models as $model) {
Expand Down
2 changes: 1 addition & 1 deletion tests/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function checkFiles(array $actual, array $expected)
self::assertFileExists($file);
self::assertFileExists($expectedFilePath);

$this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath");
$this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath \n\n cp $file $expectedFilePath \n\n ");
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@
->asReference('Category')
->setRequired()
->setDescription('Category of posts')
->setFakerStub('$uniqueFaker->numberBetween(0, 1000000)'),
->setFakerStub('$faker->randomElement(\Category::find()->select("id")->column())'),
'created_at' => (new Attribute('created_at', ['phpType' => 'string', 'dbType' => 'date']))
->setFakerStub('$faker->dateTimeThisCentury->format(\'Y-m-d\')'),
'created_by' => (new Attribute('created_by', ['phpType' => 'int', 'dbType' => 'integer']))
->asReference('User')
->setDescription('The User')
->setFakerStub('$uniqueFaker->numberBetween(0, 1000000)'),
->setFakerStub('$faker->randomElement(\User::find()->select("id")->column())'),
],
'relations' => [
'category' => new AttributeRelation('category',
Expand Down Expand Up @@ -110,12 +110,12 @@
->setSize(128)
->asReference('Post')
->setDescription('A blog post (uid used as pk for test purposes)')
->setFakerStub('substr($uniqueFaker->sha256, 0, 128)'),
->setFakerStub('$faker->randomElement(\Post::find()->select("id")->column())'),
'author' => (new Attribute('author', ['phpType' => 'int', 'dbType' => 'integer']))
->setRequired()
->asReference('User')
->setDescription('The User')
->setFakerStub('$uniqueFaker->numberBetween(0, 1000000)'),
->setFakerStub('$faker->randomElement(\User::find()->select("id")->column())'),
'message' => (new Attribute('message', ['phpType' => 'array', 'dbType' => 'json', 'xDbType' => 'json']))
->setRequired()->setDefault([])->setFakerStub('[]'),
'meta_data' => (new Attribute('meta_data', ['phpType' => 'array', 'dbType' => 'json', 'xDbType' => 'json']))
Expand Down
12 changes: 12 additions & 0 deletions tests/specs/blog/models/CommentFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function generateModel($attributes = [])
$uniqueFaker = $this->uniqueFaker;
$model = new Comment();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->post_id = $faker->randomElement(\app\models\Post::find()->select("id")->column());
$model->author_id = $faker->randomElement(\app\models\User::find()->select("id")->column());
$model->message = [];
$model->meta_data = [];
$model->created_at = $faker->unixTime;
Expand All @@ -40,4 +42,14 @@ public function generateModel($attributes = [])
}
return $model;
}

public static function dependentOn()
{
return [
// just model class names
'Post',
'User',

];
}
}
12 changes: 12 additions & 0 deletions tests/specs/blog/models/PostFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,25 @@ public function generateModel($attributes = [])
$model->uid = substr($uniqueFaker->sha256, 0, 128);
$model->title = substr($faker->sentence, 0, 255);
$model->slug = substr($uniqueFaker->slug, 0, 200);
$model->category_id = $faker->randomElement(\app\models\Category::find()->select("id")->column());
$model->active = $faker->boolean;
$model->created_at = $faker->dateTimeThisCentury->format('Y-m-d');
$model->created_by_id = $faker->randomElement(\app\models\User::find()->select("id")->column());
if (!is_callable($attributes)) {
$model->setAttributes($attributes, false);
} else {
$model = $attributes($model, $faker, $uniqueFaker);
}
return $model;
}

public static function dependentOn()
{
return [
// just model class names
'Category',
'User',

];
}
}
12 changes: 12 additions & 0 deletions tests/specs/blog_v2/models/CommentFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function generateModel($attributes = [])
$uniqueFaker = $this->uniqueFaker;
$model = new Comment();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->post_id = $faker->randomElement(\app\models\Post::find()->select("id")->column());
$model->user_id = $faker->randomElement(\app\models\User::find()->select("id")->column());
$model->message = $faker->sentence;
$model->meta_data = substr($faker->text(300), 0, 300);
$model->created_at = $faker->dateTimeThisYear('now', 'UTC')->format('Y-m-d H:i:s');
Expand All @@ -40,4 +42,14 @@ public function generateModel($attributes = [])
}
return $model;
}

public static function dependentOn()
{
return [
// just model class names
'Post',
'User',

];
}
}
12 changes: 12 additions & 0 deletions tests/specs/blog_v2/models/PostFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,25 @@ public function generateModel($attributes = [])
$model->title = substr($faker->sentence, 0, 255);
$model->slug = substr($uniqueFaker->slug, 0, 200);
$model->lang = $faker->randomElement(['ru','eng']);
$model->category_id = $faker->randomElement(\app\models\Category::find()->select("id")->column());
$model->active = $faker->boolean;
$model->created_at = $faker->dateTimeThisCentury->format('Y-m-d');
$model->created_by_id = $faker->randomElement(\app\models\User::find()->select("id")->column());
if (!is_callable($attributes)) {
$model->setAttributes($attributes, false);
} else {
$model = $attributes($model, $faker, $uniqueFaker);
}
return $model;
}

public static function dependentOn()
{
return [
// just model class names
'Category',
'User',

];
}
}
12 changes: 12 additions & 0 deletions tests/specs/many2many/models/Photos2PostsFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@ public function generateModel($attributes = [])
$uniqueFaker = $this->uniqueFaker;
$model = new Photos2Posts();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->photo_id = $faker->randomElement(\app\models\Photo::find()->select("id")->column());
$model->post_id = $faker->randomElement(\app\models\Post::find()->select("id")->column());
if (!is_callable($attributes)) {
$model->setAttributes($attributes, false);
} else {
$model = $attributes($model, $faker, $uniqueFaker);
}
return $model;
}

public static function dependentOn()
{
return [
// just model class names
'Photo',
'Post',

];
}
}
12 changes: 12 additions & 0 deletions tests/specs/many2many/models/PostsAttachesFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@ public function generateModel($attributes = [])
$uniqueFaker = $this->uniqueFaker;
$model = new PostsAttaches();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->attach_id = $faker->randomElement(\app\models\Photo::find()->select("id")->column());
$model->target_id = $faker->randomElement(\app\models\Post::find()->select("id")->column());
if (!is_callable($attributes)) {
$model->setAttributes($attributes, false);
} else {
$model = $attributes($model, $faker, $uniqueFaker);
}
return $model;
}

public static function dependentOn()
{
return [
// just model class names
'Photo',
'Post',

];
}
}
12 changes: 12 additions & 0 deletions tests/specs/many2many/models/PostsGalleryFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function generateModel($attributes = [])
$faker = $this->faker;
$uniqueFaker = $this->uniqueFaker;
$model = new PostsGallery();
$model->image_id = $faker->randomElement(\app\models\Photo::find()->select("id")->column());
$model->article_id = $faker->randomElement(\app\models\Post::find()->select("id")->column());
$model->is_cover = $faker->boolean;
if (!is_callable($attributes)) {
$model->setAttributes($attributes, false);
Expand All @@ -37,4 +39,14 @@ public function generateModel($attributes = [])
}
return $model;
}

public static function dependentOn()
{
return [
// just model class names
'Photo',
'Post',

];
}
}
10 changes: 10 additions & 0 deletions tests/specs/menu/models/MenuFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function generateModel($attributes = [])
$model = new Menu();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->name = substr($faker->text(100), 0, 100);
$model->parent_id = $faker->randomElement(\app\models\Menu::find()->select("id")->column());
$model->args = [];
$model->kwargs = [];
if (!is_callable($attributes)) {
Expand All @@ -40,4 +41,13 @@ public function generateModel($attributes = [])
}
return $model;
}

public static function dependentOn()
{
return [
// just model class names
'Menu',

];
}
}
10 changes: 10 additions & 0 deletions tests/specs/petstore/models/PetFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function generateModel($attributes = [])
$model = new Pet();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->name = $faker->sentence;
$model->store_id = $faker->randomElement(\app\models\Store::find()->select("id")->column());
$model->tag = $faker->randomElement(['one', 'two', 'three', 'four']);
if (!is_callable($attributes)) {
$model->setAttributes($attributes, false);
Expand All @@ -39,4 +40,13 @@ public function generateModel($attributes = [])
}
return $model;
}

public static function dependentOn()
{
return [
// just model class names
'Store',

];
}
}
Loading