Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SOHELAHMED7 committed Nov 21, 2024
1 parent 3815cb3 commit 8fd5b8b
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,38 @@ components:
schemas:
Fruit:
type: object
# x-description-is-comment: true
properties:
id:
type: integer
name:
type: string
description: desc with ' quote
x-description-is-comment: true
description:
type: number
x-db-type: double precision
description: desc ' 2
x-description-is-comment: true
Animal:
type: object
# x-description-is-comment: true
properties:
id:
type: integer
name:
type: integer
x-description-is-comment: true
g:
type: string
description: desc for g
x-description-is-comment: true
g2:
type: string
description: changed comment on g2 col
x-description-is-comment: true
g3:
type: string
description: the comment on g3 col remains same
x-description-is-comment: true
g4:
type: integer
description: data type changes but comment remains same
x-description-is-comment: true
new_col:
type: string
description: new col added
x-description-is-comment: true

paths:
'/':
Expand Down
132 changes: 132 additions & 0 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,138 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC
$this->deleteTableFor60DescriptionOfAProperty();
}

// https://github.com/php-openapi/yii2-openapi/issues/60
public function test60ComponentSchemaLevelExtension()
{


$schema = <<<YAML
openapi: 3.0.3
info:
title: 'test60ComponentSchemaLevelExtension'
version: 1.0.0
components:
schemas:
Fruit:
type: object
x-description-is-comment: true
properties:
id:
type: integer
name:
type: string
nullable: false
description: Hi there
paths:
'/':
get:
responses:
'200':
description: OK
YAML;

$expected = <<<'PHP'
<?php
/**
* Table for Fruit
*/
class m200000_000000_change_table_fruits extends \yii\db\Migration
{
public function up()
{
$this->alterColumn('{{%fruits}}', 'name', $this->text()->notNull()->comment('Hi there'));
}
public function down()
{
$this->alterColumn('{{%fruits}}', 'name', $this->text()->null());
}
}

PHP;

$this->for60($schema, $expected);
}

// https://github.com/php-openapi/yii2-openapi/issues/60
public function test60PropertyLevelExtension()
{
$schema = <<<YAML
openapi: 3.0.3
info:
title: 'test60ComponentSchemaLevelExtension'
version: 1.0.0
components:
schemas:
Fruit:
type: object
properties:
id:
type: integer
name:
type: string
nullable: false
x-description-is-comment: true
description: Hi there
paths:
'/':
get:
responses:
'200':
description: OK
YAML;

$expected = <<<'PHP'
<?php
/**
* Table for Fruit
*/
class m200000_000000_change_table_fruits extends \yii\db\Migration
{
public function up()
{
$this->alterColumn('{{%fruits}}', 'name', $this->text()->notNull()->comment('Hi there'));
}
public function down()
{
$this->alterColumn('{{%fruits}}', 'name', $this->text()->null());
}
}

PHP;

$this->for60($schema, $expected);
}

private function for60($spec, $expected)
{
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
'id' => 'pk',
'name' => 'text',
])->execute();
$config = [
'openApiPath' => 'data://text/plain;base64,' . base64_encode($spec),
'generateUrls' => false,
'generateModels' => false,
'generateControllers' => false,
'generateMigrations' => true,
'generateModelFaker' => false,
];
$tmpConfigFile = Yii::getAlias("@runtime") . "/tmp-config.php";
file_put_contents($tmpConfigFile, '<?php return ' . var_export($config, true) . ';');


$this->runGenerator($tmpConfigFile);
$actual = file_get_contents(Yii::getAlias('@app') . '/migrations_mysql_db/m200000_000000_change_table_fruits.php');
$this->assertSame($expected, $actual);
$this->runActualMigrations('mysql', 1);
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
}

private function createTableFor60DescriptionOfAProperty()
{
Yii::$app->db->createCommand()->createTable('{{%animals}}', [
Expand Down

0 comments on commit 8fd5b8b

Please sign in to comment.