Skip to content

Commit

Permalink
Complete the test
Browse files Browse the repository at this point in the history
  • Loading branch information
SOHELAHMED7 committed Sep 6, 2024
1 parent be59c82 commit 65fa9b1
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* Table for User
*/
class m200000_000000_create_table_users extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%users}}', [
'id' => $this->primaryKey(),
'name' => $this->text()->null(),
]);
}

public function down()
{
$this->dropTable('{{%users}}');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Table for Post
*/
class m200000_000001_create_table_posts extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%posts}}', [
'id' => $this->primaryKey(),
'content' => $this->text()->null(),
'user' => $this->integer()->null()->defaultValue(null),
]);
$this->addForeignKey('fk_posts_user_users_id', '{{%posts}}', 'user', '{{%users}}', 'id');
}

public function down()
{
$this->dropForeignKey('fk_posts_user_users_id', '{{%posts}}');
$this->dropTable('{{%posts}}');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace app\models;

use Faker\Factory as FakerFactory;
use Faker\Generator;
use Faker\UniqueGenerator;

/**
* Base fake data generator
*/
abstract class BaseModelFaker
{
/**
* @var Generator
*/
protected $faker;
/**
* @var UniqueGenerator
*/
protected $uniqueFaker;

public function __construct()
{
$this->faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language));
$this->uniqueFaker = new UniqueGenerator($this->faker);
}

abstract public function generateModel($attributes = []);

public function getFaker():Generator
{
return $this->faker;
}

public function getUniqueFaker():UniqueGenerator
{
return $this->uniqueFaker;
}

public function setFaker(Generator $faker):void
{
$this->faker = $faker;
}

public function setUniqueFaker(UniqueGenerator $faker):void
{
$this->uniqueFaker = $faker;
}

/**
* Generate and return model
* @param array|callable $attributes
* @param UniqueGenerator|null $uniqueFaker
* @return \yii\db\ActiveRecord
* @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']);
* @example MyFaker::makeOne( function($model, $faker) {
* $model->scenario = 'create';
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]);
* return $model;
* });
*/
public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null)
{
$fakeBuilder = new static();
if ($uniqueFaker !== null) {
$fakeBuilder->setUniqueFaker($uniqueFaker);
}
$model = $fakeBuilder->generateModel($attributes);
return $model;
}

/**
* Generate, save and return model
* @param array|callable $attributes
* @param UniqueGenerator|null $uniqueFaker
* @return \yii\db\ActiveRecord
* @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']);
* @example MyFaker::saveOne( function($model, $faker) {
* $model->scenario = 'create';
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]);
* return $model;
* });
*/
public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null)
{
$model = static::makeOne($attributes, $uniqueFaker);
$model->save();
return $model;
}

/**
* Generate and return multiple models
* @param int $number
* @param array|callable $commonAttributes
* @return \yii\db\ActiveRecord[]|array
* @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]);
* @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) {
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]);
* return $model;
* });
*/
public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array
{
if ($number < 1) {
return [];
}
$fakeBuilder = new static();
if ($uniqueFaker !== null) {
$fakeBuilder->setUniqueFaker($uniqueFaker);
}
return array_map(function () use ($commonAttributes, $fakeBuilder) {
$model = $fakeBuilder->generateModel($commonAttributes);
return $model;
}, range(0, $number -1));
}

/**
* Generate, save and return multiple models
* @param int $number
* @param array|callable $commonAttributes
* @return \yii\db\ActiveRecord[]|array
* @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]);
* @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) {
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]);
* return $model;
* });
*/
public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array
{
if ($number < 1) {
return [];
}
$fakeBuilder = new static();
if ($uniqueFaker !== null) {
$fakeBuilder->setUniqueFaker($uniqueFaker);
}
return array_map(function () use ($commonAttributes, $fakeBuilder) {
$model = $fakeBuilder->generateModel($commonAttributes);
$model->save();
return $model;
}, range(0, $number -1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Post extends \app\models\base\Post
{


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace app\models;

use Faker\UniqueGenerator;

/**
* Fake data generator for Post
* @method static Post makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Post saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Post[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
* @method static Post[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
*/
class PostFaker extends BaseModelFaker
{

/**
* @param array|callable $attributes
* @return Post|\yii\db\ActiveRecord
* @example
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
* $model->scenario = 'create';
* $model->author_id = 1;
* return $model;
* });
**/
public function generateModel($attributes = [])
{
$faker = $this->faker;
$uniqueFaker = $this->uniqueFaker;
$model = new Post();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->content = $faker->paragraphs(6, true);
$model->user = $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
'User',

];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class User extends \app\models\base\User
{


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace app\models;

use Faker\UniqueGenerator;

/**
* Fake data generator for User
* @method static User makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static User saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static User[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
* @method static User[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
*/
class UserFaker extends BaseModelFaker
{

/**
* @param array|callable $attributes
* @return User|\yii\db\ActiveRecord
* @example
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
* $model->scenario = 'create';
* $model->author_id = 1;
* return $model;
* });
**/
public function generateModel($attributes = [])
{
$faker = $this->faker;
$uniqueFaker = $this->uniqueFaker;
$model = new User();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->name = $faker->sentence;
if (!is_callable($attributes)) {
$model->setAttributes($attributes, false);
} else {
$model = $attributes($model, $faker, $uniqueFaker);
}
return $model;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace app\models\base;

/**
*
*
* @property int $id
* @property string $content
* @property int $user
*
* @property \app\models\User $userRel
*/
abstract class Post extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%posts}}';
}

public function rules()
{
return [
'trim' => [['content'], 'trim'],
'user_integer' => [['user'], 'integer'],
'user_exist' => [['user'], 'exist', 'targetRelation' => 'userRel'],
'content_string' => [['content'], 'string'],
];
}

public function getUserRel()
{
return $this->hasOne(\app\models\User::class, ['id' => 'user']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace app\models\base;

/**
*
*
* @property int $id
* @property string $name
*
*/
abstract class User extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%users}}';
}

public function rules()
{
return [
'trim' => [['name'], 'trim'],
'name_string' => [['name'], 'string'],
];
}
}
14 changes: 7 additions & 7 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,12 @@ public function test29ExtensionFkColumnNameCauseErrorInCaseOfColumnNameWithoutUn
{
$testFile = Yii::getAlias("@specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/index.php");
$this->runGenerator($testFile);
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
// 'recursive' => true,
// ]);
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql"), [
// 'recursive' => true,
// ]);
// $this->checkFiles($actualFiles, $expectedFiles); // TODO
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
'recursive' => true,
]);
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql"), [
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
}
}

0 comments on commit 65fa9b1

Please sign in to comment.