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: PHP 8.2 compatibility #22

Merged
merged 19 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1'] # TODO add '8.2' https://github.com/cebe/yii2-openapi/issues/142
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']

# TODO use cache
steps:
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ cd yii2-openapi
make clean_all
make up
make installdocker
sudo chmod -R 777 tests/tmp/ # https://github.com/cebe/yii2-openapi/issues/156
make migrate

# to check everything is setup up correctly ensure all tests passes
make cli
# in Docker container
./vendor/bin/phpunit

# create new branch from master and Happy contributing!
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"require": {
"php": ">=7.2.0",
"cebe/php-openapi": "^1.5.0",
"yiisoft/yii2": "~2.0.15",
"yiisoft/yii2": "~2.0.48",
"yiisoft/yii2-gii": "~2.0.0 | ~2.1.0 | ~2.2.0| ~2.3.0",
"laminas/laminas-code": "^3.4 | ^4.0",
"laminas/laminas-code": ">=3.4 <=4.13",
"insolita/yii2-fractal": "^1.0.0",
"fakerphp/faker": "^1.9",
"sam-it/yii2-mariadb": "^2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public static function wrapQuotes(string $str, string $quotes = "'", bool $escap

public static function enumToString(array $enum):string
{
$items = implode(", ", array_map('self::wrapQuotes', $enum));
$items = implode(", ", array_map(self::class.'::wrapQuotes', $enum));
return self::escapeQuotes($items);
}

Expand Down
9 changes: 7 additions & 2 deletions src/lib/FakerStubResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ public function resolve():?string

// column name ends with `_id`
if (substr($this->attribute->columnName, -strlen('_id'))==='_id') {
return '$faker->randomElement(\\'.$this->config->modelNamespace
. ($this->config->modelNamespace ? '\\' : '')
$config = $this->config;
if (!$config) {
$config = new Config;
}
$mn = $config->modelNamespace;
return '$faker->randomElement(\\'.$mn
. ($mn ? '\\' : '')
. ucfirst($this->attribute->reference).'::find()->select("id")->column())';
}

Expand Down
2 changes: 1 addition & 1 deletion tests/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ RUN apt-get update && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
# https://xdebug.org/docs/compat \
&& if [ "$BUILD_PHP_VERSION" = "8.2" ] ; then pecl install xdebug ; else pecl install xdebug-3.1.5 ; fi \
&& if [ "$BUILD_PHP_VERSION" = "8.2" ] || [ "$BUILD_PHP_VERSION" = "8.3" ] ; then pecl install xdebug ; else pecl install xdebug-3.1.5 ; fi \
&& docker-php-ext-enable xdebug \
&& docker-php-ext-install \
zip \
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@
->asReference('Category')
->setRequired()
->setDescription('Category of posts')
->setFakerStub('$faker->randomElement(\Category::find()->select("id")->column())'),
->setFakerStub('$faker->randomElement(\app\models\Category::find()->select("id")->column())'),
'created_at' => (new Attribute('created_at', ['phpType' => 'string', 'dbType' => 'date']))
->setFakerStub('$faker->dateTimeThisCentury->format(\'Y-m-d\')'),
'created_by' => (new Attribute('created_by', ['phpType' => 'int', 'dbType' => 'integer']))
->asReference('User')
->setDescription('The User')
->setFakerStub('$faker->randomElement(\User::find()->select("id")->column())'),
->setFakerStub('$faker->randomElement(\app\models\User::find()->select("id")->column())'),
],
'relations' => [
'category' => new AttributeRelation('category',
Expand Down Expand Up @@ -110,12 +110,12 @@
->setSize(128)
->asReference('Post')
->setDescription('A blog post (uid used as pk for test purposes)')
->setFakerStub('$faker->randomElement(\Post::find()->select("id")->column())'),
->setFakerStub('$faker->randomElement(\app\models\Post::find()->select("id")->column())'),
'author' => (new Attribute('author', ['phpType' => 'int', 'dbType' => 'integer']))
->setRequired()
->asReference('User')
->setDescription('The User')
->setFakerStub('$faker->randomElement(\User::find()->select("id")->column())'),
->setFakerStub('$faker->randomElement(\app\models\User::find()->select("id")->column())'),
'message' => (new Attribute('message', ['phpType' => 'array', 'dbType' => 'json', 'xDbType' => 'json']))
->setRequired()->setDefault([])->setFakerStub('[]'),
'meta_data' => (new Attribute('meta_data', ['phpType' => 'array', 'dbType' => 'json', 'xDbType' => 'json']))
Expand Down
16 changes: 12 additions & 4 deletions tests/unit/MultiDbFreshMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,11 @@ public function testAfterKeyword()
$dbSchema, 'tableName', $columnSchema, false, false
);

$this->assertStringContainsString('AFTER username', $column->getCode());
$this->assertStringNotContainsString('AFTER username', $columnWithoutPreviousCol->getCode());
// https://github.com/cebe/yii2-openapi/pull/141/files#diff-d2ab9925cad7eac58e0ff4cc0d251a937ecf49e4b6bf57f8b95aab76648a9d34R33
$this->assertSame(strpos($column->getCode(), 'AFTER username'), 28);
// $this->assertStringContainsString('AFTER username', $column->getCode());
// $this->assertStringNotContainsString('AFTER username', $columnWithoutPreviousCol->getCode());
$this->assertFalse(strpos($columnWithoutPreviousCol->getCode(), 'AFTER username'));

// test `after` for fluent part in function call `after()`
unset($column, $columnWithoutPreviousCol);
Expand All @@ -198,7 +201,12 @@ public function testAfterKeyword()
$dbSchema, 'tableName', $columnSchema, true, false
);

