Skip to content
Merged
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
Next Next commit
Add tests
  • Loading branch information
SOHELAHMED7 committed Jan 3, 2023
commit dcbde27ae9f367f14f8c90b62e83cfc2c425323f
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,3 @@ Professional support, consulting as well as software development services are av
https://www.cebe.cc/en/contact

Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud).

2 changes: 1 addition & 1 deletion tests/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use yii\db\mysql\Schema as MySqlSchema;
use yii\db\pgsql\Schema as PgSqlSchema;
use \SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
use yii\helpers\ArrayHelper;
use yii\helpers\{ArrayHelper, VarDumper};
use yii\helpers\FileHelper;

class DbTestCase extends \PHPUnit\Framework\TestCase
Expand Down
13 changes: 13 additions & 0 deletions tests/specs/issue_fix/camel_case_127/camel_case_127.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/camel_case_127/camel_case_127.yaml',
'generateUrls' => false,
'generateModels' => false,
'excludeModels' => [
'Error',
],
'generateControllers' => false,
'generateMigrations' => true,
'generateModelFaker' => false,
];
28 changes: 28 additions & 0 deletions tests/specs/issue_fix/camel_case_127/camel_case_127.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Camel case column name issue \#127
paths:
/:
get:
summary: List
operationId: list
responses:
'200':
description: The information

components:
schemas:
Pristine:
type: object
description: Camel Case column name test
required:
- id
properties:
id:
type: integer
firstName:
type: string
newColumn:
type: string
x-db-type: varchar(255)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Table for Pristine
*/
class m200000_000000_create_table_pristines extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%pristines}}', [
'id' => $this->primaryKey(),
'firstName' => $this->text()->null(),
0 => 'newColumn varchar(255) NULL DEFAULT NULL',
]);
}

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

/**
* Table for Pristine
*/
class m200000_000000_create_table_pristines extends \yii\db\Migration
{
public function safeUp()
{
$this->createTable('{{%pristines}}', [
'id' => $this->primaryKey(),
'firstName' => $this->text()->null()->defaultValue(null),
0 => '"newColumn" varchar(255) NULL DEFAULT NULL',
]);
}

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

/**
* Table for Fruit
*/
class m200000_000000_change_table_fruits extends \yii\db\Migration
{
public function safeUp()
{
$this->alterColumn('{{%fruits}}', 'colourName', 'text NULL USING "colourName"::text');
}

public function safeDown()
{
$this->alterColumn('{{%fruits}}', 'colourName', 'varchar(255) NULL USING "colourName"::varchar');
}
}
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 Fruit extends \app\models\base\Fruit
{


}

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

/**
* @param array|callable $attributes
* @return Fruit|\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 Fruit();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->colourName = $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,26 @@
<?php

namespace app\models\base;

/**
* Quote in alter column in Pgsql test
*
* @property int $id
* @property string $colourName
*
*/
abstract class Fruit extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%fruits}}';
}

public function rules()
{
return [
'trim' => [['colourName'], 'trim'],
'colourName_string' => [['colourName'], 'string'],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.yaml',
'generateUrls' => false,
'generateModels' => true,
'excludeModels' => [
'Error',
],
'generateControllers' => false,
'generateMigrations' => true,
'generateModelFaker' => true,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Quote in alter column in Pgsql
paths:
/:
get:
summary: List
operationId: list
responses:
'200':
description: The information

components:
schemas:
Fruit:
type: object
description: Quote in alter column in Pgsql test
required:
- id
properties:
id:
type: integer
colourName:
type: string
# x-db-type: pg_lsn
Loading