Skip to content

Commit 3bc42ff

Browse files
committed
Complete the test
1 parent 7a761f9 commit 3bc42ff

File tree

8 files changed

+355
-7
lines changed

8 files changed

+355
-7
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
use Faker\Factory as FakerFactory;
6+
use Faker\Generator;
7+
use Faker\UniqueGenerator;
8+
9+
/**
10+
* Base fake data generator
11+
*/
12+
abstract class BaseModelFaker
13+
{
14+
/**
15+
* @var Generator
16+
*/
17+
protected $faker;
18+
/**
19+
* @var UniqueGenerator
20+
*/
21+
protected $uniqueFaker;
22+
23+
public function __construct()
24+
{
25+
$this->faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language));
26+
$this->uniqueFaker = new UniqueGenerator($this->faker);
27+
}
28+
29+
abstract public function generateModel($attributes = []);
30+
31+
public function getFaker():Generator
32+
{
33+
return $this->faker;
34+
}
35+
36+
public function getUniqueFaker():UniqueGenerator
37+
{
38+
return $this->uniqueFaker;
39+
}
40+
41+
public function setFaker(Generator $faker):void
42+
{
43+
$this->faker = $faker;
44+
}
45+
46+
public function setUniqueFaker(UniqueGenerator $faker):void
47+
{
48+
$this->uniqueFaker = $faker;
49+
}
50+
51+
/**
52+
* Generate and return model
53+
* @param array|callable $attributes
54+
* @param UniqueGenerator|null $uniqueFaker
55+
* @return \yii\db\ActiveRecord
56+
* @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']);
57+
* @example MyFaker::makeOne( function($model, $faker) {
58+
* $model->scenario = 'create';
59+
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]);
60+
* return $model;
61+
* });
62+
*/
63+
public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null)
64+
{
65+
$fakeBuilder = new static();
66+
if ($uniqueFaker !== null) {
67+
$fakeBuilder->setUniqueFaker($uniqueFaker);
68+
}
69+
$model = $fakeBuilder->generateModel($attributes);
70+
return $model;
71+
}
72+
73+
/**
74+
* Generate, save and return model
75+
* @param array|callable $attributes
76+
* @param UniqueGenerator|null $uniqueFaker
77+
* @return \yii\db\ActiveRecord
78+
* @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']);
79+
* @example MyFaker::saveOne( function($model, $faker) {
80+
* $model->scenario = 'create';
81+
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]);
82+
* return $model;
83+
* });
84+
*/
85+
public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null)
86+
{
87+
$model = static::makeOne($attributes, $uniqueFaker);
88+
$model->save();
89+
return $model;
90+
}
91+
92+
/**
93+
* Generate and return multiple models
94+
* @param int $number
95+
* @param array|callable $commonAttributes
96+
* @return \yii\db\ActiveRecord[]|array
97+
* @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]);
98+
* @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) {
99+
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]);
100+
* return $model;
101+
* });
102+
*/
103+
public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array
104+
{
105+
if ($number < 1) {
106+
return [];
107+
}
108+
$fakeBuilder = new static();
109+
if ($uniqueFaker !== null) {
110+
$fakeBuilder->setUniqueFaker($uniqueFaker);
111+
}
112+
return array_map(function () use ($commonAttributes, $fakeBuilder) {
113+
$model = $fakeBuilder->generateModel($commonAttributes);
114+
return $model;
115+
}, range(0, $number -1));
116+
}
117+
118+
/**
119+
* Generate, save and return multiple models
120+
* @param int $number
121+
* @param array|callable $commonAttributes
122+
* @return \yii\db\ActiveRecord[]|array
123+
* @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]);
124+
* @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) {
125+
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]);
126+
* return $model;
127+
* });
128+
*/
129+
public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array
130+
{
131+
if ($number < 1) {
132+
return [];
133+
}
134+
$fakeBuilder = new static();
135+
if ($uniqueFaker !== null) {
136+
$fakeBuilder->setUniqueFaker($uniqueFaker);
137+
}
138+
return array_map(function () use ($commonAttributes, $fakeBuilder) {
139+
$model = $fakeBuilder->generateModel($commonAttributes);
140+
$model->save();
141+
return $model;
142+
}, range(0, $number -1));
143+
}
144+
}
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 Invoice extends \app\models\base\Invoice
6+
{
7+
8+
9+
}
10+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace app\models;
3+
4+
use Faker\UniqueGenerator;
5+
6+
/**
7+
* Fake data generator for Invoice
8+
* @method static Invoice makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
9+
* @method static Invoice saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
10+
* @method static Invoice[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
11+
* @method static Invoice[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
12+
*/
13+
class InvoiceFaker extends BaseModelFaker
14+
{
15+
16+
/**
17+
* @param array|callable $attributes
18+
* @return Invoice|\yii\db\ActiveRecord
19+
* @example
20+
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
21+
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
22+
* $model->scenario = 'create';
23+
* $model->author_id = 1;
24+
* return $model;
25+
* });
26+
**/
27+
public function generateModel($attributes = [])
28+
{
29+
$faker = $this->faker;
30+
$uniqueFaker = $this->uniqueFaker;
31+
$model = new Invoice();
32+
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
33+
$model->reference_invoice_2_id = $faker->randomElement(\app\models\Invoice::find()->select("id")->column());
34+
$model->user_id = $faker->randomElement(\app\models\Invoice::find()->select("id")->column());
35+
if (!is_callable($attributes)) {
36+
$model->setAttributes($attributes, false);
37+
} else {
38+
$model = $attributes($model, $faker, $uniqueFaker);
39+
}
40+
return $model;
41+
}
42+
43+
public static function dependentOn()
44+
{
45+
return [
46+
// just model class names
47+
48+
];
49+
}
50+
}
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 User extends \app\models\base\User
6+
{
7+
8+
9+
}
10+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace app\models;
3+
4+
use Faker\UniqueGenerator;
5+
6+
/**
7+
* Fake data generator for User
8+
* @method static User makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
9+
* @method static User saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
10+
* @method static User[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
11+
* @method static User[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
12+
*/
13+
class UserFaker extends BaseModelFaker
14+
{
15+
16+
/**
17+
* @param array|callable $attributes
18+
* @return User|\yii\db\ActiveRecord
19+
* @example
20+
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
21+
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
22+
* $model->scenario = 'create';
23+
* $model->author_id = 1;
24+
* return $model;
25+
* });
26+
**/
27+
public function generateModel($attributes = [])
28+
{
29+
$faker = $this->faker;
30+
$uniqueFaker = $this->uniqueFaker;
31+
$model = new User();
32+
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
33+
$model->name = $faker->sentence;
34+
if (!is_callable($attributes)) {
35+
$model->setAttributes($attributes, false);
36+
} else {
37+
$model = $attributes($model, $faker, $uniqueFaker);
38+
}
39+
return $model;
40+
}
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* This file is generated by Gii, do not change manually!
5+
*/
6+
7+
namespace app\models\base;
8+
9+
/**
10+
* This is the model class for table "invoices".
11+
*
12+
* @property int $id
13+
* @property int $reference_invoice_id
14+
* @property int $reference_invoice_2_id
15+
* @property int $user_id
16+
* @property int $user_2_id
17+
*
18+
* @property \app\models\Invoice $referenceInvoice
19+
* @property \app\models\Invoice $referenceInvoice2
20+
* @property \app\models\Invoice $user
21+
* @property \app\models\Invoice $user2
22+
*/
23+
abstract class Invoice extends \yii\db\ActiveRecord
24+
{
25+
public static function tableName()
26+
{
27+
return '{{%invoices}}';
28+
}
29+
30+
public function rules()
31+
{
32+
return [
33+
'reference_invoice_id_integer' => [['reference_invoice_id'], 'integer'],
34+
'reference_invoice_id_exist' => [['reference_invoice_id'], 'exist', 'targetRelation' => 'ReferenceInvoice'],
35+
'reference_invoice_2_id_integer' => [['reference_invoice_2_id'], 'integer'],
36+
'reference_invoice_2_id_exist' => [['reference_invoice_2_id'], 'exist', 'targetRelation' => 'ReferenceInvoice2'],
37+
'user_id_integer' => [['user_id'], 'integer'],
38+
'user_id_exist' => [['user_id'], 'exist', 'targetRelation' => 'User'],
39+
'user_2_id_integer' => [['user_2_id'], 'integer'],
40+
'user_2_id_exist' => [['user_2_id'], 'exist', 'targetRelation' => 'User2'],
41+
];
42+
}
43+
44+
public function getReferenceInvoice()
45+
{
46+
return $this->hasOne(\app\models\Invoice::class, ['id' => 'reference_invoice_id']);
47+
}
48+
49+
public function getReferenceInvoice2()
50+
{
51+
return $this->hasOne(\app\models\Invoice::class, ['id' => 'reference_invoice_2_id']);
52+
}
53+
54+
public function getUser()
55+
{
56+
return $this->hasOne(\app\models\Invoice::class, ['id' => 'user_id']);
57+
}
58+
59+
public function getUser2()
60+
{
61+
return $this->hasOne(\app\models\Invoice::class, ['id' => 'user_2_id']);
62+
}
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* This file is generated by Gii, do not change manually!
5+
*/
6+
7+
namespace app\models\base;
8+
9+
/**
10+
* This is the model class for table "users".
11+
*
12+
* @property int $id
13+
* @property string $name
14+
*
15+
*/
16+
abstract class User extends \yii\db\ActiveRecord
17+
{
18+
public static function tableName()
19+
{
20+
return '{{%users}}';
21+
}
22+
23+
public function rules()
24+
{
25+
return [
26+
'trim' => [['name'], 'trim'],
27+
'name_string' => [['name'], 'string'],
28+
];
29+
}
30+
}

tests/unit/IssueFixTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,12 @@ public function test52BugDependentonAllofWithXFakerFalse()
366366
{
367367
$testFile = Yii::getAlias("@specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.php");
368368
$this->runGenerator($testFile);
369-
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
370-
// 'recursive' => true,
371-
// ]);
372-
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql"), [
373-
// 'recursive' => true,
374-
// ]);
375-
// $this->checkFiles($actualFiles, $expectedFiles);
369+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
370+
'recursive' => true,
371+
]);
372+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql"), [
373+
'recursive' => true,
374+
]);
375+
$this->checkFiles($actualFiles, $expectedFiles);
376376
}
377377
}

0 commit comments

Comments
 (0)