Skip to content

Commit 18f84e3

Browse files
committed
Add passing test 2
1 parent 2558069 commit 18f84e3

File tree

10 files changed

+315
-9
lines changed

10 files changed

+315
-9
lines changed

tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
return [
4-
'openApiPath' => '@specs/issue_fix/162_bug_dollarref_with_x_faker/162_bug_dollarref_with_x_faker.yaml',
4+
'openApiPath' => '@specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/index.yaml',
55
'generateUrls' => true,
66
'generateModels' => true,
77
'excludeModels' => [
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* OpenAPI UrlRules
4+
*
5+
* This file is auto generated.
6+
*/
7+
return [
8+
'GET account/<accountId:\d+>/contacts' => 'contact/list-for-account',
9+
'GET account/<accountId:\d+>/contacts/<contactId:\d+>' => 'contact/view-for-account',
10+
'account/<accountId:\d+>/contacts' => 'contact/options',
11+
'account/<accountId:\d+>/contacts/<contactId:\d+>' => 'contact/options',
12+
];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
class ContactController extends \app\controllers\base\ContactController
6+
{
7+
8+
public function checkAccess($action, $model = null, $params = [])
9+
{
10+
//TODO implement checkAccess
11+
}
12+
13+
public function actionListForAccount($accountId)
14+
{
15+
//TODO implement actionListForAccount
16+
}
17+
18+
public function actionViewForAccount($accountId, $contactId)
19+
{
20+
//TODO implement actionViewForAccount
21+
}
22+
23+
24+
}
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace app\controllers\base;
4+
5+
abstract class ContactController extends \yii\rest\Controller
6+
{
7+
public function actions()
8+
{
9+
return [
10+
'options' => [
11+
'class' => \yii\rest\OptionsAction::class,
12+
],
13+
];
14+
}
15+
16+
/**
17+
* Checks the privilege of the current user.
18+
*
19+
* This method checks whether the current user has the privilege
20+
* to run the specified action against the specified data model.
21+
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
22+
*
23+
* @param string $action the ID of the action to be executed
24+
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
25+
* @param array $params additional parameters
26+
* @throws \yii\web\ForbiddenHttpException if the user does not have access
27+
*/
28+
abstract public function checkAccess($action, $model = null, $params = []);
29+
30+
abstract public function actionListForAccount($accountId);
31+
32+
abstract public function actionViewForAccount($accountId, $contactId);
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* Table for Contact
5+
*/
6+
class m200000_000000_create_table_contacts extends \yii\db\Migration
7+
{
8+
public function up()
9+
{
10+
$this->createTable('{{%contacts}}', [
11+
'id' => $this->primaryKey(),
12+
]);
13+
}
14+
15+
public function down()
16+
{
17+
$this->dropTable('{{%contacts}}');
18+
}
19+
}
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 Contact extends \app\models\base\Contact
6+
{
7+
8+
9+
}
10+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace app\models;
3+
4+
use Faker\UniqueGenerator;
5+
6+
/**
7+
* Fake data generator for Contact
8+
* @method static Contact makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
9+
* @method static Contact saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
10+
* @method static Contact[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
11+
* @method static Contact[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
12+
*/
13+
class ContactFaker extends BaseModelFaker
14+
{
15+
16+
/**
17+
* @param array|callable $attributes
18+
* @return Contact|\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 Contact();
32+
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
33+
if (!is_callable($attributes)) {
34+
$model->setAttributes($attributes, false);
35+
} else {
36+
$model = $attributes($model, $faker, $uniqueFaker);
37+
}
38+
return $model;
39+
}
40+
}
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\base;
4+
5+
/**
6+
*
7+
*
8+
* @property int $id
9+
*
10+
*/
11+
abstract class Contact extends \yii\db\ActiveRecord
12+
{
13+
public static function tableName()
14+
{
15+
return '{{%contacts}}';
16+
}
17+
18+
public function rules()
19+
{
20+
return [];
21+
}
22+
}

tests/unit/IssueFixTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,13 @@ public function test162BugDollarrefWithXFaker()
291291
public function test163GeneratorCrashWhenUsingReferenceInsideAnObject()
292292
{
293293
$testFile = Yii::getAlias("@specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/index.php");
294-
$this->runGenerator($testFile, 'mysql');
295-
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
296-
// 'recursive' => true,
297-
// ]);
298-
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/app"), [
299-
// 'recursive' => true,
300-
// ]);
301-
// $this->checkFiles($actualFiles, $expectedFiles);
294+
$this->runGenerator($testFile, 'pgsql');
295+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
296+
'recursive' => true,
297+
]);
298+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql"), [
299+
'recursive' => true,
300+
]);
301+
$this->checkFiles($actualFiles, $expectedFiles);
302302
}
303303
}

0 commit comments

Comments
 (0)