116 lines
No EOL
3.2 KiB
PHP
116 lines
No EOL
3.2 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, string $key = null): void
|
|
{
|
|
$collection = new Collection($startingItems);
|
|
$collection->add($itemToAdd, $key);
|
|
|
|
$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'],
|
|
'key' => null,
|
|
],
|
|
[
|
|
'startingItems' => [],
|
|
'itemToAdd' => 1,
|
|
'expectedResult' => [1],
|
|
'key' => null,
|
|
],
|
|
[
|
|
'startingItems' => [],
|
|
'itemToAdd' => null,
|
|
'expectedResult' => [],
|
|
'key' => null,
|
|
],
|
|
[
|
|
'startingItems' => ['A' => 'A', 'B' => 'B', 'C' => 'C'],
|
|
'itemToAdd' => 'D',
|
|
'expectedResult' => ['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D'],
|
|
'key' => 'D',
|
|
]
|
|
];
|
|
}
|
|
|
|
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,
|
|
],
|
|
];
|
|
}
|
|
} |