Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for every database handler #49

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 108 additions & 9 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,95 @@ on:
- 'phpunit*'
- '.github/workflows/phpunit.yml'

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

permissions:
contents: read

env:
NLS_LANG: 'AMERICAN_AMERICA.UTF8'
NLS_DATE_FORMAT: 'YYYY-MM-DD HH24:MI:SS'
NLS_TIMESTAMP_FORMAT: 'YYYY-MM-DD HH24:MI:SS'
NLS_TIMESTAMP_TZ_FORMAT: 'YYYY-MM-DD HH24:MI:SS'

jobs:
main:
name: PHP ${{ matrix.php-versions }} Unit Tests
runs-on: ubuntu-22.04
name: PHP ${{ matrix.php-versions }} - ${{ matrix.db-platforms }}
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[ci skip]')"
strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']
db-platforms: ['MySQLi', 'SQLite3']
include:
# Postgre
- php-versions: '8.1'
db-platforms: Postgre
# SQLSRV
- php-versions: '8.1'
db-platforms: SQLSRV
# OCI8
- php-versions: '8.1'
db-platforms: OCI8

services:
mysql:
image: mysql:8.0
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: test
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3

postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
ports:
- 5432:5432
options: >-
--health-cmd=pg_isready
--health-interval=10s
--health-timeout=5s
--health-retries=3

mssql:
image: mcr.microsoft.com/mssql/server:2022-latest
env:
MSSQL_SA_PASSWORD: 1Secure*Password1
ACCEPT_EULA: Y
MSSQL_PID: Developer
ports:
- 1433:1433
options: >-
--health-cmd="/opt/mssql-tools18/bin/sqlcmd -C -S 127.0.0.1 -U sa -P 1Secure*Password1 -Q 'SELECT @@VERSION'"
--health-interval=10s
--health-timeout=5s
--health-retries=3

oracle:
image: gvenzl/oracle-xe:21
env:
ORACLE_RANDOM_PASSWORD: true
APP_USER: ORACLE
APP_USER_PASSWORD: ORACLE
ports:
- 1521:1521
options: >-
--health-cmd healthcheck.sh
--health-interval 20s
--health-timeout 10s
--health-retries 10

redis:
image: redis
ports:
Expand All @@ -34,12 +117,27 @@ jobs:
--health-timeout=5s
--health-retries=3

if: "!contains(github.event.head_commit.message, '[ci skip]')"
strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']

steps:
- name: Free Disk Space (Ubuntu)
uses: jlumbroso/free-disk-space@main
with:
# this might remove tools that are actually needed,
# if set to "true" but frees about 6 GB
tool-cache: false

# all of these default to true, but feel free to set to
# "false" if necessary for your workflow
android: true
dotnet: true
haskell: true
large-packages: false
docker-images: true
swap-storage: true

- name: Create database for MSSQL Server
if: matrix.db-platforms == 'SQLSRV'
run: sqlcmd -S 127.0.0.1 -U sa -P 1Secure*Password1 -Q "CREATE DATABASE test"

- name: Checkout
uses: actions/checkout@v4

Expand All @@ -48,7 +146,7 @@ jobs:
with:
php-version: ${{ matrix.php-versions }}
tools: composer, phive, phpunit
extensions: intl, json, mbstring, gd, xdebug, xml, sqlite3, redis
extensions: intl, json, mbstring, gd, xdebug, xml, sqlite3, sqlsrv, oci8, pgsql
coverage: xdebug
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -74,6 +172,7 @@ jobs:
- name: Test with PHPUnit
run: vendor/bin/phpunit --coverage-text
env:
DB: ${{ matrix.db-platforms }}
TERM: xterm-256color
TACHYCARDIA_MONITOR_GA: enabled

Expand All @@ -86,7 +185,7 @@ jobs:
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_PARALLEL: true
COVERALLS_FLAG_NAME: PHP ${{ matrix.php-versions }}
COVERALLS_FLAG_NAME: PHP ${{ matrix.php-versions }} - ${{ matrix.db-platforms }}

coveralls:
needs: [main]
Expand Down
12 changes: 9 additions & 3 deletions src/Models/QueueJobModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ private function skipLocked(string $sql): string
return str_replace('WHERE', $replace, $sql);
}

return $sql .= ' FOR UPDATE SKIP LOCKED';
if ($this->db->DBDriver === 'OCI8') {
$sql = str_replace('SELECT *', 'SELECT "id"', $sql);
// prepare final query
$sql = sprintf('SELECT * FROM "%s" WHERE "id" = (%s)', $this->db->prefixTable($this->table), $sql);
}

return $sql . ' FOR UPDATE SKIP LOCKED';
}