$this->assertStringContainsString("->after('username')", $column->getCode());
$this->assertStringNotContainsString("->after('username')", $columnWithoutPreviousCol->getCode());
// https://github.com/cebe/yii2-openapi/pull/141/files#diff-d2ab9925cad7eac58e0ff4cc0d251a937ecf49e4b6bf57f8b95aab76648a9d34R33
// $this->assertStringContainsString("->after('username')", $column->getCode());
$this->assertSame(strpos($column->getCode(), "->after('username')"), 41);

// https://github.com/cebe/yii2-openapi/pull/141/files#diff-d2ab9925cad7eac58e0ff4cc0d251a937ecf49e4b6bf57f8b95aab76648a9d34R33
// $this->assertStringNotContainsString("->after('username')", $columnWithoutPreviousCol->getCode());
$this->assertFalse(strpos($columnWithoutPreviousCol->getCode(), "->after('username')"));
}
}
33 changes: 27 additions & 6 deletions tests/unit/SchemaToDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,35 @@ public function testFindJunctionSchemas()
//VarDumper::dump($result->indexByJunctionRef());
//VarDumper::dump($result->indexByJunctionSchema());
self::assertInstanceOf(JunctionSchemas::class, $result);
self::assertEqualsCanonicalizing(
['junction_Photos2Posts', 'junction_PostsGallery', 'junction_PostsAttaches'],
array_keys($result->indexByJunctionSchema())

// https://github.com/cebe/yii2-openapi/pull/141/files#diff-d2ab9925cad7eac58e0ff4cc0d251a937ecf49e4b6bf57f8b95aab76648a9d34R33
// self::assertEqualsCanonicalizing(
// ['junction_Photos2Posts', 'junction_PostsGallery', 'junction_PostsAttaches'],
// array_keys($result->indexByJunctionSchema())
// );
$juncs = ['junction_Photos2Posts', 'junction_PostsGallery', 'junction_PostsAttaches'];
sort($juncs);
$indexedJuncs = array_keys($result->indexByJunctionSchema());
sort($indexedJuncs);
self::assertEquals(
$juncs,
$indexedJuncs
);
self::assertEqualsCanonicalizing(
['Post', 'Photo'],
array_keys($result->indexByClassSchema())

// https://github.com/cebe/yii2-openapi/pull/141/files#diff-d2ab9925cad7eac58e0ff4cc0d251a937ecf49e4b6bf57f8b95aab76648a9d34R33
// self::assertEqualsCanonicalizing(
// ['Post', 'Photo'],
// array_keys($result->indexByClassSchema())
// );
$model = ['Post', 'Photo'];
sort($model);
$indexedModel = array_keys($result->indexByClassSchema());
sort($indexedModel);
self::assertEquals(
$model,
$indexedModel
);

self::assertEquals('junction_PostAttaches', $result->addPrefix('PostAttaches'));
self::assertEquals('PostAttaches', $result->trimPrefix('junction_PostAttaches'));
self::assertTrue($result->isJunctionSchema('PostsAttaches'));
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/XDbTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,8 @@ private function createTableForEditColumns()

public function testValidationRules()
{
$this->deleteTables();

// remove
// $this->removeStaleMigrationsRecords();

$this->deleteTables();
$testFile = Yii::getAlias("@specs/x_db_type/rules_and_more/x_db_type_mysql.php");
$this->runGenerator($testFile, 'mysql');
Expand Down