Skip to content

Commit

Permalink
Fix column and table names quoting (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jul 18, 2024
1 parent bee9e34 commit ff5bef8
Show file tree
Hide file tree
Showing 27 changed files with 2,225 additions and 48 deletions.
27 changes: 9 additions & 18 deletions .github/workflows/build.yml → .github/workflows/build-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
push:
branches: [ 'master' ]

name: build
name: MySQL build

jobs:
tests:
Expand Down Expand Up @@ -50,23 +50,14 @@ jobs:
coverage: pcov
tools: composer:v2

- name: Determine composer cache directory on Linux
if: matrix.os == 'ubuntu-latest'
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV
- name: Install Composer dependencies
uses: ramsey/composer-install@v3

- name: Cache dependencies installed with composer
uses: actions/cache@v3
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Update composer
run: composer self-update
- name: Run unit tests
run: vendor/bin/codecept run Unit

- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
- name: Run integration tests
run: vendor/bin/codecept run MySqlIntegration

- name: Run tests with codeception
run: vendor/bin/codecept run
- name: Run preload tests
run: vendor/bin/codecept run MySqlPreload
64 changes: 64 additions & 0 deletions .github/workflows/build-pgsql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
on:
pull_request:
push:
branches: [ 'master' ]

name: PgSQL build

jobs:
tests:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

env:
key: cache-v1

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- "8.0"
- "8.1"
- "8.2"
- "8.3"

pgsql:
- 16

services:
postgres:
image: postgres:${{ matrix.pgsql }}
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: db_test
ports:
- 5432:5432
options: --name=postgres --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
ini-values: date.timezone='UTC'
coverage: pcov
tools: composer:v2

- name: Install Composer dependencies
uses: ramsey/composer-install@v3

- name: Run unit tests
run: vendor/bin/codecept run Unit

- name: Run integration tests
run: vendor/bin/codecept run PgSqlIntegration

- name: Run preload tests
run: vendor/bin/codecept run PgSqlPreload
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Database Populator for Codeception DB Module Change Log

## 1.1.1 under development

- Bug #5: Fix column and table names quotation.

## 1.1.0 August 4, 2022

- Enh: Raise minimum required versions: PHP to `^8.0`, `codeception/codeception` to `^5.0` and
Expand Down
14 changes: 11 additions & 3 deletions src/DatabasePopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function loadRows(string ...$sets): void
foreach ($sets as $set) {
/**
* @psalm-suppress UnresolvableInclude
* @psalm-var array<string, array<string,array>> $data
* @psalm-var array<string, list<array<string, mixed>>> $data
*/
$data = require $this->getRowsFilePath($set);
foreach ($data as $table => $rows) {
Expand All @@ -57,6 +57,7 @@ public function loadRows(string ...$sets): void

/**
* @param array[] $rows
* @psalm-param list<array<string,mixed>> $rows
*/
private function insertRows(string $table, array $rows): void
{
Expand All @@ -75,8 +76,15 @@ private function insertRows(string $table, array $rows): void
}

foreach ($requests as $request) {
$columns = array_map(static fn ($c) => "`$c`", $request['columns']);
$sql = 'INSERT INTO ' . $table . ' (' . implode(',', $columns) . ') VALUES ';
$columns = array_map(
fn($c) => $this->dbDriver->getQuotedName($c),
$request['columns']
);
$sql = sprintf(
'INSERT INTO %s (%s) VALUES ',
$this->dbDriver->getQuotedName($table),
implode(',', $columns),
);

$insertQuery = [];
$insertData = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\Integration
actor: IntegrationTester
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\MySqlIntegration
actor: MySqlIntegrationTester
modules:
enabled:
- Db:
dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%'
user: '%DB_USERNAME%'
password: '%DB_PASSWORD%'
- Vjik\Codeception\DatabasePopulator\Module:
dumpsPath: 'tests/_data/dumps'
dumpsPath: 'tests/_data/dumps/mysql'
rowsPath: 'tests/_data/rows'
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

declare(strict_types=1);

namespace Vjik\Codeception\DatabasePopulator\Tests\Integration;
namespace Vjik\Codeception\DatabasePopulator\Tests\MySqlIntegration;

use Codeception\Exception\ModuleException;
use Codeception\Test\Unit;
use PDO;
use Vjik\Codeception\DatabasePopulator\Tests\IntegrationTester;
use Vjik\Codeception\DatabasePopulator\Tests\MySqlIntegrationTester;

use function dirname;

final class DatabasePopulateTest extends Unit
{
/**
* @var IntegrationTester
* @var MySqlIntegrationTester
*/
protected $tester;

Expand All @@ -32,7 +32,7 @@ public function testLoadNotExistDump(): void
$this->expectException(ModuleException::class);
$this->expectExceptionMessage(
"\nFile with dump doesn't exist.\nPlease, check path for SQL-file: " .
dirname(__DIR__) . '/_data/dumps/shop.sql'
dirname(__DIR__) . '/_data/dumps/mysql/shop.sql'
);
$this->tester->loadDump('shop');
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Preload.suite.yml → tests/MySqlPreload.suite.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\Preload
actor: PreloadTester
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\MySqlPreload
actor: MySqlPreloadTester
modules:
enabled:
- Db:
dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%'
user: '%DB_USERNAME%'
password: '%DB_PASSWORD%'
- Vjik\Codeception\DatabasePopulator\Module:
dumpsPath: 'tests/_data/dumps'
dumpsPath: 'tests/_data/dumps/mysql'
rowsPath: 'tests/_data/rows'
preloadDump: 'blog'
preloadRows: 'authors'
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

declare(strict_types=1);

namespace Vjik\Codeception\DatabasePopulator\Tests\Preload;
namespace Vjik\Codeception\DatabasePopulator\Tests\MySqlPreload;

use Codeception\Test\Unit;
use Vjik\Codeception\DatabasePopulator\Tests\PreloadTester;
use Vjik\Codeception\DatabasePopulator\Tests\MySqlPreloadTester;

final class PreloadTest extends Unit
{
/**
* @var PreloadTester
* @var MySqlPreloadTester
*/
protected $tester;

Expand Down
11 changes: 11 additions & 0 deletions tests/PgSqlIntegration.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\PgSqlIntegration
actor: PgSqlIntegrationTester
modules:
enabled:
- Db:
dsn: 'pgsql:host=%DB_HOST%;dbname=%DB_NAME%'
user: '%DB_USERNAME%'
password: '%DB_PASSWORD%'
- Vjik\Codeception\DatabasePopulator\Module:
dumpsPath: 'tests/_data/dumps/pgsql'
rowsPath: 'tests/_data/rows'
51 changes: 51 additions & 0 deletions tests/PgSqlIntegration/DatabasePopulateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Vjik\Codeception\DatabasePopulator\Tests\PgSqlIntegration;

use Codeception\Exception\ModuleException;
use Codeception\Test\Unit;
use PDO;
use Vjik\Codeception\DatabasePopulator\Tests\PgSqlIntegrationTester;

use function dirname;

final class DatabasePopulateTest extends Unit
{
/**
* @var PgSqlIntegrationTester
*/
protected $tester;

public function testBase(): void
{
$this->tester->loadDump('blog');
$this->tester->loadRows('authors');

$this->tester->seeInDatabase('author', ['id' => 1, 'name' => 'Ivan']);
$this->tester->seeInDatabase('author', ['id' => 2, 'name' => 'Petr']);
}

public function testLoadNotExistDump(): void
{
$this->expectException(ModuleException::class);
$this->expectExceptionMessage(
"\nFile with dump doesn't exist.\nPlease, check path for SQL-file: " .
dirname(__DIR__) . '/_data/dumps/pgsql/shop.sql'
);
$this->tester->loadDump('shop');
}

public function testLoadEmptyDump(): void
{
$this->tester->loadDump('blog');
$this->tester->loadDump('empty');

/** @var PDO $pdo */
$pdo = $this->getModule('Db')->_getDbh();
$tableNames = $pdo->query('SELECT table_name FROM information_schema.tables WHERE table_schema=\'public\' AND table_type=\'BASE TABLE\'')->fetchAll(PDO::FETCH_COLUMN);

$this->assertSame([], $tableNames);
}
}
13 changes: 13 additions & 0 deletions tests/PgSqlPreload.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\PgSqlPreload
actor: PgSqlPreloadTester
modules:
enabled:
- Db:
dsn: 'pgsql:host=%DB_HOST%;dbname=%DB_NAME%'
user: '%DB_USERNAME%'
password: '%DB_PASSWORD%'
- Vjik\Codeception\DatabasePopulator\Module:
dumpsPath: 'tests/_data/dumps/pgsql'
rowsPath: 'tests/_data/rows'
preloadDump: 'blog'
preloadRows: 'authors'
22 changes: 22 additions & 0 deletions tests/PgSqlPreload/PreloadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Vjik\Codeception\DatabasePopulator\Tests\PgSqlPreload;

use Codeception\Test\Unit;
use Vjik\Codeception\DatabasePopulator\Tests\PgSqlPreloadTester;

final class PreloadTest extends Unit
{
/**
* @var PgSqlPreloadTester
*/
protected $tester;

public function testBase(): void
{
$this->tester->seeInDatabase('author', ['id' => 1, 'name' => 'Ivan']);
$this->tester->seeInDatabase('author', ['id' => 2, 'name' => 'Petr']);
}
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ff5bef8

Please sign in to comment.