Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve: Invalid schema reference error #75

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Complete test
  • Loading branch information
SOHELAHMED7 committed Jan 7, 2025
commit 064673a29c64f097d889f1133e809d508f8d0b74
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* Table for Invoice
*/
class m200000_000000_create_table_invoices extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%invoices}}', [
'id' => $this->primaryKey(),
'vat_rate' => 'enum("standard", "none") NOT NULL DEFAULT \'standard\'',
]);
}

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

/**
* Table for Product
*/
class m200000_000001_create_table_products extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%products}}', [
'id' => $this->primaryKey(),
'vat_rate' => 'enum("standard", "none") NOT NULL DEFAULT \'standard\'',
]);
}

public function down()
{
$this->dropTable('{{%products}}');
}
}
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 Invoice extends \app\models\base\Invoice
{


}

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 Invoice
* @method static Invoice makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Invoice saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Invoice[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
* @method static Invoice[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
*/
class InvoiceFaker extends BaseModelFaker
{

/**
* @param array|callable $attributes
* @return Invoice|\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 Invoice();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->vat_rate = $faker->randomElement(['standard','none']);
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,10 @@
<?php

namespace app\models;

class Product extends \app\models\base\Product
{


}

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 Product
* @method static Product makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Product saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Product[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
* @method static Product[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
*/
class ProductFaker extends BaseModelFaker
{

/**
* @param array|callable $attributes
* @return Product|\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 Product();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->vat_rate = $faker->randomElement(['standard','none']);
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

/**
* This file is generated by Gii, do not change manually!
*/

namespace app\models\base;

/**
* This is the model class for table "invoices".
*
* @property int $id
* @property string $vat_rate
*
*/
abstract class Invoice extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%invoices}}';
}

public function rules()
{
return [
'required' => [['vat_rate'], 'required'],
'vat_rate_string' => [['vat_rate'], 'string'],
'vat_rate_in' => [['vat_rate'], 'in', 'range' => [
'standard',
'none',
]],
'vat_rate_default' => [['vat_rate'], 'default', 'value' => 'standard'],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* This file is generated by Gii, do not change manually!
*/

namespace app\models\base;

/**
* This is the model class for table "products".
*
* @property int $id
* @property string $vat_rate
*
*/
abstract class Product extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%products}}';
}

public function rules()
{
return [
'required' => [['vat_rate'], 'required'],
'vat_rate_string' => [['vat_rate'], 'string'],
'vat_rate_in' => [['vat_rate'], 'in', 'range' => [
'standard',
'none',
]],
'vat_rate_default' => [['vat_rate'], 'default', 'value' => 'standard'],
];
}
}
25 changes: 8 additions & 17 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -966,24 +966,15 @@ public function test63JustColumnNameRename()
// https://github.com/php-openapi/yii2-openapi/issues/63
public function test74InvalidSchemaReferenceError()
{
// $pattern = '~^(?<schemaName>.+)(\.+)(yaml+)(.*)$~';
// $pattern = '/((\.\/)*)(?<schemaName>.+)(\.)(yml|yaml)(.*)/';
//
// $pointer = './Product.yaml#/properties/vat_rate';
// preg_match($pattern, $pointer, $matches);
// $this->assertSame('supda saf', $matches);
// return;

$testFile = Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/index.php");

$this->runGenerator($testFile);
// $this->runActualMigrations('mysql', 1);
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
// 'recursive' => true,
// ]);
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/mysql"), [
// 'recursive' => true,
// ]);
// $this->checkFiles($actualFiles, $expectedFiles);
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
'recursive' => true,
]);
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/mysql"), [
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations();
}
}
Loading