From eb6e2751ce36aa522ee055c660a1f1311b2df1cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Thu, 7 Oct 2021 10:31:30 +0200 Subject: [PATCH] use standard db config X --- .github/workflows/test-unit.yml | 20 ++++++++++++++----- demos/Dockerfile | 2 +- demos/_demo-data/create-db.php | 25 +++++++++++++---------- demos/init-db.php | 9 ++++----- phpunit-mssql.xml.dist | 35 --------------------------------- phpunit-mysql.xml.dist | 35 --------------------------------- 6 files changed, 35 insertions(+), 91 deletions(-) delete mode 100644 phpunit-mssql.xml.dist delete mode 100644 phpunit-mysql.xml.dist diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index 08d69685da..9b55bf017f 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -100,7 +100,7 @@ jobs: services: mysql: image: mysql:8 - options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 -e MYSQL_ROOT_PASSWORD=atk4_pass_root -e MYSQL_USER=atk4_test -e MYSQL_PASSWORD=atk4_pass -e MYSQL_DATABASE=atk4_test__data --entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password" + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 -e MYSQL_ROOT_PASSWORD=atk4_pass_root -e MYSQL_USER=atk4_test -e MYSQL_PASSWORD=atk4_pass -e MYSQL_DATABASE=atk4_test__ui --entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password" # mssql: # image: mcr.microsoft.com/mssql/server # env: @@ -142,11 +142,12 @@ jobs: run: | mkdir -p build/logs php -r '(new PDO("mysql:host=mysql", "root", "atk4_pass_root"))->exec("ALTER USER '"'"'atk4_test'"'"'@'"'"'%'"'"' WITH MAX_USER_CONNECTIONS 5");' - php demos/_demo-data/create-db.php + sed -E "s/\(('sqlite:.+)\);/(\$_ENV['DB_DSN'] ?? \\1, \$_ENV['DB_USER'] ?? null, \$_ENV['DB_PASSWD'] ?? null);/g" -i demos/db.default.php - name: "Run tests: SQLite (only for Phpunit)" if: startsWith(matrix.type, 'Phpunit') run: | + php demos/_demo-data/create-db.php if [ -n "$LOG_COVERAGE" ]; then cp tools/CoverageUtil.php demos mkdir coverage @@ -158,11 +159,20 @@ jobs: - name: "Run tests: MySQL (only for Phpunit)" if: startsWith(matrix.type, 'Phpunit') - run: "vendor/bin/phpunit --configuration phpunit-mysql.xml.dist --exclude-group none --no-coverage -v" + env: + DB_DSN: "mysql:dbname=atk4_test__ui;host=mysql" + DB_USER: atk4_test + DB_PASSWD: atk4_pass + + run: | + php demos/_demo-data/create-db.php + vendor/bin/phpunit --exclude-group none --no-coverage -v - name: "Run tests: MSSQL (only for Phpunit)" if: startsWith(matrix.type, 'Phpunit') - run: "vendor/bin/phpunit --configuration phpunit-mssql.xml.dist --exclude-group none --no-coverage -v" + run: | + php demos/_demo-data/create-db.php + vendor/bin/phpunit --exclude-group none --no-coverage -v - name: Upload coverage logs 1/2 (only for "latest" Phpunit) if: env.LOG_COVERAGE @@ -272,7 +282,6 @@ jobs: - name: Init run: | mkdir -p build/logs - php demos/_demo-data/create-db.php sed -i "s~'https://raw.githack.com/atk4/ui/develop/public.*~'/public',~" src/App.php - name: "Run tests: Behat" @@ -287,4 +296,5 @@ jobs: sed -i 's/usleep(100000)/usleep(5000)/' vendor/behat/mink/src/Element/Element.php sed -i 's/usleep(100000)/usleep(5000)/' vendor/behat/mink-selenium2-driver/src/Selenium2Driver.php + php demos/_demo-data/create-db.php vendor/bin/behat -vv --config behat.yml.dist diff --git a/demos/Dockerfile b/demos/Dockerfile index de39ea549a..18b2ad5dc0 100644 --- a/demos/Dockerfile +++ b/demos/Dockerfile @@ -43,4 +43,4 @@ COPY demos demos COPY demos/db.default.php demos RUN php demos/_demo-data/create-db.php -RUN sed -E "s/\(('sqlite:.+')\);/(\$_ENV['DB_DSN'] ?? \\1);/g" -i demos/db.default.php +RUN sed -E "s/\(('sqlite:.+)\);/(\$_ENV['DB_DSN'] ?? \\1, \$_ENV['DB_USER'] ?? null, \$_ENV['DB_PASSWD'] ?? null);/g" -i demos/db.default.php diff --git a/demos/_demo-data/create-db.php b/demos/_demo-data/create-db.php index 1928d8a0e5..0a37767d9d 100644 --- a/demos/_demo-data/create-db.php +++ b/demos/_demo-data/create-db.php @@ -7,14 +7,19 @@ use Atk4\Data\Model; use Atk4\Data\Schema\Migration; +var_dump(file_get_contents(__DIR__ . '/../db.default.php')); +var_dump(getenv('DB_DSN')); + require_once __DIR__ . '/../init-autoloader.php'; $sqliteFile = __DIR__ . '/db.sqlite'; -if (file_exists($sqliteFile)) { - unlink($sqliteFile); +if (!file_exists($sqliteFile)) { + new \Atk4\Data\Persistence\Sql('sqlite:' . $sqliteFile); } +unset($sqliteFile); -$persistence = new \Atk4\Data\Persistence\Sql('sqlite:' . $sqliteFile); +/** @var \Atk4\Data\Persistence\Sql $db */ +require_once __DIR__ . '/../init-db.php'; class ImportModelWithPrefixedFields extends Model { @@ -49,7 +54,7 @@ public function import(array $rowsMulti) } } -$model = new ImportModelWithPrefixedFields($persistence, ['table' => 'client']); +$model = new ImportModelWithPrefixedFields($db, ['table' => 'client']); $model->addField('name', ['type' => 'string']); $model->addField('addresses', ['type' => 'text']); $model->addField('accounts', ['type' => 'text']); @@ -59,7 +64,7 @@ public function import(array $rowsMulti) ['id' => 2, 'name' => 'Jane', 'addresses' => null, 'accounts' => null], ]); -$model = new ImportModelWithPrefixedFields($persistence, ['table' => 'country']); +$model = new ImportModelWithPrefixedFields($db, ['table' => 'country']); $model->addField('iso', ['type' => 'string']); // should be CHAR(2) NOT NULL $model->addField('name', ['type' => 'string']); $model->addField('nicename', ['type' => 'string']); @@ -322,7 +327,7 @@ public function import(array $rowsMulti) ['id' => 253, 'iso' => 'SS', 'name' => 'SOUTH SUDAN', 'nicename' => 'South Sudan', 'iso3' => 'SSD', 'numcode' => 728, 'phonecode' => 211], ]); -$model = new ImportModelWithPrefixedFields($persistence, ['table' => 'file']); +$model = new ImportModelWithPrefixedFields($db, ['table' => 'file']); $model->addField('name', ['type' => 'string']); $model->addField('type', ['type' => 'string']); $model->addField('is_folder', ['type' => 'boolean']); @@ -393,7 +398,7 @@ public function import(array $rowsMulti) ['id' => 61, 'name' => 'Button.php', 'type' => 'php', 'is_folder' => 0, 'parent_folder_id' => 46], ]); -$model = new ImportModelWithPrefixedFields($persistence, ['table' => 'stat']); +$model = new ImportModelWithPrefixedFields($db, ['table' => 'stat']); $model->addField('project_name', ['type' => 'string']); $model->addField('project_code', ['type' => 'string']); $model->addField('description', ['type' => 'text']); @@ -432,7 +437,7 @@ public function import(array $rowsMulti) } $model->import($data); -$model = new ImportModelWithPrefixedFields($persistence, ['table' => 'product_category']); +$model = new ImportModelWithPrefixedFields($db, ['table' => 'product_category']); $model->addField('name', ['type' => 'string']); (new Migration($model))->create(); $model->import([ @@ -441,7 +446,7 @@ public function import(array $rowsMulti) ['id' => 3, 'name' => 'Dairy'], ]); -$model = new ImportModelWithPrefixedFields($persistence, ['table' => 'product_sub_category']); +$model = new ImportModelWithPrefixedFields($db, ['table' => 'product_sub_category']); $model->addField('name', ['type' => 'string']); $model->addField('product_category_id', ['type' => 'bigint']); (new Migration($model))->create(); @@ -457,7 +462,7 @@ public function import(array $rowsMulti) ['id' => 9, 'name' => 'Sugar/Sweetened', 'product_category_id' => 2], ]); -$model = new ImportModelWithPrefixedFields($persistence, ['table' => 'product']); +$model = new ImportModelWithPrefixedFields($db, ['table' => 'product']); $model->addField('name', ['type' => 'string']); $model->addField('brand', ['type' => 'string']); $model->addField('product_category_id', ['type' => 'bigint']); diff --git a/demos/init-db.php b/demos/init-db.php index 08480edd76..3c3acdb422 100644 --- a/demos/init-db.php +++ b/demos/init-db.php @@ -9,12 +9,11 @@ use Mvorisek\Atk4\Hintable\Data\HintablePropertyDef; try { - if (file_exists(__DIR__ . '/db.php')) { - require_once __DIR__ . '/db.php'; - } else { - require_once __DIR__ . '/db.default.php'; - } + require_once file_exists(__DIR__ . '/db.php') + ? __DIR__ . '/db.php' + : __DIR__ . '/db.default.php'; } catch (\PDOException $e) { + echo $e; // do not pass $e unless you can secure DSN! throw (new \Atk4\Ui\Exception('This demo requires access to the database. See "demos/init-db.php"')) ->addMoreInfo('PDO error', $e->getMessage()); diff --git a/phpunit-mssql.xml.dist b/phpunit-mssql.xml.dist deleted file mode 100644 index 2a77358d17..0000000000 --- a/phpunit-mssql.xml.dist +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - tests - tests/DemosTest.php - tests/DemosHttpTest.php - tests/DemosHttpNoExitTest.php - tests/DemosTest.php - tests/DemosHttpTest.php - tests/DemosHttpNoExitTest.php - - - - - demos_http - - - - - - - - src - - - - - - diff --git a/phpunit-mysql.xml.dist b/phpunit-mysql.xml.dist deleted file mode 100644 index 803414a2e7..0000000000 --- a/phpunit-mysql.xml.dist +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - tests - tests/DemosTest.php - tests/DemosHttpTest.php - tests/DemosHttpNoExitTest.php - tests/DemosTest.php - tests/DemosHttpTest.php - tests/DemosHttpNoExitTest.php - - - - - demos_http - - - - - - - - src - - - - - -