/**
Expand All @@ -111,9 +117,9 @@ private function setPriority(BaseBuilder $builder, array $priority): BaseBuilder
$builder->whereIn('priority', $priority);

if ($priority !== ['default']) {
if ($this->db->DBDriver === 'SQLite3') {
if ($this->db->DBDriver !== 'MySQLi') {
$builder->orderBy(
'CASE priority '
sprintf('CASE %s ', $this->db->protectIdentifiers('priority'))
. implode(
' ',
array_map(static fn ($value, $key) => "WHEN '{$value}' THEN {$key}", $priority, array_keys($priority))
Expand Down
60 changes: 30 additions & 30 deletions tests/DatabaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public function testPush(): void
$result = $handler->push('queue', 'success', ['key' => 'value']);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
'available_at' => '1703859316',
$this->seeInDatabaseExtended('queue_jobs', [
'queue' => 'queue',
$this->field('payload') => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
'available_at' => 1703859316,
]);
}

Expand All @@ -104,11 +104,11 @@ public function testPushWithPriority(): void
$result = $handler->setPriority('high')->push('queue', 'success', ['key' => 'value']);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
'priority' => 'high',
'available_at' => '1703859316',
$this->seeInDatabaseExtended('queue_jobs', [
'queue' => 'queue',
$this->field('payload') => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
'priority' => 'high',
'available_at' => 1703859316,
]);
}

Expand All @@ -120,21 +120,21 @@ public function testPushAndPopWithPriority(): void
$result = $handler->push('queue', 'success', ['key1' => 'value1']);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]),
'priority' => 'low',
'available_at' => '1703859316',
$this->seeInDatabaseExtended('queue_jobs', [
'queue' => 'queue',
$this->field('payload') => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]),
'priority' => 'low',
'available_at' => 1703859316,
]);

$result = $handler->setPriority('high')->push('queue', 'success', ['key2' => 'value2']);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
'priority' => 'high',
'available_at' => '1703859316',
$this->seeInDatabaseExtended('queue_jobs', [
'queue' => 'queue',
$this->field('payload') => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
'priority' => 'high',
'available_at' => 1703859316,
]);

$result = $handler->pop('queue', ['high', 'low']);
Expand Down Expand Up @@ -205,7 +205,7 @@ public function testPop(): void
$result = $handler->pop('queue1', ['default']);

$this->assertInstanceOf(QueueJob::class, $result);
$this->seeInDatabase('queue_jobs', [
$this->seeInDatabaseExtended('queue_jobs', [
'status' => Status::RESERVED->value,
'available_at' => 1_697_269_860,
]);
Expand All @@ -232,15 +232,15 @@ public function testLater(): void
$handler = new DatabaseHandler($this->config);
$queueJob = $handler->pop('queue1', ['default']);

$this->seeInDatabase('queue_jobs', [
$this->seeInDatabaseExtended('queue_jobs', [
'id' => 2,
'status' => Status::RESERVED->value,
]);

$result = $handler->later($queueJob, 60);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
$this->seeInDatabaseExtended('queue_jobs', [
'id' => 2,
'status' => Status::PENDING->value,
'available_at' => Time::now()->addSeconds(60)->timestamp,
Expand All @@ -264,11 +264,11 @@ public function testFailedAndKeepJob(): void
$this->dontSeeInDatabase('queue_jobs', [
'id' => 2,
]);
$this->seeInDatabase('queue_jobs_failed', [
$this->seeInDatabaseExtended('queue_jobs_failed', [
'id' => 2,
'connection' => 'database',
'queue' => 'queue1',
'failed_at' => '1703859316',
'failed_at' => 1703859316,
]);
}

Expand Down Expand Up @@ -302,7 +302,7 @@ public function testDoneAndKeepJob(): void
$result = $handler->done($queueJob, true);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
$this->seeInDatabaseExtended('queue_jobs', [
'id' => 2,
'status' => Status::DONE->value,
]);
Expand Down Expand Up @@ -346,10 +346,10 @@ public function testRetry(): void

$this->assertSame($count, 1);

$this->seeInDatabase('queue_jobs', [
'id' => 3,
'queue' => 'queue1',
'payload' => json_encode(['job' => 'failure', 'data' => []]),
$this->seeInDatabaseExtended('queue_jobs', [
'id' => 3,
'queue' => 'queue1',
$this->field('payload') => json_encode(['job' => 'failure', 'data' => []]),
]);
$this->dontSeeInDatabase('queue_jobs_failed', [
'id' => 1,
Expand Down Expand Up @@ -396,7 +396,7 @@ public function testFlush(): void
$this->dontSeeInDatabase('queue_jobs_failed', [
'id' => 1,
]);
$this->seeInDatabase('queue_jobs_failed', [
$this->seeInDatabaseExtended('queue_jobs_failed', [
'id' => 2,
]);
}
Expand Down
6 changes: 2 additions & 4 deletions tests/Models/QueueJobModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ public function testSkipLocked(): void
if ($model->db->DBDriver === 'SQLite3') {
$this->assertSame($sql, $result);
} elseif ($model->db->DBDriver === 'SQLSRV') {
$expected = 'SELECT * FROM queue_jobs WITH (ROWLOCK,UPDLOCK,READPAST) WHERE queue = "test" AND status = 0 AND available_at < 123456 LIMIT 1';
$this->assertSame($expected, $result);
$this->assertStringContainsString('WITH (ROWLOCK,UPDLOCK,READPAST) WHERE', $result);
} else {
$expected = 'SELECT * FROM queue_jobs WHERE queue = "test" AND status = 0 AND available_at < 123456 LIMIT 1 FOR UPDATE SKIP LOCKED';
$this->assertSame($expected, $result);
$this->assertStringContainsString('FOR UPDATE SKIP LOCKED', $result);
}
}

Expand Down
Loading
Loading