Skip to content

Commit ff5bef8

Browse files
authored
Fix column and table names quoting (#5)
1 parent bee9e34 commit ff5bef8

27 files changed

+2225
-48
lines changed

.github/workflows/build.yml .github/workflows/build-mysql.yml

+9-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ on:
33
push:
44
branches: [ 'master' ]
55

6-
name: build
6+
name: MySQL build
77

88
jobs:
99
tests:
@@ -50,23 +50,14 @@ jobs:
5050
coverage: pcov
5151
tools: composer:v2
5252

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

57-
- name: Cache dependencies installed with composer
58-
uses: actions/cache@v3
59-
with:
60-
path: ${{ env.COMPOSER_CACHE_DIR }}
61-
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
62-
restore-keys: |
63-
php${{ matrix.php }}-composer-
64-
65-
- name: Update composer
66-
run: composer self-update
56+
- name: Run unit tests
57+
run: vendor/bin/codecept run Unit
6758

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

71-
- name: Run tests with codeception
72-
run: vendor/bin/codecept run
62+
- name: Run preload tests
63+
run: vendor/bin/codecept run MySqlPreload

.github/workflows/build-pgsql.yml

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
on:
2+
pull_request:
3+
push:
4+
branches: [ 'master' ]
5+
6+
name: PgSQL build
7+
8+
jobs:
9+
tests:
10+
name: PHP ${{ matrix.php }}-${{ matrix.os }}
11+
12+
env:
13+
key: cache-v1
14+
15+
runs-on: ${{ matrix.os }}
16+
17+
strategy:
18+
matrix:
19+
os:
20+
- ubuntu-latest
21+
22+
php:
23+
- "8.0"
24+
- "8.1"
25+
- "8.2"
26+
- "8.3"
27+
28+
pgsql:
29+
- 16
30+
31+
services:
32+
postgres:
33+
image: postgres:${{ matrix.pgsql }}
34+
env:
35+
POSTGRES_USER: root
36+
POSTGRES_PASSWORD: root
37+
POSTGRES_DB: db_test
38+
ports:
39+
- 5432:5432
40+
options: --name=postgres --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3
41+
42+
steps:
43+
- name: Checkout
44+
uses: actions/checkout@v3
45+
46+
- name: Install PHP
47+
uses: shivammathur/setup-php@v2
48+
with:
49+
php-version: ${{ matrix.php }}
50+
ini-values: date.timezone='UTC'
51+
coverage: pcov
52+
tools: composer:v2
53+
54+
- name: Install Composer dependencies
55+
uses: ramsey/composer-install@v3
56+
57+
- name: Run unit tests
58+
run: vendor/bin/codecept run Unit
59+
60+
- name: Run integration tests
61+
run: vendor/bin/codecept run PgSqlIntegration
62+
63+
- name: Run preload tests
64+
run: vendor/bin/codecept run PgSqlPreload

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Database Populator for Codeception DB Module Change Log
22

3+
## 1.1.1 under development
4+
5+
- Bug #5: Fix column and table names quotation.
6+
37
## 1.1.0 August 4, 2022
48

59
- Enh: Raise minimum required versions: PHP to `^8.0`, `codeception/codeception` to `^5.0` and

src/DatabasePopulator.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function loadRows(string ...$sets): void
4444
foreach ($sets as $set) {
4545
/**
4646
* @psalm-suppress UnresolvableInclude
47-
* @psalm-var array<string, array<string,array>> $data
47+
* @psalm-var array<string, list<array<string, mixed>>> $data
4848
*/
4949
$data = require $this->getRowsFilePath($set);
5050
foreach ($data as $table => $rows) {
@@ -57,6 +57,7 @@ public function loadRows(string ...$sets): void
5757

5858
/**
5959
* @param array[] $rows
60+
* @psalm-param list<array<string,mixed>> $rows
6061
*/
6162
private function insertRows(string $table, array $rows): void
6263
{
@@ -75,8 +76,15 @@ private function insertRows(string $table, array $rows): void
7576
}
7677

7778
foreach ($requests as $request) {
78-
$columns = array_map(static fn ($c) => "`$c`", $request['columns']);
79-
$sql = 'INSERT INTO ' . $table . ' (' . implode(',', $columns) . ') VALUES ';
79+
$columns = array_map(
80+
fn($c) => $this->dbDriver->getQuotedName($c),
81+
$request['columns']
82+
);
83+
$sql = sprintf(
84+
'INSERT INTO %s (%s) VALUES ',
85+
$this->dbDriver->getQuotedName($table),
86+
implode(',', $columns),
87+
);
8088

8189
$insertQuery = [];
8290
$insertData = [];
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\Integration
2-
actor: IntegrationTester
1+
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\MySqlIntegration
2+
actor: MySqlIntegrationTester
33
modules:
44
enabled:
55
- Db:
66
dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%'
77
user: '%DB_USERNAME%'
88
password: '%DB_PASSWORD%'
99
- Vjik\Codeception\DatabasePopulator\Module:
10-
dumpsPath: 'tests/_data/dumps'
10+
dumpsPath: 'tests/_data/dumps/mysql'
1111
rowsPath: 'tests/_data/rows'

tests/Integration/DatabasePopulateTest.php tests/MySqlIntegration/DatabasePopulateTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
declare(strict_types=1);
44

5-
namespace Vjik\Codeception\DatabasePopulator\Tests\Integration;
5+
namespace Vjik\Codeception\DatabasePopulator\Tests\MySqlIntegration;
66

77
use Codeception\Exception\ModuleException;
88
use Codeception\Test\Unit;
99
use PDO;
10-
use Vjik\Codeception\DatabasePopulator\Tests\IntegrationTester;
10+
use Vjik\Codeception\DatabasePopulator\Tests\MySqlIntegrationTester;
1111

1212
use function dirname;
1313

1414
final class DatabasePopulateTest extends Unit
1515
{
1616
/**
17-
* @var IntegrationTester
17+
* @var MySqlIntegrationTester
1818
*/
1919
protected $tester;
2020

@@ -32,7 +32,7 @@ public function testLoadNotExistDump(): void
3232
$this->expectException(ModuleException::class);
3333
$this->expectExceptionMessage(
3434
"\nFile with dump doesn't exist.\nPlease, check path for SQL-file: " .
35-
dirname(__DIR__) . '/_data/dumps/shop.sql'
35+
dirname(__DIR__) . '/_data/dumps/mysql/shop.sql'
3636
);
3737
$this->tester->loadDump('shop');
3838
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\Preload
2-
actor: PreloadTester
1+
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\MySqlPreload
2+
actor: MySqlPreloadTester
33
modules:
44
enabled:
55
- Db:
66
dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%'
77
user: '%DB_USERNAME%'
88
password: '%DB_PASSWORD%'
99
- Vjik\Codeception\DatabasePopulator\Module:
10-
dumpsPath: 'tests/_data/dumps'
10+
dumpsPath: 'tests/_data/dumps/mysql'
1111
rowsPath: 'tests/_data/rows'
1212
preloadDump: 'blog'
1313
preloadRows: 'authors'

tests/Preload/PreloadTest.php tests/MySqlPreload/PreloadTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
declare(strict_types=1);
44

5-
namespace Vjik\Codeception\DatabasePopulator\Tests\Preload;
5+
namespace Vjik\Codeception\DatabasePopulator\Tests\MySqlPreload;
66

77
use Codeception\Test\Unit;
8-
use Vjik\Codeception\DatabasePopulator\Tests\PreloadTester;
8+
use Vjik\Codeception\DatabasePopulator\Tests\MySqlPreloadTester;
99

1010
final class PreloadTest extends Unit
1111
{
1212
/**
13-
* @var PreloadTester
13+
* @var MySqlPreloadTester
1414
*/
1515
protected $tester;
1616

tests/PgSqlIntegration.suite.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\PgSqlIntegration
2+
actor: PgSqlIntegrationTester
3+
modules:
4+
enabled:
5+
- Db:
6+
dsn: 'pgsql:host=%DB_HOST%;dbname=%DB_NAME%'
7+
user: '%DB_USERNAME%'
8+
password: '%DB_PASSWORD%'
9+
- Vjik\Codeception\DatabasePopulator\Module:
10+
dumpsPath: 'tests/_data/dumps/pgsql'
11+
rowsPath: 'tests/_data/rows'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vjik\Codeception\DatabasePopulator\Tests\PgSqlIntegration;
6+
7+
use Codeception\Exception\ModuleException;
8+
use Codeception\Test\Unit;
9+
use PDO;
10+
use Vjik\Codeception\DatabasePopulator\Tests\PgSqlIntegrationTester;
11+
12+
use function dirname;
13+
14+
final class DatabasePopulateTest extends Unit
15+
{
16+
/**
17+
* @var PgSqlIntegrationTester
18+
*/
19+
protected $tester;
20+
21+
public function testBase(): void
22+
{
23+
$this->tester->loadDump('blog');
24+
$this->tester->loadRows('authors');
25+
26+
$this->tester->seeInDatabase('author', ['id' => 1, 'name' => 'Ivan']);
27+
$this->tester->seeInDatabase('author', ['id' => 2, 'name' => 'Petr']);
28+
}
29+
30+
public function testLoadNotExistDump(): void
31+
{
32+
$this->expectException(ModuleException::class);
33+
$this->expectExceptionMessage(
34+
"\nFile with dump doesn't exist.\nPlease, check path for SQL-file: " .
35+
dirname(__DIR__) . '/_data/dumps/pgsql/shop.sql'
36+
);
37+
$this->tester->loadDump('shop');
38+
}
39+
40+
public function testLoadEmptyDump(): void
41+
{
42+
$this->tester->loadDump('blog');
43+
$this->tester->loadDump('empty');
44+
45+
/** @var PDO $pdo */
46+
$pdo = $this->getModule('Db')->_getDbh();
47+
$tableNames = $pdo->query('SELECT table_name FROM information_schema.tables WHERE table_schema=\'public\' AND table_type=\'BASE TABLE\'')->fetchAll(PDO::FETCH_COLUMN);
48+
49+
$this->assertSame([], $tableNames);
50+
}
51+
}

tests/PgSqlPreload.suite.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
suite_namespace: Vjik\Codeception\DatabasePopulator\Tests\PgSqlPreload
2+
actor: PgSqlPreloadTester
3+
modules:
4+
enabled:
5+
- Db:
6+
dsn: 'pgsql:host=%DB_HOST%;dbname=%DB_NAME%'
7+
user: '%DB_USERNAME%'
8+
password: '%DB_PASSWORD%'
9+
- Vjik\Codeception\DatabasePopulator\Module:
10+
dumpsPath: 'tests/_data/dumps/pgsql'
11+
rowsPath: 'tests/_data/rows'
12+
preloadDump: 'blog'
13+
preloadRows: 'authors'

tests/PgSqlPreload/PreloadTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vjik\Codeception\DatabasePopulator\Tests\PgSqlPreload;
6+
7+
use Codeception\Test\Unit;
8+
use Vjik\Codeception\DatabasePopulator\Tests\PgSqlPreloadTester;
9+
10+
final class PreloadTest extends Unit
11+
{
12+
/**
13+
* @var PgSqlPreloadTester
14+
*/
15+
protected $tester;
16+
17+
public function testBase(): void
18+
{
19+
$this->tester->seeInDatabase('author', ['id' => 1, 'name' => 'Ivan']);
20+
$this->tester->seeInDatabase('author', ['id' => 2, 'name' => 'Petr']);
21+
}
22+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)