Files
2026-11-11 13:18:47 +00:00

261 lines
6.6 KiB
PHP

<?php
namespace Tests\Feature\Db2;
use Illuminate\Database\QueryException;
class UpsertTest extends Db2TestCase
{
private string $tableName = '';
protected function setUp(): void
{
parent::setUp();
$this->tableName = 'DB2_TEST.DB2T_'.
strtoupper(bin2hex(random_bytes(6)));
$this->db()->statement("
CREATE TABLE {$this->tableName} (
ID INTEGER NOT NULL,
CODE VARCHAR(30) NOT NULL,
NAME VARCHAR(100) NOT NULL,
STATUS VARCHAR(20) NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT UQ_CODE UNIQUE (CODE)
)
");
$this->db()->table($this->tableName)->insert([
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Original',
'STATUS' => 'active',
]);
}
protected function tearDown(): void
{
try {
if ($this->tableName !== '') {
$this->db()->statement(
"DROP TABLE {$this->tableName}"
);
}
} finally {
parent::tearDown();
}
}
public function test_upsert_inserts_new_row(): void
{
$this->db()->table($this->tableName)->upsert(
[[
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'Second',
'STATUS' => 'active',
]],
['ID'],
['NAME', 'STATUS']
);
$this->assertSame(
'Second',
$this->db()->table($this->tableName)
->where('ID', 2)->first()->NAME
);
}
public function test_upsert_updates_existing_row(): void
{
$this->db()->table($this->tableName)->upsert(
[[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Updated',
'STATUS' => 'inactive',
]],
['ID'],
['NAME', 'STATUS']
);
$row = $this->db()->table($this->tableName)
->where('ID', 1)->first();
$this->assertSame('Updated', $row->NAME);
$this->assertSame('inactive', $row->STATUS);
}
public function test_upsert_multiple_rows(): void
{
$this->db()->table($this->tableName)->upsert(
[
[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Changed',
'STATUS' => 'active',
],
[
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'New',
'STATUS' => 'active',
],
],
['ID'],
['NAME']
);
$this->assertSame(
2,
$this->db()->table($this->tableName)->count()
);
$this->assertSame(
'Changed',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
}
public function test_upsert_preserves_unselected_columns(): void
{
$this->db()->table($this->tableName)->upsert(
[[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Changed',
'STATUS' => 'inactive',
]],
['ID'],
['NAME']
);
$row = $this->db()->table($this->tableName)
->where('ID', 1)->first();
$this->assertSame('Changed', $row->NAME);
$this->assertSame('active', $row->STATUS);
}
public function test_insert_or_ignore_inserts_new_row(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'Second',
'STATUS' => 'active',
]);
$this->assertSame(
2,
$this->db()->table($this->tableName)->count()
);
}
public function test_insert_or_ignore_skips_duplicate(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Duplicate',
'STATUS' => 'inactive',
]);
$this->assertSame(
'Original',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
}
public function test_insert_or_ignore_multiple_rows(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Duplicate',
'STATUS' => 'inactive',
],
[
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'New',
'STATUS' => 'active',
],
]);
$this->assertSame(
2,
$this->db()->table($this->tableName)->count()
);
}
public function test_insert_or_ignore_duplicate_unique_code(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 99,
'CODE' => 'A001',
'NAME' => 'Different',
'STATUS' => 'inactive',
]);
$this->assertSame(
1,
$this->db()->table($this->tableName)->count()
);
$this->assertSame(
'Original',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
}
public function test_insert_or_ignore_does_not_hide_check_violations(): void
{
$this->expectException(
QueryException::class
);
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 99,
'CODE' => 'A099',
'NAME' => 'Invalid',
'STATUS' => null,
]);
}
public function test_update_or_insert(): void
{
$this->db()->table($this->tableName)->updateOrInsert(
['ID' => 1],
['NAME' => 'Updated']
);
$this->db()->table($this->tableName)->updateOrInsert(
['ID' => 2],
[
'CODE' => 'A002',
'NAME' => 'Inserted',
'STATUS' => 'active',
]
);
$this->assertSame(
'Updated',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
$this->assertSame(
'Inserted',
$this->db()->table($this->tableName)
->where('ID', 2)->first()->NAME
);
}
}