Skip to content

Commit bb4ccaa

Browse files
authored
Add real MySQL and PostgreSQL coverage for ORM query and relationship test suites (#284)
2 parents 38fb371 + d26bbfa commit bb4ccaa

29 files changed

Lines changed: 1410 additions & 1099 deletions

.github/workflows/tests.yml

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,93 @@ jobs:
7171
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --with="phpunit/phpunit:~${{ matrix.phpunit }}"
7272

7373
- name: Execute tests
74-
run: vendor/bin/phpunit
74+
run: vendor/bin/phpunit --exclude-group database-external
7575
env:
7676
DB_PORT: ${{ job.services.mysql.ports[3306] }}
7777
DB_USERNAME: root
7878

79+
database_driver_tests:
80+
runs-on: ubuntu-24.04
81+
82+
services:
83+
mysql:
84+
image: mysql:5.7
85+
env:
86+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
87+
MYSQL_DATABASE: testing
88+
ports:
89+
- 33306:3306
90+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
91+
postgres:
92+
image: postgres:16
93+
env:
94+
POSTGRES_DB: testing
95+
POSTGRES_USER: postgres
96+
POSTGRES_PASSWORD: postgres
97+
ports:
98+
- 35432:5432
99+
options: >-
100+
--health-cmd="pg_isready -U postgres -d testing"
101+
--health-interval=10s
102+
--health-timeout=5s
103+
--health-retries=5
104+
105+
strategy:
106+
fail-fast: false
107+
matrix:
108+
driver: [mysql, pgsql]
109+
110+
name: External DB - ${{ matrix.driver }}
111+
112+
steps:
113+
- name: Checkout code
114+
uses: actions/checkout@v4
115+
116+
- name: Setup PHP
117+
uses: shivammathur/setup-php@v2
118+
with:
119+
php-version: 8.5
120+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, pdo_mysql, pgsql, pdo_pgsql
121+
ini-values: error_reporting=E_ALL
122+
tools: composer:v2
123+
coverage: none
124+
125+
- name: Set Framework version
126+
run: composer config version "2.x-dev"
127+
128+
- name: Install dependencies
129+
uses: nick-fields/retry@v3
130+
with:
131+
timeout_minutes: 5
132+
max_attempts: 5
133+
command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress --with="phpunit/phpunit:~12.5.8"
134+
135+
- name: Execute MySQL model query tests
136+
if: matrix.driver == 'mysql'
137+
run: vendor/bin/phpunit --group mysql
138+
env:
139+
DOPPAR_TEST_MYSQL_HOST: 127.0.0.1
140+
DOPPAR_TEST_MYSQL_PORT: ${{ job.services.mysql.ports[3306] }}
141+
DOPPAR_TEST_MYSQL_DATABASE: testing
142+
DOPPAR_TEST_MYSQL_USERNAME: root
143+
DOPPAR_TEST_MYSQL_PASSWORD: ''
144+
DOPPAR_TEST_MYSQL_CHARSET: utf8mb4
145+
146+
- name: Execute PostgreSQL model query tests
147+
if: matrix.driver == 'pgsql'
148+
run: vendor/bin/phpunit --group pgsql
149+
env:
150+
DOPPAR_TEST_PGSQL_HOST: 127.0.0.1
151+
DOPPAR_TEST_PGSQL_PORT: ${{ job.services.postgres.ports[5432] }}
152+
DOPPAR_TEST_PGSQL_DATABASE: testing
153+
DOPPAR_TEST_PGSQL_USERNAME: postgres
154+
DOPPAR_TEST_PGSQL_PASSWORD: postgres
155+
79156
- name: Store artifacts
80157
if: always()
81158
uses: actions/upload-artifact@v4
82159
with:
83-
name: linux-logs-${{ matrix.php }}-${{ matrix.phpunit }}-${{ matrix.stability }}
160+
name: external-db-logs-${{ matrix.driver }}
84161
path: |
85162
vendor/orchestra/testbench-core/doppar/storage/logs
86163
!vendor/**/.gitignore
@@ -132,7 +209,7 @@ jobs:
132209
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --with="phpunit/phpunit:~${{ matrix.phpunit }}"
133210

134211
- name: Execute tests
135-
run: vendor/bin/phpunit
212+
run: vendor/bin/phpunit --exclude-group database-external
136213

137214
- name: Store artifacts
138215
if: always()
@@ -141,4 +218,4 @@ jobs:
141218
name: windows-logs-${{ matrix.php }}-${{ matrix.phpunit }}-${{ matrix.stability }}
142219
path: |
143220
vendor/orchestra/testbench-core/doppar/storage/logs
144-
!vendor/**/.gitignore
221+
!vendor/**/.gitignore

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@
5353
"Tests\\": "tests/"
5454
}
5555
},
56+
"scripts": {
57+
"test": "vendor/bin/phpunit --exclude-group database-external",
58+
"test:sqlite": "@test",
59+
"test:mysql": "vendor/bin/phpunit --group mysql",
60+
"test:pgsql": "vendor/bin/phpunit --group pgsql",
61+
"test:all": "vendor/bin/phpunit"
62+
},
5663
"extra": {
5764
"branch-alias": {
5865
"dev-master": "3.x-dev"
@@ -74,4 +81,4 @@
7481
},
7582
"minimum-stability": "dev",
7683
"prefer-stable": true
77-
}
84+
}

src/Phaseolies/Database/Entity/Query/InteractsWithNestedRelations.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ protected function buildNestedRelationshipSubquery(Model $model, array $relation
187187
$escapeValue = fn($val) => $this->escapeValue($val);
188188

189189
$currentModel = $model;
190-
$currentTable = $this->table;
190+
$rootAlias = $this->table . '_sub';
191+
$currentTable = $rootAlias;
191192
$joins = [];
192193
$lastForeignKey = null;
193194
$lastLocalKey = null;
@@ -243,7 +244,7 @@ protected function buildNestedRelationshipSubquery(Model $model, array $relation
243244
}
244245

245246
// Build the final subquery
246-
$subquery = "SELECT 1 FROM {$quote($this->table)} AS {$quote($this->table . '_sub')}";
247+
$subquery = "SELECT 1 FROM {$quote($this->table)} AS {$quote($rootAlias)}";
247248

248249
// Add all the joins
249250
$subquery .= ' ' . implode(' ', $joins);
@@ -254,7 +255,7 @@ protected function buildNestedRelationshipSubquery(Model $model, array $relation
254255
$model->$firstRelation();
255256
$firstLocalKey = $model->getLastLocalKey();
256257

257-
$subquery .= " WHERE {$quote($this->table . '_sub')}.{$quote($firstLocalKey)} = {$quote($this->table)}.{$quote($firstLocalKey)}";
258+
$subquery .= " WHERE {$quote($rootAlias)}.{$quote($firstLocalKey)} = {$quote($this->table)}.{$quote($firstLocalKey)}";
258259
}
259260

260261
// Add the condition on the final table

0 commit comments

Comments
 (0)