Skip to content

Commit 00d5be8

Browse files
committed
increasing test coverage
1 parent 6ee1aa0 commit 00d5be8

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

tests/TableTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ public function test_can_get_column_from_last(): void
149149
$this->assertNull($table->getFromLast("unknown_column"));
150150
}
151151

152+
public function test_get_from_first_returns_null_on_grouped_table(): void
153+
{
154+
$table = Table::make($this->dataTable);
155+
$grouped = $table->groupBy("product");
156+
157+
$this->assertNull($grouped->getFromFirst("product"));
158+
}
159+
160+
public function test_get_from_last_returns_null_on_grouped_table(): void
161+
{
162+
$table = Table::make($this->dataTable);
163+
$grouped = $table->groupBy("product");
164+
165+
$this->assertNull($grouped->getFromLast("product"));
166+
}
167+
168+
public function test_get_from_first_returns_null_on_empty_table(): void
169+
{
170+
$table = Table::make([]);
171+
172+
$this->assertNull($table->getFromFirst("product"));
173+
}
174+
175+
public function test_get_from_last_returns_null_on_empty_table(): void
176+
{
177+
$table = Table::make([]);
178+
179+
$this->assertNull($table->getFromLast("product"));
180+
}
181+
152182
public function test_can_select(): void
153183
{
154184
$table = Table::make($this->dataTable);
@@ -198,6 +228,17 @@ public function test_can_except(): void
198228
$this->assertCount(5, $table->rows());
199229
}
200230

231+
public function test_except_skips_non_arr_rows_on_grouped_table(): void
232+
{
233+
$table = Table::make($this->dataTable);
234+
$grouped = $table->groupBy("product");
235+
236+
$result = $grouped->except("price");
237+
238+
// non-Arr rows are skipped, so the result is empty
239+
$this->assertCount(0, $result);
240+
}
241+
201242
public function test_can_filter(): void
202243
{
203244
$table = Table::make($this->dataTable);
@@ -323,6 +364,21 @@ public function test_can_create_calculated_field(): void
323364
$this->assertEquals(300, $calculatedTable->last()?->get("new_field"));
324365
}
325366

367+
public function test_calc_skips_non_arr_rows_on_grouped_table(): void
368+
{
369+
$table = Table::make($this->dataTable);
370+
$grouped = $table->groupBy("product");
371+
372+
$result = $grouped->calc("doubled", fn ($item): int|float => $item["price"] * 2);
373+
374+
// calc returns $this, count unchanged
375+
$this->assertCount(4, $result);
376+
// sub-Tables are untouched (no "doubled" field injected)
377+
$firstGroup = $result->first();
378+
$this->assertInstanceOf(Table::class, $firstGroup);
379+
$this->assertNull($firstGroup->first()?->get("doubled"));
380+
}
381+
326382
public function test_can_group(): void
327383
{
328384
$table = Table::make($this->dataTable);
@@ -567,6 +623,24 @@ public function test_can_transform_all_of_the_elements_in_a_specific_column(): v
567623
);
568624
}
569625

626+
public function test_transform_skips_non_arr_rows_on_grouped_table(): void
627+
{
628+
$table = Table::make($this->dataTable);
629+
$grouped = $table->groupBy("product");
630+
631+
$result = $grouped->transform(
632+
"price",
633+
fn ($price): string => number_format($price, 2),
634+
);
635+
636+
// transform returns a new Table, count unchanged
637+
$this->assertCount(4, $result);
638+
// sub-Tables are untouched (price not formatted)
639+
$firstGroup = $result->first();
640+
$this->assertInstanceOf(Table::class, $firstGroup);
641+
$this->assertSame(200, $firstGroup->first()?->get("price"));
642+
}
643+
570644
public function test_can_transform_to_native_array(): void
571645
{
572646
$table = Table::make($this->dataTable);

0 commit comments

Comments
 (0)