Files
Testbed/tests/Feature/Db2/QueryCompatibilityTest.php
T
2026-11-11 13:18:47 +00:00

94 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature\Db2;
use Illuminate\Database\Eloquent\Model;
class QueryCompatibilityTest extends Db2TestCase
{
private ?string $table = null;
protected function setUp(): void
{
parent::setUp();
$table = 'DB2_TEST.DB2Q_'.strtoupper(bin2hex(random_bytes(6)));
$this->db()->statement("CREATE TABLE {$table} (ID INTEGER NOT NULL PRIMARY KEY, LABEL VARCHAR(40), CREATED_AT TIMESTAMP NOT NULL, LAST_USED_AT TIMESTAMP)");
$this->table = $table;
$this->db()->statement("INSERT INTO {$table} (ID, LABEL, CREATED_AT, LAST_USED_AT) VALUES
(1, 'Alpha', TIMESTAMP('2026-11-11 08:47:26.123456'), NULL),
(2, 'beta', TIMESTAMP('2026-11-11 08:47:26.123456'), TIMESTAMP('2026-11-12 10:11:12.654321')),
(3, NULL, TIMESTAMP('2026-11-11 08:47:26.123456'), NULL)");
}
protected function tearDown(): void
{
try {
if ($this->table !== null) {
$this->db()->statement("DROP TABLE {$this->table}");
}
} finally {
parent::tearDown();
}
}
public function test_scalar_subquery_with_nullable_projection(): void
{
$db = $this->db();
$label = $db->table($this->table)->select('LABEL')->where('ID', 1);
$row = $db->table($this->table)->selectSub($label, 'matched')->where('ID', 2)->first();
$this->assertSame('Alpha', $row->matched);
}
public function test_where_in_with_nullable_projection(): void
{
$db = $this->db();
$labels = $db->table($this->table)->select('LABEL')->where('ID', 2);
$this->assertSame([2], $db->table($this->table)->whereIn('LABEL', $labels)->pluck('ID')->map(fn ($id) => (int) $id)->all());
}
public function test_union_with_nullable_projection(): void
{
$db = $this->db();
$query = $db->table($this->table)->select('LABEL')->where('ID', 1)
->unionAll($db->table($this->table)->select('LABEL')->where('ID', 2))
->orderBy('LABEL')->offset(1)->limit(1);
$this->assertSame(['beta'], $query->pluck('LABEL')->all());
}
public function test_exists_returns_true_and_false(): void
{
$this->assertTrue($this->db()->table($this->table)->where('ID', 1)->exists());
$this->assertFalse($this->db()->table($this->table)->where('ID', 4)->exists());
}
public function test_where_like_honors_case_sensitivity(): void
{
$db = $this->db();
$this->assertSame([1], $db->table($this->table)->whereLike('LABEL', 'alpha%')->pluck('ID')->map(fn ($id) => (int) $id)->all());
$this->assertSame([], $db->table($this->table)->whereLike('LABEL', 'alpha%', caseSensitive: true)->pluck('ID')->map(fn ($id) => (int) $id)->all());
$this->assertSame([2], $db->table($this->table)->whereNotLike('LABEL', 'ALPHA%')->pluck('ID')->map(fn ($id) => (int) $id)->all());
}
public function test_random_order_executes_on_db2(): void
{
$this->assertCount(2, $this->db()->table($this->table)->select('ID')->inRandomOrder()->limit(2)->get());
}
public function test_eloquent_casts_db2_timestamps_for_security_settings(): void
{
$model = new class extends Model
{
protected $casts = ['CREATED_AT' => 'datetime', 'LAST_USED_AT' => 'datetime'];
};
$model->setConnection('db2_local')->setTable($this->table);
$rows = $model->newQuery()->select('ID', 'CREATED_AT', 'LAST_USED_AT')->orderBy('ID')->get();
$this->assertSame('2026-11-11 08:47:26.123456', $rows[0]->CREATED_AT->format('Y-m-d H:i:s.u'));
$this->assertNull($rows[0]->LAST_USED_AT);
$this->assertSame('2026-11-12 10:11:12.654321', $rows[1]->LAST_USED_AT->format('Y-m-d H:i:s.u'));
}
}