utility.collection/tests/CollectionTest.php
2025-04-10 04:17:42 +01:00

107 lines
No EOL
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Loom\Utility\Collection\Tests;
use Loom\Utility\Collection\Collection;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class CollectionTest extends TestCase
{
#[DataProvider('addDataProvider')]
public function testAdd(array $startingItems, mixed $itemToAdd, array $expectedResult): void
{
$collection = new Collection($startingItems);
$collection->add($itemToAdd);
$this->assertEquals($expectedResult, $collection->toArray());
}
#[DataProvider('removeDataProvider')]
public function testRemove(array $startingArray, mixed $itemToRemove, array $expectedResult): void
{
$collection = new Collection($startingArray);
$collection->remove($itemToRemove);
$this->assertEquals($expectedResult, $collection->toArray());
}
#[DataProvider('countDataProvider')]
public function testCount(array $items, int $expectedResult): void
{
$collection = new Collection($items);
$this->assertEquals($expectedResult, $collection->count());
}
public function testIteration(): void
{
$array = ['A', 'B', 'C'];
$collection = new Collection($array);
foreach ($collection as $key => $value) {
$this->assertEquals($array[$key], $value);
}
}
public static function addDataProvider(): array
{
return [
[
'startingItems' => ['A', 'B', 'C'],
'itemToAdd' => 'D',
'expectedResult' => ['A', 'B', 'C', 'D'],
],
[
'startingItems' => [],
'itemToAdd' => 1,
'expectedResult' => [1],
],
[
'startingItems' => [],
'itemToAdd' => null,
'expectedResult' => [],
],
];
}
public static function removeDataProvider(): array
{
return [
[
'startingArray' => ['A', 'B', 'C'],
'itemToRemove' => 'B',
'expectedResult' => ['A', 'C'],
],
[
'startingArray' => ['A', 'B', 'C'],
'itemToRemove' => 'orange',
'expectedResult' => ['A', 'B', 'C'],
],
[
'startingArray' => [],
'itemToRemove' => 'A',
'expectedResult' => [],
],
];
}
public static function countDataProvider(): array
{
return [
[
'items' => ['A', 'B', 'C'],
'expectedResult' => 3,
],
[
'items' => [],
'expectedResult' => 0,
],
[
'items' => [1, 2, 3, 4, 5],
'expectedResult' => 5,
],
];
}
}