Skip to content

Commit 3e28b5b

Browse files
committed
fix: use SA user for MSSQL tests in CI
- Update BaseMSSQLTestCase to read DB_USER and DB_PASS from environment variables - Update ConnectionTests and ErrorTests to use environment variables - Simplify GitHub Actions workflow to use SA user directly (no need to create testuser) - This avoids password policy and user creation issues in CI environment
1 parent 887a39c commit 3e28b5b

4 files changed

Lines changed: 29 additions & 26 deletions

File tree

.github/workflows/tests.yml

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,29 +226,17 @@ jobs:
226226
echo "Waiting for MSSQL... ($i/120)"
227227
sleep 2
228228
done
229-
- name: Create database and user
229+
- name: Create database
230230
run: |
231231
export PATH="$PATH:/opt/mssql-tools18/bin"
232232
# Create database (ignore error if exists)
233233
sqlcmd -S localhost -U sa -P 'Test123!@#' -C -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'testdb') CREATE DATABASE testdb;" || true
234-
# Drop login if exists (for clean state)
235-
sqlcmd -S localhost -U sa -P 'Test123!@#' -C -Q "IF EXISTS (SELECT * FROM sys.server_principals WHERE name = 'testuser') DROP LOGIN testuser;" || true
236-
# Create login with password policy disabled for testing
237-
sqlcmd -S localhost -U sa -P 'Test123!@#' -C -Q "CREATE LOGIN testuser WITH PASSWORD='TestPass123!@#', CHECK_POLICY=OFF, CHECK_EXPIRATION=OFF;"
238-
# Create user in database
239-
sqlcmd -S localhost -U sa -P 'Test123!@#' -C -d testdb -Q "IF EXISTS (SELECT * FROM sys.database_principals WHERE name = 'testuser') DROP USER testuser;" || true
240-
sqlcmd -S localhost -U sa -P 'Test123!@#' -C -d testdb -Q "CREATE USER testuser FOR LOGIN testuser;"
241-
# Grant permissions
242-
sqlcmd -S localhost -U sa -P 'Test123!@#' -C -d testdb -Q "ALTER ROLE db_owner ADD MEMBER testuser;"
243-
# Verify user can connect
244-
echo "Testing connection as testuser..."
245-
sqlcmd -S localhost -U testuser -P 'TestPass123!@#' -C -d testdb -Q "SELECT USER_NAME() AS current_user, DB_NAME() AS current_database;" || (echo "Connection test failed!" && exit 1)
246-
echo "User created and verified successfully!"
234+
echo "Database 'testdb' ready"
247235
- run: composer install --no-interaction --prefer-dist
248236
- run: |
249237
DB_DSN="sqlsrv:Server=localhost,1433;Database=testdb;TrustServerCertificate=yes" \
250-
DB_USER="testuser" \
251-
DB_PASS="TestPass123!@#" \
238+
DB_USER="sa" \
239+
DB_PASS="Test123!@#" \
252240
vendor/bin/phpunit --testsuite="MSSQL Tests" --coverage-clover coverage-mssql.xml
253241
- name: Test MSSQL Examples
254242
run: |

tests/mssql/BaseMSSQLTestCase.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,21 @@ public static function setUpBeforeClass(): void
2626
* USE testdb;
2727
* CREATE USER testuser FOR LOGIN testuser;
2828
* ALTER ROLE db_owner ADD MEMBER testuser;
29+
*
30+
* Or use SA user for CI/testing:
31+
* DB_USER=sa DB_PASS=Test123!@#
2932
*/
33+
// Use environment variables if set (for CI), otherwise use constants
34+
$username = getenv('DB_USER') ?: self::DB_USER;
35+
$password = getenv('DB_PASS') ?: self::DB_PASSWORD;
36+
3037
self::$db = new PdoDb(
3138
'sqlsrv',
3239
[
3340
'host' => self::DB_HOST,
3441
'port' => self::DB_PORT,
35-
'username' => self::DB_USER,
36-
'password' => self::DB_PASSWORD,
42+
'username' => $username,
43+
'password' => $password,
3744
'dbname' => self::DB_NAME,
3845
'trust_server_certificate' => true,
3946
'encrypt' => true,

tests/mssql/ConnectionTests.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ public function testDisconnectAndPing(): void
5959
{
6060
self::$db->disconnect();
6161
$this->assertFalse(self::$db->ping());
62+
$username = getenv('DB_USER') ?: self::DB_USER;
63+
$password = getenv('DB_PASS') ?: self::DB_PASSWORD;
6264
self::$db = new PdoDb('sqlsrv', [
6365
'host' => self::DB_HOST,
6466
'port' => self::DB_PORT,
65-
'username' => self::DB_USER,
66-
'password' => self::DB_PASSWORD,
67+
'username' => $username,
68+
'password' => $password,
6769
'dbname' => self::DB_NAME,
6870
'trust_server_certificate' => true,
6971
'encrypt' => true,
@@ -73,12 +75,14 @@ public function testDisconnectAndPing(): void
7375

7476
public function testAddConnectionAndSwitch(): void
7577
{
78+
$username = getenv('DB_USER') ?: self::DB_USER;
79+
$password = getenv('DB_PASS') ?: self::DB_PASSWORD;
7680
self::$db->addConnection('secondary', [
7781
'driver' => 'sqlsrv',
7882
'host' => self::DB_HOST,
7983
'port' => self::DB_PORT,
80-
'username' => self::DB_USER,
81-
'password' => self::DB_PASSWORD,
84+
'username' => $username,
85+
'password' => $password,
8286
'dbname' => self::DB_NAME,
8387
'trust_server_certificate' => true,
8488
'encrypt' => true,

tests/mssql/ErrorTests.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ public function testInvalidSqlLogsErrorAndException(): void
2525
$testHandler = new TestHandler();
2626
$logger = new Logger('test-db');
2727
$logger->pushHandler($testHandler);
28+
$username = getenv('DB_USER') ?: self::DB_USER;
29+
$password = getenv('DB_PASS') ?: self::DB_PASSWORD;
2830
$db = new PdoDb(
2931
'sqlsrv',
3032
[
3133
'host' => self::DB_HOST,
3234
'port' => self::DB_PORT,
33-
'username' => self::DB_USER,
34-
'password' => self::DB_PASSWORD,
35+
'username' => $username,
36+
'password' => $password,
3537
'dbname' => self::DB_NAME,
3638
'trust_server_certificate' => true,
3739
'encrypt' => true,
@@ -66,13 +68,15 @@ public function testTransactionBeginCommitRollbackLogging(): void
6668
$testHandler = new TestHandler();
6769
$logger = new Logger('test-db');
6870
$logger->pushHandler($testHandler);
71+
$username = getenv('DB_USER') ?: self::DB_USER;
72+
$password = getenv('DB_PASS') ?: self::DB_PASSWORD;
6973
$db = new PdoDb(
7074
'sqlsrv',
7175
[
7276
'host' => self::DB_HOST,
7377
'port' => self::DB_PORT,
74-
'username' => self::DB_USER,
75-
'password' => self::DB_PASSWORD,
78+
'username' => $username,
79+
'password' => $password,
7680
'dbname' => self::DB_NAME,
7781
'trust_server_certificate' => true,
7882
'encrypt' => true,

0 commit comments

Comments
 (0)