Skip to content

Commit 8fd5b8b

Browse files
committed
Add more tests
1 parent 3815cb3 commit 8fd5b8b

File tree

2 files changed

+132
-10
lines changed
  • tests

2 files changed

+132
-10
lines changed

tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,38 @@ components:
88
schemas:
99
Fruit:
1010
type: object
11-
# x-description-is-comment: true
1211
properties:
1312
id:
1413
type: integer
1514
name:
1615
type: string
1716
description: desc with ' quote
18-
x-description-is-comment: true
1917
description:
2018
type: number
2119
x-db-type: double precision
2220
description: desc ' 2
23-
x-description-is-comment: true
2421
Animal:
2522
type: object
26-
# x-description-is-comment: true
2723
properties:
2824
id:
2925
type: integer
3026
name:
3127
type: integer
32-
x-description-is-comment: true
3328
g:
3429
type: string
3530
description: desc for g
36-
x-description-is-comment: true
3731
g2:
3832
type: string
3933
description: changed comment on g2 col
40-
x-description-is-comment: true
4134
g3:
4235
type: string
4336
description: the comment on g3 col remains same
44-
x-description-is-comment: true
4537
g4:
4638
type: integer
4739
description: data type changes but comment remains same
48-
x-description-is-comment: true
4940
new_col:
5041
type: string
5142
description: new col added
52-
x-description-is-comment: true
5343

5444
paths:
5545
'/':

tests/unit/IssueFixTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,138 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC
580580
$this->deleteTableFor60DescriptionOfAProperty();
581581
}
582582

583+
// https://github.com/php-openapi/yii2-openapi/issues/60
584+
public function test60ComponentSchemaLevelExtension()
585+
{
586+
587+
588+
$schema = <<<YAML
589+
openapi: 3.0.3
590+
info:
591+
title: 'test60ComponentSchemaLevelExtension'
592+
version: 1.0.0
593+
components:
594+
schemas:
595+
Fruit:
596+
type: object
597+
x-description-is-comment: true
598+
properties:
599+
id:
600+
type: integer
601+
name:
602+
type: string
603+
nullable: false
604+
description: Hi there
605+
paths:
606+
'/':
607+
get:
608+
responses:
609+
'200':
610+
description: OK
611+
YAML;
612+
613+
$expected = <<<'PHP'
614+
<?php
615+
616+
/**
617+
* Table for Fruit
618+
*/
619+
class m200000_000000_change_table_fruits extends \yii\db\Migration
620+
{
621+
public function up()
622+
{
623+
$this->alterColumn('{{%fruits}}', 'name', $this->text()->notNull()->comment('Hi there'));
624+
}
625+
626+
public function down()
627+
{
628+
$this->alterColumn('{{%fruits}}', 'name', $this->text()->null());
629+
}
630+
}
631+
632+
PHP;
633+
634+
$this->for60($schema, $expected);
635+
}
636+
637+
// https://github.com/php-openapi/yii2-openapi/issues/60
638+
public function test60PropertyLevelExtension()
639+
{
640+
$schema = <<<YAML
641+
openapi: 3.0.3
642+
info:
643+
title: 'test60ComponentSchemaLevelExtension'
644+
version: 1.0.0
645+
components:
646+
schemas:
647+
Fruit:
648+
type: object
649+
properties:
650+
id:
651+
type: integer
652+
name:
653+
type: string
654+
nullable: false
655+
x-description-is-comment: true
656+
description: Hi there
657+
paths:
658+
'/':
659+
get:
660+
responses:
661+
'200':
662+
description: OK
663+
YAML;
664+
665+
$expected = <<<'PHP'
666+
<?php
667+
668+
/**
669+
* Table for Fruit
670+
*/
671+
class m200000_000000_change_table_fruits extends \yii\db\Migration
672+
{
673+
public function up()
674+
{
675+
$this->alterColumn('{{%fruits}}', 'name', $this->text()->notNull()->comment('Hi there'));
676+
}
677+
678+
public function down()
679+
{
680+
$this->alterColumn('{{%fruits}}', 'name', $this->text()->null());
681+
}
682+
}
683+
684+
PHP;
685+
686+
$this->for60($schema, $expected);
687+
}
688+
689+
private function for60($spec, $expected)
690+
{
691+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
692+
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
693+
'id' => 'pk',
694+
'name' => 'text',
695+
])->execute();
696+
$config = [
697+
'openApiPath' => 'data://text/plain;base64,' . base64_encode($spec),
698+
'generateUrls' => false,
699+
'generateModels' => false,
700+
'generateControllers' => false,
701+
'generateMigrations' => true,
702+
'generateModelFaker' => false,
703+
];
704+
$tmpConfigFile = Yii::getAlias("@runtime") . "/tmp-config.php";
705+
file_put_contents($tmpConfigFile, '<?php return ' . var_export($config, true) . ';');
706+
707+
708+
$this->runGenerator($tmpConfigFile);
709+
$actual = file_get_contents(Yii::getAlias('@app') . '/migrations_mysql_db/m200000_000000_change_table_fruits.php');
710+
$this->assertSame($expected, $actual);
711+
$this->runActualMigrations('mysql', 1);
712+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
713+
}
714+
583715
private function createTableFor60DescriptionOfAProperty()
584716
{
585717
Yii::$app->db->createCommand()->createTable('{{%animals}}', [

0 commit comments

Comments
 (0)