139 lines
3.8 KiB
PHP
139 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Db2;
|
|
|
|
class PaginationIdentityTest 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 GENERATED ALWAYS AS IDENTITY
|
|
(START WITH 1 INCREMENT BY 1),
|
|
NAME VARCHAR(100) NOT NULL,
|
|
PRIMARY KEY (ID)
|
|
)
|
|
");
|
|
|
|
foreach (range(1, 20) as $number) {
|
|
$this->db()->table($this->tableName)->insert([
|
|
'NAME' => sprintf('Item %02d', $number),
|
|
]);
|
|
}
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
try {
|
|
if ($this->tableName !== '') {
|
|
$this->db()->statement(
|
|
"DROP TABLE {$this->tableName}"
|
|
);
|
|
}
|
|
} finally {
|
|
parent::tearDown();
|
|
}
|
|
}
|
|
|
|
public function test_limit_returns_requested_rows(): void
|
|
{
|
|
$rows = $this->db()->table($this->tableName)
|
|
->orderBy('ID')
|
|
->limit(5)
|
|
->get();
|
|
|
|
$this->assertCount(5, $rows);
|
|
$this->assertSame('Item 01', $rows[0]->NAME);
|
|
$this->assertSame('Item 05', $rows[4]->NAME);
|
|
}
|
|
|
|
public function test_offset_skips_correct_rows(): void
|
|
{
|
|
$rows = $this->db()->table($this->tableName)
|
|
->orderBy('ID')
|
|
->offset(10)
|
|
->limit(5)
|
|
->get();
|
|
|
|
$this->assertCount(5, $rows);
|
|
$this->assertSame('Item 11', $rows[0]->NAME);
|
|
$this->assertSame('Item 15', $rows[4]->NAME);
|
|
}
|
|
|
|
public function test_offset_without_limit(): void
|
|
{
|
|
$rows = $this->db()->table($this->tableName)
|
|
->orderBy('ID')
|
|
->offset(15)
|
|
->get();
|
|
|
|
$this->assertCount(5, $rows);
|
|
$this->assertSame('Item 16', $rows[0]->NAME);
|
|
}
|
|
|
|
public function test_standard_pagination(): void
|
|
{
|
|
$page = $this->db()->table($this->tableName)
|
|
->orderBy('ID')
|
|
->paginate(5, ['*'], 'page', 3);
|
|
|
|
$this->assertSame(20, $page->total());
|
|
$this->assertSame(4, $page->lastPage());
|
|
$this->assertSame(3, $page->currentPage());
|
|
$this->assertCount(5, $page->items());
|
|
$this->assertSame('Item 11', $page->items()[0]->NAME);
|
|
}
|
|
|
|
public function test_simple_pagination(): void
|
|
{
|
|
$page = $this->db()->table($this->tableName)
|
|
->orderBy('ID')
|
|
->simplePaginate(5, ['*'], 'page', 2);
|
|
|
|
$this->assertSame(2, $page->currentPage());
|
|
$this->assertCount(5, $page->items());
|
|
$this->assertSame('Item 06', $page->items()[0]->NAME);
|
|
$this->assertTrue($page->hasMorePages());
|
|
}
|
|
|
|
public function test_insert_get_id_returns_generated_identity(): void
|
|
{
|
|
$id = $this->db()->table($this->tableName)
|
|
->insertGetId(['NAME' => 'New Item']);
|
|
|
|
$this->assertEquals(21, $id);
|
|
|
|
$row = $this->db()->table($this->tableName)
|
|
->where('ID', $id)
|
|
->first();
|
|
|
|
$this->assertNotNull($row);
|
|
$this->assertSame('New Item', $row->NAME);
|
|
}
|
|
|
|
public function test_pagination_does_not_duplicate_rows(): void
|
|
{
|
|
$ids = [];
|
|
|
|
for ($page = 1; $page <= 4; $page++) {
|
|
$result = $this->db()->table($this->tableName)
|
|
->orderBy('ID')
|
|
->paginate(5, ['*'], 'page', $page);
|
|
|
|
foreach ($result->items() as $row) {
|
|
$ids[] = (int) $row->ID;
|
|
}
|
|
}
|
|
|
|
$this->assertCount(20, $ids);
|
|
$this->assertSame(range(1, 20), $ids);
|
|
}
|
|
}
|