From 3ad1ddd05a314cf0d9e03e8f4efb7915c0ff0213 Mon Sep 17 00:00:00 2001 From: Imants Horsts Date: Tue, 28 Apr 2020 10:19:00 +0000 Subject: [PATCH 01/21] Revert "Apply fixes from StyleCI" This reverts commit 1fb7b82906e744ec85cafcc5974cb69eda057a55. --- demos/modelmigrator.php | 2 +- src/Migration.php | 22 +++++++++++----------- src/Migration/PgSQL.php | 10 +++++----- src/MigratorConsole.php | 2 +- src/PhpunitTestCase.php | 2 +- tests/ModelTest.php | 6 +++--- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/demos/modelmigrator.php b/demos/modelmigrator.php index b88c415..b57a734 100644 --- a/demos/modelmigrator.php +++ b/demos/modelmigrator.php @@ -21,7 +21,7 @@ public function init(): void // ok, now we surely have DB! $user->save([ - 'name' => 'John' . rand(1, 100), + 'name' => 'John'.rand(1, 100), ]); } catch (\atk4\core\Exception $e) { echo $e->getColorfulText(); diff --git a/src/Migration.php b/src/Migration.php index 3b6ed77..4c3d3bf 100644 --- a/src/Migration.php +++ b/src/Migration.php @@ -478,9 +478,9 @@ public function run(): string if ($changes) { $this->alter(); - return 'added ' . $added . ' field' . ($added == 1 ? '' : 's') . ', ' . - 'changed ' . $altered . ' field' . ($altered == 1 ? '' : 's') . ' and ' . - 'deleted ' . $dropped . ' field' . ($dropped == 1 ? '' : 's'); + return 'added '.$added.' field'.($added == 1 ? '' : 's').', '. + 'changed '.$altered.' field'.($altered == 1 ? '' : 's').' and '. + 'deleted '.$dropped.' field'.($dropped == 1 ? '' : 's'); } return 'no changes'; @@ -497,19 +497,19 @@ public function _render_statements(): string if (isset($this->args['dropField'])) { foreach ($this->args['dropField'] as $field => $junk) { - $result[] = 'drop column ' . $this->_escape($field); + $result[] = 'drop column '.$this->_escape($field); } } if (isset($this->args['newField'])) { foreach ($this->args['newField'] as $field => $option) { - $result[] = 'add column ' . $this->_render_one_field($field, $option); + $result[] = 'add column '.$this->_render_one_field($field, $option); } } if (isset($this->args['alterField'])) { foreach ($this->args['alterField'] as $field => $option) { - $result[] = 'change column ' . $this->_escape($field) . ' ' . $this->_render_one_field($field, $option); + $result[] = 'change column '.$this->_escape($field).' '.$this->_render_one_field($field, $option); } } @@ -656,7 +656,7 @@ public function getSQLFieldType(?string $type, array $options = []): ?string $res = $a[0]; if (count($a) > 1) { - $res .= ' (' . implode(',', array_slice($a, 1)) . ')'; + $res .= ' ('.implode(',', array_slice($a, 1)).')'; } if (!empty($options['ref_type']) && $options['ref_type'] !== self::REF_TYPE_NONE && $type === 'integer') { @@ -671,7 +671,7 @@ public function getSQLFieldType(?string $type, array $options = []): ?string } if (!empty($options['ref_type']) && $options['ref_type'] === self::REF_TYPE_PRIMARY) { - $res .= ' ' . $this->primary_key_expr; + $res .= ' '.$this->primary_key_expr; } return $res; @@ -792,7 +792,7 @@ public function _render_field() foreach ($this->args['field'] as $field => $options) { if ($options instanceof Expression) { - $ret[] = $this->_escape($field) . ' ' . $this->_consume($options); + $ret[] = $this->_escape($field).' '.$this->_consume($options); continue; } @@ -815,7 +815,7 @@ protected function _render_one_field(string $field, array $options): string $name = $options['name'] ?? $field; $type = $this->getSQLFieldType($options['type'] ?? null, $options); - return $this->_escape($name) . ' ' . $type; + return $this->_escape($name).' '.$type; } /** @@ -846,7 +846,7 @@ protected function _set_args(string $what, string $alias, $value) // don't allow multiple values with same alias if (isset($this->args[$what][$alias])) { throw new Exception([ - ucfirst($what) . ' alias should be unique', + ucfirst($what).' alias should be unique', 'alias' => $alias, ]); } diff --git a/src/Migration/PgSQL.php b/src/Migration/PgSQL.php index 9c225f8..f30cd84 100644 --- a/src/Migration/PgSQL.php +++ b/src/Migration/PgSQL.php @@ -66,22 +66,22 @@ public function _render_statements(): string if (isset($this->args['dropField'])) { foreach ($this->args['dropField'] as $field => $junk) { - $result[] = 'drop column ' . $this->_escape($field); + $result[] = 'drop column '.$this->_escape($field); } } if (isset($this->args['newField'])) { foreach ($this->args['newField'] as $field => $option) { - $result[] = 'add column ' . $this->_render_one_field($field, $option); + $result[] = 'add column '.$this->_render_one_field($field, $option); } } if (isset($this->args['alterField'])) { foreach ($this->args['alterField'] as $field => $option) { $type = $this->getSQLFieldType($option['type'] ?? null, $option); - $result[] = 'alter column ' . $this->_escape($field) . - ' type ' . $type . - ' using (' . $this->_escape($field) . '::' . $type . ')'; // requires to cast value + $result[] = 'alter column '.$this->_escape($field). + ' type '.$type. + ' using ('.$this->_escape($field).'::'.$type.')'; // requires to cast value } } diff --git a/src/MigratorConsole.php b/src/MigratorConsole.php index 69b00df..13cc65c 100644 --- a/src/MigratorConsole.php +++ b/src/MigratorConsole.php @@ -34,7 +34,7 @@ public function migrateModels($models) $result = $migrator::of($model)->run(); - $console->debug(' ' . get_class($model) . '.. ' . $result); + $console->debug(' '.get_class($model).'.. '.$result); } $console->notice('Done with migration'); diff --git a/src/PhpunitTestCase.php b/src/PhpunitTestCase.php index 45fdd90..4d8f74a 100644 --- a/src/PhpunitTestCase.php +++ b/src/PhpunitTestCase.php @@ -33,7 +33,7 @@ public function setUp(): void parent::setUp(); // establish connection - $this->dsn = ($this->debug ? ('dumper:') : '') . ($GLOBALS['DB_DSN'] ?? 'sqlite::memory:'); + $this->dsn = ($this->debug ? ('dumper:') : '').($GLOBALS['DB_DSN'] ?? 'sqlite::memory:'); $user = $GLOBALS['DB_USER'] ?? null; $pass = $GLOBALS['DB_PASSWD'] ?? null; diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 7533388..23b9d95 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -53,9 +53,9 @@ public function testImportTable() 'date' => (new \DateTime())->format('Y-m-d'), 'datetime' => (new \DateTime())->format('Y-m-d H:i:s'), 'time' => (new \DateTime())->format('H:i:s'), - 'txt' => 'very long text value' . str_repeat('-=#', 1000), // 3000+ chars - 'arr' => 'very long text value' . str_repeat('-=#', 1000), // 3000+ chars - 'obj' => 'very long text value' . str_repeat('-=#', 1000), // 3000+ chars + 'txt' => 'very long text value'.str_repeat('-=#', 1000), // 3000+ chars + 'arr' => 'very long text value'.str_repeat('-=#', 1000), // 3000+ chars + 'obj' => 'very long text value'.str_repeat('-=#', 1000), // 3000+ chars ])->insert(); $migrator2 = $this->getMigrator(); From 13827c1aa31a8a5424b27fe6655ba7a087ab564b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Thu, 23 Apr 2020 15:16:27 +0200 Subject: [PATCH 02/21] Normalize Unit Testing workflow across repos --- .github/workflows/unit-tests.yml | 64 ++++++++++++++++++++++---------- composer.json | 12 +++--- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index cf22a9b..fdee0eb 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -2,15 +2,15 @@ name: Unit Testing on: pull_request: - branches: '*' + branches: '**' push: - branches: - - master - - develop + branches: '**' + schedule: + - cron: '0 */6 * * *' jobs: unit-test: - name: Unit Testing + name: Unit runs-on: ubuntu-latest container: image: atk4/image:${{ matrix.php }} # https://github.com/atk4/image @@ -18,6 +18,12 @@ jobs: fail-fast: false matrix: php: ['7.2', '7.3', 'latest'] + type: ['Phpunit'] + include: + - php: 'latest' + type: 'CodingStyle' + env: + LOG_COVERAGE: "${{ fromJSON('{true: \"1\", false: \"\"}')[matrix.php == 'latest' && matrix.type == 'Phpunit' && (github.event_name == 'pull_request' || (github.event_name == 'push' && (github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master')))] }}" services: mysql: image: mysql:5.7 @@ -26,35 +32,55 @@ jobs: DB_DATABASE: dsql_test options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 steps: - - uses: actions/checkout@v2 - - run: php --version - - name: Get Composer Cache Directory + - name: Checkout + uses: actions/checkout@v2 + + - name: Configure PHP + run: | + if [ -z "$LOG_COVERAGE" ]; then rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ; fi + php --version + + - name: Setup cache 1/2 id: composer-cache run: | echo "::set-output name=dir::$(composer config cache-files-dir)" - - uses: actions/cache@v1 + + - name: Setup cache 2/2 + uses: actions/cache@v1 with: path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('composer.json') }} + key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ matrix.type }}-${{ hashFiles('composer.json') }} restore-keys: | ${{ runner.os }}-composer- - - run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader + - name: Install PHP dependencies + run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader - - name: Run Tests + - name: Init run: | mkdir -p build/logs mysql -uroot -ppassword -h mysql -e 'CREATE DATABASE dsql_test;' - - name: SQLite Testing - run: vendor/bin/phpunit --configuration phpunit.xml --coverage-text --exclude-group dns - - name: MySQL Testing - run: vendor/bin/phpunit --configuration phpunit-mysql-workflow.xml --exclude-group dns + - name: "Run tests: SQLite (only for Phpunit)" + if: matrix.type == 'Phpunit' + run: "vendor/bin/phpunit --configuration phpunit.xml \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" + + - name: "Run tests: MySQL (only for Phpunit)" + if: matrix.type == 'Phpunit' + run: "vendor/bin/phpunit --configuration phpunit-mysql-workflow.xml \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" + + - name: Check Coding Style (only for CodingStyle) + if: matrix.type == 'CodingStyle' + continue-on-error: true # remove once CS is fully fixer + run: vendor/bin/php-cs-fixer fix --dry-run --using-cache=yes --cache-file="${{ steps.composer-cache.outputs.dir }}/.php_cs.cache" --diff --diff-format=udiff --verbose --show-progress=dots - - name: Merge coverage logs - run: vendor/bin/phpcov merge build/logs/ --clover build/logs/cc.xml; + - name: Merge coverage logs (only for "latest" Phpunit) + if: env.LOG_COVERAGE + run: vendor/bin/phpcov merge build/logs/ --clover build/logs/cc.xml - - uses: codecov/codecov-action@v1 + - name: Upload coverage logs (only for "latest" Phpunit) + if: env.LOG_COVERAGE + uses: codecov/codecov-action@v1 with: token: ${{ secrets.CODECOV_TOKEN }} file: build/logs/cc.xml diff --git a/composer.json b/composer.json index 737ea35..fe5a025 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,12 @@ "minimum-stability": "dev", "prefer-stable": true, "authors": [ - { - "name": "Romans Malinovskis", - "email": "romans@agiletoolkit.org", - "homepage": "https://nearly.guru/" - } + {"name": "Romans Malinovskis", "email": "romans@agiletoolkit.org", "homepage": "https://nearly.guru/" }, + {"name": "Michael Voříšek", "homepage": "https://mvorisek.cz/"} ], + "config": { + "sort-packages": true + }, "require": { "php": ">=7.2.0", "atk4/data": "dev-develop" @@ -33,11 +33,13 @@ "require-dev": { "atk4/ui": "dev-develop", "phpunit/phpunit": "*", + "friendsofphp/php-cs-fixer": "^2.16", "phpunit/phpcov": "*" }, "require-dev-release": { "atk4/ui": "^2.0", "phpunit/phpunit": "*", + "friendsofphp/php-cs-fixer": "^2.16", "phpunit/phpcov": "*" }, "suggest": { From 4ac0dbbe1badff7af1ee98b3f83c9b30ac0f613d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 26 Apr 2020 14:01:28 +0200 Subject: [PATCH 03/21] sort composer packages --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index fe5a025..a260f56 100644 --- a/composer.json +++ b/composer.json @@ -32,15 +32,15 @@ }, "require-dev": { "atk4/ui": "dev-develop", - "phpunit/phpunit": "*", "friendsofphp/php-cs-fixer": "^2.16", - "phpunit/phpcov": "*" + "phpunit/phpcov": "*", + "phpunit/phpunit": "*" }, "require-dev-release": { "atk4/ui": "^2.0", - "phpunit/phpunit": "*", "friendsofphp/php-cs-fixer": "^2.16", - "phpunit/phpcov": "*" + "phpunit/phpcov": "*", + "phpunit/phpunit": "*" }, "suggest": { "atk4/ui": "dev-develop", From 3e752ad983782e32fad76013db14f1d36d0c101d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 26 Apr 2020 14:30:37 +0200 Subject: [PATCH 04/21] add major authors to the composer --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a260f56..750ddbb 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,10 @@ "prefer-stable": true, "authors": [ {"name": "Romans Malinovskis", "email": "romans@agiletoolkit.org", "homepage": "https://nearly.guru/" }, - {"name": "Michael Voříšek", "homepage": "https://mvorisek.cz/"} + {"name": "Imants Horsts", "homepage": "https://darkside.lv/"}, + {"name": "Francesco Danti", "homepage": "https://oracoltech.com/"}, + {"name": "Michael Voříšek", "homepage": "https://mvorisek.cz/"}, + {"name": "Georgi Hristov"} ], "config": { "sort-packages": true From 1af62c51cea6923f6c6c54f1464fb7e8940af508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 26 Apr 2020 17:32:04 +0200 Subject: [PATCH 05/21] normalize install across repos --- .github/workflows/unit-tests.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index fdee0eb..ced8fb2 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -54,7 +54,10 @@ jobs: ${{ runner.os }}-composer- - name: Install PHP dependencies - run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader + run: | + if [ "${{ matrix.type }}" != "Phpunit" ]; then composer remove --no-interaction --no-update phpunit/phpunit phpunit/phpcov --dev ; fi + if [ "${{ matrix.type }}" != "CodingStyle" ]; then composer remove --no-interaction --no-update friendsofphp/php-cs-fixer --dev ; fi + composer install --no-suggest --ansi --prefer-dist --no-interaction --no-progress --optimize-autoloader - name: Init run: | From ecc88b2362223b248bf3f92a6b3125130e45e4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 26 Apr 2020 17:44:47 +0200 Subject: [PATCH 06/21] add lint test --- .github/workflows/unit-tests.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index ced8fb2..b3d9e46 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -72,16 +72,20 @@ jobs: if: matrix.type == 'Phpunit' run: "vendor/bin/phpunit --configuration phpunit-mysql-workflow.xml \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" + - name: Lint / check syntax (only for CodingStyle) + if: matrix.type == 'CodingStyle' + run: find . \( -type d \( -path './vendor/*' \) \) -prune -o ! -type d -name '*.php' -print0 | xargs -0 -n1 php -l + - name: Check Coding Style (only for CodingStyle) if: matrix.type == 'CodingStyle' continue-on-error: true # remove once CS is fully fixer - run: vendor/bin/php-cs-fixer fix --dry-run --using-cache=yes --cache-file="${{ steps.composer-cache.outputs.dir }}/.php_cs.cache" --diff --diff-format=udiff --verbose --show-progress=dots + run: vendor/bin/php-cs-fixer fix --dry-run --using-cache=no --diff --diff-format=udiff --verbose --show-progress=dots - - name: Merge coverage logs (only for "latest" Phpunit) + - name: Upload coverage logs 1/2 (only for "latest" Phpunit) if: env.LOG_COVERAGE run: vendor/bin/phpcov merge build/logs/ --clover build/logs/cc.xml - - name: Upload coverage logs (only for "latest" Phpunit) + - name: Upload coverage logs 2/2 (only for "latest" Phpunit) if: env.LOG_COVERAGE uses: codecov/codecov-action@v1 with: From caf45adf8c9c2246da62afef04f0799dcc4ff190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 26 Apr 2020 21:01:10 +0200 Subject: [PATCH 07/21] remove Travis --- .old.travis.yml | 51 ------------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 .old.travis.yml diff --git a/.old.travis.yml b/.old.travis.yml deleted file mode 100644 index 64b4eec..0000000 --- a/.old.travis.yml +++ /dev/null @@ -1,51 +0,0 @@ -language: php - -php: - - '7.2' - - '7.3' - -cache: - directories: - - $HOME/.composer/cache - -services: - - mysql - -before_script: - - composer self-update - - composer install --no-ansi - - mysql -e 'create database dsql_test;' - - mysql -e 'SET GLOBAL max_connections = 1000;' - - mkdir -p build/logs - -script: - - if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2" ]]; then CM=""; NC=""; else CM=""; NC="--no-coverage"; fi - - $CM ./vendor/bin/phpunit --configuration phpunit.xml $NC - - $CM ./vendor/bin/phpunit --configuration phpunit-mysql.xml $NC - -after_script: - - if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2" ]]; then - echo "Merging coverage reports:"; - vendor/bin/phpcov merge build/logs/ --clover build/logs/cc.xml; - echo "We now have these coverage files:"; - ls -l build/logs; - echo "Sending codeclimate report:"; - vendor/bin/test-reporter --coverage-report build/logs/cc.xml; - echo "Sending codecov report:"; - TRAVIS_CMD="" bash <(curl -s https://codecov.io/bash) -f build/logs/cc.xml; - fi - -notifications: - slack: - rooms: - - agiletoolkit:bjrKuPBf1h4cYiNxPBQ1kF6c#dsql - on_success: change - - urls: - - https://webhooks.gitter.im/e/b33a2db0c636f34bafa9 - - on_success: change # options: [always|never|change] default: always - on_failure: always # options: [always|never|change] default: always - on_start: never # options: [always|never|change] default: always - - email: false From 706eb95625bd0fe7a89e8172b35c241d8b697f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 26 Apr 2020 21:43:08 +0200 Subject: [PATCH 08/21] normalize DB config --- .github/workflows/unit-tests.yml | 9 +++++---- phpunit-mysql-workflow.xml | 21 --------------------- phpunit-mysql.xml | 8 ++++---- phpunit.xml | 10 ++++------ 4 files changed, 13 insertions(+), 35 deletions(-) delete mode 100644 phpunit-mysql-workflow.xml diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index b3d9e46..506e388 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -28,8 +28,10 @@ jobs: mysql: image: mysql:5.7 env: - MYSQL_ROOT_PASSWORD: password - DB_DATABASE: dsql_test + MYSQL_ROOT_PASSWORD: atk4_pass + MYSQL_USER: atk4_test + MYSQL_PASSWORD: atk4_pass + MYSQL_DATABASE: atk4_test__schema options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 steps: - name: Checkout @@ -62,7 +64,6 @@ jobs: - name: Init run: | mkdir -p build/logs - mysql -uroot -ppassword -h mysql -e 'CREATE DATABASE dsql_test;' - name: "Run tests: SQLite (only for Phpunit)" if: matrix.type == 'Phpunit' @@ -70,7 +71,7 @@ jobs: - name: "Run tests: MySQL (only for Phpunit)" if: matrix.type == 'Phpunit' - run: "vendor/bin/phpunit --configuration phpunit-mysql-workflow.xml \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" + run: "vendor/bin/phpunit --configuration phpunit-mysql.xml \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" - name: Lint / check syntax (only for CodingStyle) if: matrix.type == 'CodingStyle' diff --git a/phpunit-mysql-workflow.xml b/phpunit-mysql-workflow.xml deleted file mode 100644 index 883126f..0000000 --- a/phpunit-mysql-workflow.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - src - - - - - tests - - - - - - diff --git a/phpunit-mysql.xml b/phpunit-mysql.xml index f636365..856d208 100644 --- a/phpunit-mysql.xml +++ b/phpunit-mysql.xml @@ -1,9 +1,9 @@ - - - - + + + + diff --git a/phpunit.xml b/phpunit.xml index 613ce44..36d6578 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,11 +1,9 @@ - + + + + From 38ca55f5f9408c6639a4e8ca306672fa25e1d5ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 27 Apr 2020 11:54:00 +0200 Subject: [PATCH 09/21] norm .gitignore --- .github/workflows/unit-tests.yml | 4 ++-- .gitignore | 15 +++++++++++++-- phpunit-mysql.xml => phpunit-mysql.xml.dist | 0 phpunit.xml => phpunit.xml.dist | 6 +++--- 4 files changed, 18 insertions(+), 7 deletions(-) rename phpunit-mysql.xml => phpunit-mysql.xml.dist (100%) rename phpunit.xml => phpunit.xml.dist (79%) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 506e388..f64181e 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -67,11 +67,11 @@ jobs: - name: "Run tests: SQLite (only for Phpunit)" if: matrix.type == 'Phpunit' - run: "vendor/bin/phpunit --configuration phpunit.xml \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" + run: "vendor/bin/phpunit \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" - name: "Run tests: MySQL (only for Phpunit)" if: matrix.type == 'Phpunit' - run: "vendor/bin/phpunit --configuration phpunit-mysql.xml \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" + run: "vendor/bin/phpunit --configuration phpunit-mysql.xml.dist \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" - name: Lint / check syntax (only for CodingStyle) if: matrix.type == 'CodingStyle' diff --git a/.gitignore b/.gitignore index 44d9033..230e244 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,17 @@ -/composer.lock docs/build /build /vendor +/composer.lock +.idea +nbproject .DS_Store -/.idea/ + +local +*.local +*.local.* +cache +*.cache +*.cache.* + +/phpunit.xml +/phpunit-mysql.xml diff --git a/phpunit-mysql.xml b/phpunit-mysql.xml.dist similarity index 100% rename from phpunit-mysql.xml rename to phpunit-mysql.xml.dist diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 79% rename from phpunit.xml rename to phpunit.xml.dist index 36d6578..368b6fc 100644 --- a/phpunit.xml +++ b/phpunit.xml.dist @@ -1,9 +1,9 @@ - - - + + + From f33d66ede461d45d9ffc2b771b8fa3fbdacfa2ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 27 Apr 2020 17:34:17 +0200 Subject: [PATCH 10/21] Add CS fixer rules --- .github/workflows/unit-tests.yml | 1 - .php_cs.dist | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 .php_cs.dist diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index f64181e..8250d7a 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -79,7 +79,6 @@ jobs: - name: Check Coding Style (only for CodingStyle) if: matrix.type == 'CodingStyle' - continue-on-error: true # remove once CS is fully fixer run: vendor/bin/php-cs-fixer fix --dry-run --using-cache=no --diff --diff-format=udiff --verbose --show-progress=dots - name: Upload coverage logs 1/2 (only for "latest" Phpunit) diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..66f72c5 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,57 @@ +in([__DIR__]) + ->exclude([ + 'cache', + 'build', + 'vendor', + ]); + +return PhpCsFixer\Config::create() + ->setRiskyAllowed(true) + ->setRules([ + '@PhpCsFixer' => true, + '@PhpCsFixer:risky' =>true, + '@PHP71Migration:risky' => true, + + // required by PSR-12 + 'concat_space' => [ + 'spacing' => 'one', + ], + + // disable some too strict rules + 'strict_comparison' => false, + 'strict_param' => false, + 'phpdoc_types_order' => [ + 'null_adjustment' => 'always_last', + 'sort_algorithm' => 'none', + ], + 'yoda_style' => [ + 'equal' => false, + 'identical' => false, + ], + 'native_function_invocation' => false, + 'non_printable_character' => [ + 'use_escape_sequences_in_strings' => true, + ], + 'declare_strict_types' => false, + 'void_return' => false, + 'combine_consecutive_issets' => false, + 'combine_consecutive_unsets' => false, + 'multiline_whitespace_before_semicolons' => false, + 'no_superfluous_elseif' => false, + 'ordered_class_elements' => false, + 'php_unit_internal_class' => false, + 'php_unit_test_class_requires_covers' => false, + 'phpdoc_add_missing_param_annotation' => false, + 'return_assignment' => false, + 'comment_to_phpdoc' => false, + + // may be fixed/removed later + 'php_unit_strict' => false, + 'php_unit_test_case_static_method_calls' => false, + + ]) + ->setFinder($finder) + ->setCacheFile(__DIR__ . '/.php_cs.cache'); From b08210264bf18d9208d5c281fef1af14282a657b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 28 Apr 2020 01:07:40 +0200 Subject: [PATCH 11/21] apply safe/mass CS fixes --- .php_cs.dist | 14 ++++- demos/modelmigrator.php | 2 +- src/Migration.php | 126 +++++++++++++++------------------------ src/Migration/MySQL.php | 16 ++--- src/Migration/Oracle.php | 6 +- src/Migration/PgSQL.php | 32 ++++------ src/Migration/SQLite.php | 6 -- src/MigratorConsole.php | 4 +- src/PhpunitTestCase.php | 8 ++- tests/ModelTest.php | 26 ++++---- 10 files changed, 104 insertions(+), 136 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index 66f72c5..f574b2a 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -12,8 +12,14 @@ return PhpCsFixer\Config::create() ->setRiskyAllowed(true) ->setRules([ '@PhpCsFixer' => true, - '@PhpCsFixer:risky' =>true, - '@PHP71Migration:risky' => true, + // '@PhpCsFixer:risky' =>true, + // '@PHP71Migration:risky' => true, + + // to be checked manually, @TODO + 'php_unit_fqcn_annotation' => false, + 'phpdoc_annotation_without_dot' => false, + 'no_useless_else' => false, // check/add spacing after manually + // required by PSR-12 'concat_space' => [ @@ -27,6 +33,7 @@ return PhpCsFixer\Config::create() 'null_adjustment' => 'always_last', 'sort_algorithm' => 'none', ], + 'single_line_throw' => false, 'yoda_style' => [ 'equal' => false, 'identical' => false, @@ -47,6 +54,9 @@ return PhpCsFixer\Config::create() 'phpdoc_add_missing_param_annotation' => false, 'return_assignment' => false, 'comment_to_phpdoc' => false, + 'nullable_type_declaration_for_default_null_value' => [ + 'use_nullable_type_declaration' => false, + ], // may be fixed/removed later 'php_unit_strict' => false, diff --git a/demos/modelmigrator.php b/demos/modelmigrator.php index b57a734..b88c415 100644 --- a/demos/modelmigrator.php +++ b/demos/modelmigrator.php @@ -21,7 +21,7 @@ public function init(): void // ok, now we surely have DB! $user->save([ - 'name' => 'John'.rand(1, 100), + 'name' => 'John' . rand(1, 100), ]); } catch (\atk4\core\Exception $e) { echo $e->getColorfulText(); diff --git a/src/Migration.php b/src/Migration.php index 4c3d3bf..144640e 100644 --- a/src/Migration.php +++ b/src/Migration.php @@ -23,8 +23,8 @@ class Migration extends Expression /** @var array Expression templates */ protected $templates = [ 'create' => 'create table {table} ([field])', - 'drop' => 'drop table if exists {table}', - 'alter' => 'alter table {table} [statements]', + 'drop' => 'drop table if exists {table}', + 'alter' => 'alter table {table} [statements]', 'rename' => 'rename table {old_table} to {table}', ]; @@ -45,16 +45,16 @@ class Migration extends Expression /** @var array Conversion mapping from Agile Data types to persistence types */ protected $defaultMapToPersistence = [ ['varchar', 255], // default - 'boolean' => ['tinyint', 1], - 'integer' => ['bigint'], - 'money' => ['decimal', 12, 2], - 'float' => ['decimal', 16, 6], - 'date' => ['date'], - 'datetime' => ['datetime'], - 'time' => ['varchar', 8], - 'text' => ['text'], - 'array' => ['text'], - 'object' => ['text'], + 'boolean' => ['tinyint', 1], + 'integer' => ['bigint'], + 'money' => ['decimal', 12, 2], + 'float' => ['decimal', 16, 6], + 'date' => ['date'], + 'datetime' => ['datetime'], + 'time' => ['varchar', 8], + 'text' => ['text'], + 'array' => ['text'], + 'object' => ['text'], ]; /** @var array use this array in extended classes to overwrite or extend values of default mapping */ @@ -63,16 +63,16 @@ class Migration extends Expression /** @var array Conversion mapping from persistence types to Agile Data types */ protected $defaultMapToAgile = [ [null], // default - 'tinyint' => ['boolean'], - 'int' => ['integer'], - 'integer' => ['integer'], - 'bigint' => ['integer'], - 'decimal' => ['float'], - 'numeric' => ['float'], - 'date' => ['date'], - 'datetime' => ['datetime'], + 'tinyint' => ['boolean'], + 'int' => ['integer'], + 'integer' => ['integer'], + 'bigint' => ['integer'], + 'decimal' => ['float'], + 'numeric' => ['float'], + 'date' => ['date'], + 'datetime' => ['datetime'], 'timestamp' => ['datetime'], - 'text' => ['text'], + 'text' => ['text'], ]; /** @var array use this array in extended classes to overwrite or extend values of default mapping */ @@ -91,9 +91,9 @@ class Migration extends Expression * */ private static $registry = [ 'sqlite' => Migration\SQLite::class, - 'mysql' => Migration\MySQL::class, - 'pgsql' => Migration\PgSQL::class, - 'oci' => Migration\Oracle::class, + 'mysql' => Migration\MySQL::class, + 'pgsql' => Migration\PgSQL::class, + 'oci' => Migration\Oracle::class, ]; /** @@ -126,7 +126,7 @@ public static function of($source, $params = []): self throw new Exception([ 'Not sure which migration class to use for your DSN', 'driverType' => $connection->driverType, - 'source' => $source, + 'source' => $source, ]); } @@ -143,7 +143,6 @@ public static function of($source, $params = []): self * * CustomMigration\MySQL must be descendant of Migration class. * - * @param string $driverType * @param string $migrator */ public static function register(string $driverType, string $migrator = null) @@ -178,8 +177,6 @@ public static function register(string $driverType, string $migrator = null) * @param Connection|Persistence|Model $source * * @throws Exception - * - * @return Connection */ public static function getConnection($source): Connection { @@ -240,12 +237,8 @@ public function setSource($source) /** * Sets model. * - * @param Model $m - * * @throws Exception * @throws \ReflectionException - * - * @return Model */ public function setModel(Model $m): Model { @@ -271,8 +264,8 @@ public function setModel(Model $m): Model } $options = [ - 'type' => $ref_type !== self::REF_TYPE_NONE && empty($persist_field->type) ? 'integer' : $persist_field->type, - 'ref_type' => $ref_type, + 'type' => $ref_type !== self::REF_TYPE_NONE && empty($persist_field->type) ? 'integer' : $persist_field->type, + 'ref_type' => $ref_type, 'mandatory' => ($field->mandatory || $field->required) && ($persist_field->mandatory || $persist_field->required), // todo add more options here ]; @@ -449,8 +442,8 @@ public function run(): string $newSQLFieldType = $this->getSQLFieldType($options['type']); if ($oldSQLFieldType !== $newSQLFieldType) { $this->alterField($field, $options); - $altered++; - $changes++; + ++$altered; + ++$changes; } } @@ -458,8 +451,8 @@ public function run(): string } else { // new field, so let's just add it $this->newField($field, $options); - $added++; - $changes++; + ++$added; + ++$changes; } } @@ -471,16 +464,16 @@ public function run(): string } $this->dropField($field); - $dropped++; - $changes++; + ++$dropped; + ++$changes; } if ($changes) { $this->alter(); - return 'added '.$added.' field'.($added == 1 ? '' : 's').', '. - 'changed '.$altered.' field'.($altered == 1 ? '' : 's').' and '. - 'deleted '.$dropped.' field'.($dropped == 1 ? '' : 's'); + return 'added ' . $added . ' field' . ($added == 1 ? '' : 's') . ', ' . + 'changed ' . $altered . ' field' . ($altered == 1 ? '' : 's') . ' and ' . + 'deleted ' . $dropped . ' field' . ($dropped == 1 ? '' : 's'); } return 'no changes'; @@ -488,8 +481,6 @@ public function run(): string /** * Renders statement. - * - * @return string */ public function _render_statements(): string { @@ -497,19 +488,19 @@ public function _render_statements(): string if (isset($this->args['dropField'])) { foreach ($this->args['dropField'] as $field => $junk) { - $result[] = 'drop column '.$this->_escape($field); + $result[] = 'drop column ' . $this->_escape($field); } } if (isset($this->args['newField'])) { foreach ($this->args['newField'] as $field => $option) { - $result[] = 'add column '.$this->_render_one_field($field, $option); + $result[] = 'add column ' . $this->_render_one_field($field, $option); } } if (isset($this->args['alterField'])) { foreach ($this->args['alterField'] as $field => $option) { - $result[] = 'change column '.$this->_escape($field).' '.$this->_render_one_field($field, $option); + $result[] = 'change column ' . $this->_escape($field) . ' ' . $this->_render_one_field($field, $option); } } @@ -525,8 +516,6 @@ public function _render_statements(): string * * @throws Exception * @throws \atk4\data\Exception - * - * @return Model */ public function createModel($persistence, $table = null): Model { @@ -576,8 +565,7 @@ public function newField($field, $options = []): self /** * Sets alterField argument. * - * @param string $field - * @param array $options + * @param array $options * * @throws Exception * @@ -611,10 +599,6 @@ public function dropField($field): self * DB engine specific. * * @todo Maybe convert to abstract function - * - * @param string $table - * - * @return array */ public function describeTable(string $table): array { @@ -625,8 +609,6 @@ public function describeTable(string $table): array * Convert SQL field types to Agile Data field types. * * @param string $type SQL field type - * - * @return string|null */ public function getModelFieldType(string $type): ?string { @@ -644,8 +626,6 @@ public function getModelFieldType(string $type): ?string * * @param string $type Agile Data field type * @param array $options More options - * - * @return string|null */ public function getSQLFieldType(?string $type, array $options = []): ?string { @@ -656,7 +636,7 @@ public function getSQLFieldType(?string $type, array $options = []): ?string $res = $a[0]; if (count($a) > 1) { - $res .= ' ('.implode(',', array_slice($a, 1)).')'; + $res .= ' (' . implode(',', array_slice($a, 1)) . ')'; } if (!empty($options['ref_type']) && $options['ref_type'] !== self::REF_TYPE_NONE && $type === 'integer') { @@ -671,7 +651,7 @@ public function getSQLFieldType(?string $type, array $options = []): ?string } if (!empty($options['ref_type']) && $options['ref_type'] === self::REF_TYPE_PRIMARY) { - $res .= ' '.$this->primary_key_expr; + $res .= ' ' . $this->primary_key_expr; } return $res; @@ -680,11 +660,7 @@ public function getSQLFieldType(?string $type, array $options = []): ?string /** * Import fields from database into migration field config. * - * @param string $table - * * @throws Exception - * - * @return bool */ public function importTable(string $table): bool { @@ -697,7 +673,7 @@ public function importTable(string $table): bool $ref_type = $row['pk'] ? self::REF_TYPE_PRIMARY : self::REF_TYPE_NONE; $options = [ - 'type' => $type, + 'type' => $type, 'ref_type' => $ref_type, ]; @@ -763,7 +739,7 @@ public function field($name, $options = []) public function id($name = null) { $options = [ - 'type' => 'integer', + 'type' => 'integer', 'ref_type' => self::REF_TYPE_PRIMARY, ]; @@ -792,7 +768,8 @@ public function _render_field() foreach ($this->args['field'] as $field => $options) { if ($options instanceof Expression) { - $ret[] = $this->_escape($field).' '.$this->_consume($options); + $ret[] = $this->_escape($field) . ' ' . $this->_consume($options); + continue; } @@ -804,24 +781,17 @@ public function _render_field() /** * Renders one field. - * - * @param string $field - * @param array $options - * - * @return string */ protected function _render_one_field(string $field, array $options): string { $name = $options['name'] ?? $field; $type = $this->getSQLFieldType($options['type'] ?? null, $options); - return $this->_escape($name).' '.$type; + return $this->_escape($name) . ' ' . $type; } /** * Return fields. - * - * @return array */ public function _getFields(): array { @@ -846,7 +816,7 @@ protected function _set_args(string $what, string $alias, $value) // don't allow multiple values with same alias if (isset($this->args[$what][$alias])) { throw new Exception([ - ucfirst($what).' alias should be unique', + ucfirst($what) . ' alias should be unique', 'alias' => $alias, ]); } diff --git a/src/Migration/MySQL.php b/src/Migration/MySQL.php index a91c363..f7ee188 100644 --- a/src/Migration/MySQL.php +++ b/src/Migration/MySQL.php @@ -17,25 +17,21 @@ class MySQL extends \atk4\schema\Migration /** @var array use this array in extended classes to overwrite or extend values of default mapping */ public $mapToPersistence = [ - 'text' => ['longtext'], - 'array' => ['longtext'], - 'object' => ['longtext'], + 'text' => ['longtext'], + 'array' => ['longtext'], + 'object' => ['longtext'], ]; /** @var array use this array in extended classes to overwrite or extend values of default mapping */ public $mapToAgile = [ - 0 => ['string'], - 'longtext' => ['text'], - 'longblob' => ['text'], + 0 => ['string'], + 'longtext' => ['text'], + 'longblob' => ['text'], ]; /** * Return database table descriptions. * DB engine specific. - * - * @param string $table - * - * @return array */ public function describeTable(string $table): array { diff --git a/src/Migration/Oracle.php b/src/Migration/Oracle.php index 0de8ec6..73c6d74 100644 --- a/src/Migration/Oracle.php +++ b/src/Migration/Oracle.php @@ -9,12 +9,12 @@ class Oracle extends Migration { /** @var array use this array in extended classes to overwrite or extend values of default mapping */ public $mapToPersistence = [ - 'date' => ['date'], - 'datetime' => ['date'], // in Oracle DATE data type is actually datetime + 'date' => ['date'], + 'datetime' => ['date'], // in Oracle DATE data type is actually datetime ]; /** @var array use this array in extended classes to overwrite or extend values of default mapping */ public $mapToAgile = [ - 'date' => ['datetime'], + 'date' => ['datetime'], ]; } diff --git a/src/Migration/PgSQL.php b/src/Migration/PgSQL.php index f30cd84..7685d67 100644 --- a/src/Migration/PgSQL.php +++ b/src/Migration/PgSQL.php @@ -10,28 +10,24 @@ class PgSQL extends \atk4\schema\Migration /** @var array use this array in extended classes to overwrite or extend values of default mapping */ public $mapToPersistence = [ - 'boolean' => ['boolean'], - 'date' => ['date'], - 'datetime' => ['timestamp'], // without timezone - 'time' => ['time'], // without timezone + 'boolean' => ['boolean'], + 'date' => ['date'], + 'datetime' => ['timestamp'], // without timezone + 'time' => ['time'], // without timezone ]; /** @var array use this array in extended classes to overwrite or extend values of default mapping */ public $mapToAgile = [ - 'boolean' => ['boolean'], - 'date' => ['date'], - 'datetime' => ['datetime'], + 'boolean' => ['boolean'], + 'date' => ['date'], + 'datetime' => ['datetime'], 'timestamp' => ['datetime'], - 'time' => ['time'], + 'time' => ['time'], ]; /** * Return database table descriptions. * DB engine specific. - * - * @param string $table - * - * @return array */ public function describeTable(string $table): array { @@ -57,8 +53,6 @@ public function describeTable(string $table): array /** * Renders statement. - * - * @return string */ public function _render_statements(): string { @@ -66,22 +60,22 @@ public function _render_statements(): string if (isset($this->args['dropField'])) { foreach ($this->args['dropField'] as $field => $junk) { - $result[] = 'drop column '.$this->_escape($field); + $result[] = 'drop column ' . $this->_escape($field); } } if (isset($this->args['newField'])) { foreach ($this->args['newField'] as $field => $option) { - $result[] = 'add column '.$this->_render_one_field($field, $option); + $result[] = 'add column ' . $this->_render_one_field($field, $option); } } if (isset($this->args['alterField'])) { foreach ($this->args['alterField'] as $field => $option) { $type = $this->getSQLFieldType($option['type'] ?? null, $option); - $result[] = 'alter column '.$this->_escape($field). - ' type '.$type. - ' using ('.$this->_escape($field).'::'.$type.')'; // requires to cast value + $result[] = 'alter column ' . $this->_escape($field) . + ' type ' . $type . + ' using (' . $this->_escape($field) . '::' . $type . ')'; // requires to cast value } } diff --git a/src/Migration/SQLite.php b/src/Migration/SQLite.php index 4ada0ed..b8bcc56 100644 --- a/src/Migration/SQLite.php +++ b/src/Migration/SQLite.php @@ -14,10 +14,6 @@ class SQLite extends \atk4\schema\Migration /** * Return database table descriptions. * DB engine specific. - * - * @param string $table - * - * @return array */ public function describeTable(string $table): array { @@ -29,8 +25,6 @@ public function describeTable(string $table): array * * @param string $type Agile Data field type * @param array $options More options - * - * @return string|null */ public function getSQLFieldType(?string $type, array $options = []): ?string { diff --git a/src/MigratorConsole.php b/src/MigratorConsole.php index 13cc65c..cadecc2 100644 --- a/src/MigratorConsole.php +++ b/src/MigratorConsole.php @@ -9,7 +9,7 @@ class MigratorConsole extends \atk4\ui\Console { /** @var string Name of migrator class to use */ - public $migrator_class = null; + public $migrator_class; /** * Provided with array of models, perform migration for each of them. @@ -34,7 +34,7 @@ public function migrateModels($models) $result = $migrator::of($model)->run(); - $console->debug(' '.get_class($model).'.. '.$result); + $console->debug(' ' . get_class($model) . '.. ' . $result); } $console->notice('Done with migration'); diff --git a/src/PhpunitTestCase.php b/src/PhpunitTestCase.php index 4d8f74a..518efbd 100644 --- a/src/PhpunitTestCase.php +++ b/src/PhpunitTestCase.php @@ -14,7 +14,7 @@ class PhpunitTestCase extends AtkPhpunit\TestCase public $db; /** @var array Array of database table names */ - public $tables = null; + public $tables; /** @var bool Debug mode enabled/disabled. In debug mode will use Dumper persistence */ public $debug = false; @@ -33,7 +33,7 @@ public function setUp(): void parent::setUp(); // establish connection - $this->dsn = ($this->debug ? ('dumper:') : '').($GLOBALS['DB_DSN'] ?? 'sqlite::memory:'); + $this->dsn = ($this->debug ? ('dumper:') : '') . ($GLOBALS['DB_DSN'] ?? 'sqlite::memory:'); $user = $GLOBALS['DB_USER'] ?? null; $pass = $GLOBALS['DB_PASSWD'] ?? null; @@ -94,17 +94,21 @@ public function setDB($db_data, $import_data = true) foreach ($first_row as $field => $row) { if ($field === 'id') { $migrator->id('id'); + continue; } if (is_int($row)) { $migrator->field($field, ['type' => 'integer']); + continue; } elseif (is_float($row)) { $migrator->field($field, ['type' => 'numeric(10,5)']); + continue; } elseif ($row instanceof \DateTime) { $migrator->field($field, ['type' => 'datetime']); + continue; } diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 23b9d95..4528f46 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -43,19 +43,19 @@ public function testImportTable() $this->db->dsql()->table('user') ->set([ - 'id' => 1, - 'foo' => 'quite short value, max 255 characters', - 'str' => 'quite short value, max 255 characters', - 'bool' => true, - 'int' => 123, - 'mon' => 123.45, - 'flt' => 123.456789, - 'date' => (new \DateTime())->format('Y-m-d'), + 'id' => 1, + 'foo' => 'quite short value, max 255 characters', + 'str' => 'quite short value, max 255 characters', + 'bool' => true, + 'int' => 123, + 'mon' => 123.45, + 'flt' => 123.456789, + 'date' => (new \DateTime())->format('Y-m-d'), 'datetime' => (new \DateTime())->format('Y-m-d H:i:s'), - 'time' => (new \DateTime())->format('H:i:s'), - 'txt' => 'very long text value'.str_repeat('-=#', 1000), // 3000+ chars - 'arr' => 'very long text value'.str_repeat('-=#', 1000), // 3000+ chars - 'obj' => 'very long text value'.str_repeat('-=#', 1000), // 3000+ chars + 'time' => (new \DateTime())->format('H:i:s'), + 'txt' => 'very long text value' . str_repeat('-=#', 1000), // 3000+ chars + 'arr' => 'very long text value' . str_repeat('-=#', 1000), // 3000+ chars + 'obj' => 'very long text value' . str_repeat('-=#', 1000), // 3000+ chars ])->insert(); $migrator2 = $this->getMigrator(); @@ -87,7 +87,7 @@ public function testMigrateTable() ->create(); $this->db->dsql()->table('user') ->set([ - 'id' => 1, + 'id' => 1, 'foo' => 'foovalue', 'bar' => 123, 'baz' => 'long text value', From b44c68805576ee505cf7ea6009b23d15932943dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 28 Apr 2020 01:17:38 +0200 Subject: [PATCH 12/21] apply risky/mass CS fixes --- .php_cs.dist | 4 ++-- demos/modelmigrator.php | 2 +- src/PhpunitTestCase.php | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index f574b2a..6d5e83d 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -12,8 +12,8 @@ return PhpCsFixer\Config::create() ->setRiskyAllowed(true) ->setRules([ '@PhpCsFixer' => true, - // '@PhpCsFixer:risky' =>true, - // '@PHP71Migration:risky' => true, + '@PhpCsFixer:risky' =>true, + '@PHP71Migration:risky' => true, // to be checked manually, @TODO 'php_unit_fqcn_annotation' => false, diff --git a/demos/modelmigrator.php b/demos/modelmigrator.php index b88c415..01af426 100644 --- a/demos/modelmigrator.php +++ b/demos/modelmigrator.php @@ -21,7 +21,7 @@ public function init(): void // ok, now we surely have DB! $user->save([ - 'name' => 'John' . rand(1, 100), + 'name' => 'John' . random_int(1, 100), ]); } catch (\atk4\core\Exception $e) { echo $e->getColorfulText(); diff --git a/src/PhpunitTestCase.php b/src/PhpunitTestCase.php index 518efbd..e196fc7 100644 --- a/src/PhpunitTestCase.php +++ b/src/PhpunitTestCase.php @@ -28,7 +28,7 @@ class PhpunitTestCase extends AtkPhpunit\TestCase /** * Setup test database. */ - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -41,9 +41,9 @@ public function setUp(): void $this->driverType = $this->db->connection->driverType; } - public function tearDown(): void + protected function tearDown(): void { - unset($this->db); + $this->db = null; parent::tearDown(); // TODO: Change the autogenerated stub } From a641916862a90a37767fe875859d33c854c045ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 28 Apr 2020 01:26:06 +0200 Subject: [PATCH 13/21] manual/checked CS fixes --- .php_cs.dist | 6 ------ src/Migration.php | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index 6d5e83d..79f5f1f 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -15,12 +15,6 @@ return PhpCsFixer\Config::create() '@PhpCsFixer:risky' =>true, '@PHP71Migration:risky' => true, - // to be checked manually, @TODO - 'php_unit_fqcn_annotation' => false, - 'phpdoc_annotation_without_dot' => false, - 'no_useless_else' => false, // check/add spacing after manually - - // required by PSR-12 'concat_space' => [ 'spacing' => 'one', diff --git a/src/Migration.php b/src/Migration.php index 144640e..4ee121f 100644 --- a/src/Migration.php +++ b/src/Migration.php @@ -307,9 +307,9 @@ protected function getReferenceField(Field $field): ?Field $dummyPersistence = new Persistence\SQL($this->connection); return (new $reference_model_class($dummyPersistence))->getField($reference_field); - } else { - return null; } + + return null; } /** From 55154a12b94ee590509276a1e8b7bbdbbb341159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 28 Apr 2020 02:12:29 +0200 Subject: [PATCH 14/21] verbose phpunit --- .github/workflows/unit-tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 8250d7a..ca5471a 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -67,11 +67,11 @@ jobs: - name: "Run tests: SQLite (only for Phpunit)" if: matrix.type == 'Phpunit' - run: "vendor/bin/phpunit \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" + run: "vendor/bin/phpunit \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\" -v" - name: "Run tests: MySQL (only for Phpunit)" if: matrix.type == 'Phpunit' - run: "vendor/bin/phpunit --configuration phpunit-mysql.xml.dist \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\"" + run: "vendor/bin/phpunit --configuration phpunit-mysql.xml.dist \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\" -v" - name: Lint / check syntax (only for CodingStyle) if: matrix.type == 'CodingStyle' From 3526c215d071bbe98b93e83a1e465cf3d98481a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 28 Apr 2020 11:14:54 +0200 Subject: [PATCH 15/21] Add homepage for Georgi --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 750ddbb..abca88e 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ {"name": "Imants Horsts", "homepage": "https://darkside.lv/"}, {"name": "Francesco Danti", "homepage": "https://oracoltech.com/"}, {"name": "Michael Voříšek", "homepage": "https://mvorisek.cz/"}, - {"name": "Georgi Hristov"} + {"name": "Georgi Hristov", "homepage": "https://xsystems.io/"} ], "config": { "sort-packages": true From 585bfc7a03e40295ef49a6adc62295de8eb81b54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 28 Apr 2020 12:22:17 +0200 Subject: [PATCH 16/21] more strict CS --- .php_cs.dist | 10 +++------- src/Migration.php | 16 ++++++++-------- src/Migration/MySQL.php | 2 +- src/Migration/PgSQL.php | 2 +- tests/BasicTest.php | 8 ++++---- tests/ModelTest.php | 6 +++--- tests/PhpunitTestCaseTest.php | 4 ++-- 7 files changed, 22 insertions(+), 26 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index 79f5f1f..3bdb35d 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -21,8 +21,6 @@ return PhpCsFixer\Config::create() ], // disable some too strict rules - 'strict_comparison' => false, - 'strict_param' => false, 'phpdoc_types_order' => [ 'null_adjustment' => 'always_last', 'sort_algorithm' => 'none', @@ -44,6 +42,9 @@ return PhpCsFixer\Config::create() 'no_superfluous_elseif' => false, 'ordered_class_elements' => false, 'php_unit_internal_class' => false, + 'php_unit_test_case_static_method_calls' => [ + 'call_type' => 'this', + ], 'php_unit_test_class_requires_covers' => false, 'phpdoc_add_missing_param_annotation' => false, 'return_assignment' => false, @@ -51,11 +52,6 @@ return PhpCsFixer\Config::create() 'nullable_type_declaration_for_default_null_value' => [ 'use_nullable_type_declaration' => false, ], - - // may be fixed/removed later - 'php_unit_strict' => false, - 'php_unit_test_case_static_method_calls' => false, - ]) ->setFinder($finder) ->setCacheFile(__DIR__ . '/.php_cs.cache'); diff --git a/src/Migration.php b/src/Migration.php index 4ee121f..d69d3af 100644 --- a/src/Migration.php +++ b/src/Migration.php @@ -122,7 +122,7 @@ public static function of($source, $params = []): self // if used within a subclass Migration method will create migrator of that class // if $migrator class is the generic class Migration then migrator was not resolved correctly - if ($migrator == __CLASS__) { + if ($migrator === __CLASS__) { throw new Exception([ 'Not sure which migration class to use for your DSN', 'driverType' => $connection->driverType, @@ -254,7 +254,7 @@ public function setModel(Model $m): Model continue; } - if ($field->short_name == $m->id_field) { + if ($field->short_name === $m->id_field) { $ref_type = self::REF_TYPE_PRIMARY; $persist_field = $field; } else { @@ -430,7 +430,7 @@ public function run(): string // add new fields or update existing ones foreach ($new as $field => $options) { // never update ID field (sadly hard-coded field name) - if ($field == 'id') { + if ($field === 'id') { continue; } @@ -459,7 +459,7 @@ public function run(): string // remaining old fields - drop them foreach ($old as $field => $options) { // never delete ID field (sadly hard-coded field name) - if ($field == 'id') { + if ($field === 'id') { continue; } @@ -471,9 +471,9 @@ public function run(): string if ($changes) { $this->alter(); - return 'added ' . $added . ' field' . ($added == 1 ? '' : 's') . ', ' . - 'changed ' . $altered . ' field' . ($altered == 1 ? '' : 's') . ' and ' . - 'deleted ' . $dropped . ' field' . ($dropped == 1 ? '' : 's'); + return 'added ' . $added . ' field' . ($added === 1 ? '' : 's') . ', ' . + 'changed ' . $altered . ' field' . ($altered === 1 ? '' : 's') . ' and ' . + 'deleted ' . $dropped . ' field' . ($dropped === 1 ? '' : 's'); } return 'no changes'; @@ -526,7 +526,7 @@ public function createModel($persistence, $table = null): Model $this->importTable($this['table']); foreach ($this->_getFields() as $field => $options) { - if ($field == 'id') { + if ($field === 'id') { continue; } diff --git a/src/Migration/MySQL.php b/src/Migration/MySQL.php index f7ee188..02b24fd 100644 --- a/src/Migration/MySQL.php +++ b/src/Migration/MySQL.php @@ -44,7 +44,7 @@ public function describeTable(string $table): array foreach ($this->connection->expr('describe {}', [$table]) as $row) { $row2 = []; $row2['name'] = $row['Field']; - $row2['pk'] = $row['Key'] == 'PRI'; + $row2['pk'] = $row['Key'] === 'PRI'; $row2['type'] = preg_replace('/\(.*/', '', $row['Type']); $result[] = $row2; diff --git a/src/Migration/PgSQL.php b/src/Migration/PgSQL.php index 7685d67..35e7b4c 100644 --- a/src/Migration/PgSQL.php +++ b/src/Migration/PgSQL.php @@ -42,7 +42,7 @@ public function describeTable(string $table): array foreach ($columns as $row) { $row2 = []; $row2['name'] = $row['column_name']; - $row2['pk'] = $row['is_identity'] == 'YES'; + $row2['pk'] = $row['is_identity'] === 'YES'; $row2['type'] = preg_replace('/\(.*/', '', $row['udt_name']); // $row['data_type'], but it's PgSQL specific type $result[] = $row2; diff --git a/tests/BasicTest.php b/tests/BasicTest.php index fad0042..f964244 100644 --- a/tests/BasicTest.php +++ b/tests/BasicTest.php @@ -51,7 +51,7 @@ public function testCreateAndAlter() */ public function testCreateAndDrop() { - if ($this->driverType == 'sqlite') { + if ($this->driverType === 'sqlite') { $this->markTestSkipped('SQLite does not support DROP'); } @@ -87,7 +87,7 @@ public function testDirectMigratorResolving() $directMigrator = $migratorClass::of($this->db); - $this->assertEquals($migratorClass, get_class($directMigrator)); + $this->assertSame($migratorClass, get_class($directMigrator)); } /** @@ -100,11 +100,11 @@ public function testMigratorRegistering() Migration::register($this->driverType, CustomMySQLMigrator::class); - $this->assertEquals(CustomMySQLMigrator::class, get_class($this->getMigrator())); + $this->assertSame(CustomMySQLMigrator::class, get_class($this->getMigrator())); CustomMySQLMigrator::register($this->driverType); - $this->assertEquals(CustomMySQLMigrator::class, get_class($this->getMigrator())); + $this->assertSame(CustomMySQLMigrator::class, get_class($this->getMigrator())); // restore original migrator registration Migration::register($this->driverType, $origMigratorClass); diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 4528f46..8102d64 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -65,7 +65,7 @@ public function testImportTable() $q1 = preg_replace('/\([0-9,]*\)/i', '', $migrator->getDebugQuery()); // remove parenthesis otherwise we can't differ money from float etc. $q2 = preg_replace('/\([0-9,]*\)/i', '', $migrator2->getDebugQuery()); - $this->assertEquals($q1, $q2); + $this->assertSame($q1, $q2); } /** @@ -73,7 +73,7 @@ public function testImportTable() */ public function testMigrateTable() { - if ($this->driverType == 'sqlite') { + if ($this->driverType === 'sqlite') { // http://www.sqlitetutorial.net/sqlite-alter-table/ $this->markTestSkipped('SQLite does not support DROP COLUMN in ALTER TABLE'); } @@ -109,7 +109,7 @@ public function testCreateModel() $user_model = $this->getMigrator($this->db)->createModel($this->db, 'user'); - $this->assertEquals( + $this->assertSame( [ 'name', 'password', diff --git a/tests/PhpunitTestCaseTest.php b/tests/PhpunitTestCaseTest.php index 54ae4aa..a36ef22 100644 --- a/tests/PhpunitTestCaseTest.php +++ b/tests/PhpunitTestCaseTest.php @@ -20,8 +20,8 @@ public function testInit() $this->setDB($q2); $q3 = $this->getDB('user'); - $this->assertEquals($q2, $q3); + $this->assertSame($q2, $q3); - $this->assertEquals($q, $this->getDB('user', true)); + $this->assertSame($q, $this->getDB('user', true)); } } From ead48778573f6c7ea7620fd4af475d662df17e54 Mon Sep 17 00:00:00 2001 From: DarkSide Date: Tue, 28 Apr 2020 14:48:54 +0300 Subject: [PATCH 17/21] codecov config test --- codecov.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 codecov.yml diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..8a359d4 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,20 @@ +ignore: + - demos + - docs + - tests + - tools +codecov: + notify: + require_ci_to_pass: yes + strict_yaml_branch: master # only use the latest copy on master branch +coverage: + precision: 2 + round: down + range: "95...100" + status: + project: + default: + target: 100 + target: 95 + patch: yes + changes: no From 99378f1cb5ee7176d0e751d70337ebee0c286e41 Mon Sep 17 00:00:00 2001 From: DarkSide Date: Tue, 28 Apr 2020 14:57:21 +0300 Subject: [PATCH 18/21] test --- codecov.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codecov.yml b/codecov.yml index 8a359d4..268cda4 100644 --- a/codecov.yml +++ b/codecov.yml @@ -10,11 +10,11 @@ codecov: coverage: precision: 2 round: down - range: "95...100" + range: "40...100" status: project: default: target: 100 - target: 95 + target: 40 patch: yes changes: no From e73c20a5008535588ebadc7dfe829005f36696b6 Mon Sep 17 00:00:00 2001 From: DarkSide Date: Tue, 28 Apr 2020 15:02:17 +0300 Subject: [PATCH 19/21] not so strict --- codecov.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/codecov.yml b/codecov.yml index 268cda4..df9561b 100644 --- a/codecov.yml +++ b/codecov.yml @@ -3,14 +3,10 @@ ignore: - docs - tests - tools -codecov: - notify: - require_ci_to_pass: yes - strict_yaml_branch: master # only use the latest copy on master branch coverage: precision: 2 round: down - range: "40...100" + range: "40...100" # lower coverage expectations status: project: default: From 002490f64350f7e6d544dacb0453832d20a5066c Mon Sep 17 00:00:00 2001 From: DarkSide Date: Tue, 28 Apr 2020 15:09:32 +0300 Subject: [PATCH 20/21] maybe fix threshold ? --- codecov.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/codecov.yml b/codecov.yml index df9561b..78a74a4 100644 --- a/codecov.yml +++ b/codecov.yml @@ -10,6 +10,7 @@ coverage: status: project: default: + threshold: 1% # lower coverage diff threshold target: 100 target: 40 patch: yes From 8cd2cd7d3cc6b5da132e2b61859679274be313b3 Mon Sep 17 00:00:00 2001 From: DarkSide Date: Tue, 28 Apr 2020 15:26:09 +0300 Subject: [PATCH 21/21] comment --- codecov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codecov.yml b/codecov.yml index 78a74a4..069c9a3 100644 --- a/codecov.yml +++ b/codecov.yml @@ -10,7 +10,7 @@ coverage: status: project: default: - threshold: 1% # lower coverage diff threshold + threshold: 1% # lower coverage diff threshold, but maybe is not working target: 100 target: 40 patch: yes