Skip to content

Commit 78c5f6e

Browse files
committed
Add unit tests for Map class functionality in MapTest.php
- Implement tests for various methods including from, empty, make, put, putAll, get, has, first, last, sole, slice, reverse, merge, map, filter, reject, reduce, sort, values, keys, and remove. - Include data providers for testing pairs with different types including primitive values, custom classes, and mixed types. - Ensure immutability of the Map collection by verifying that original collections remain unchanged after operations. - Validate behavior for edge cases such as empty collections and non-existent keys.
1 parent 3040ed7 commit 78c5f6e

File tree

12 files changed

+184
-184
lines changed

12 files changed

+184
-184
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ if ($decimalResult->isOk()) {
126126
#### リストコレクション
127127

128128
```php
129-
use WizDevelop\PhpValueObject\Collection\ListCollection;
129+
use WizDevelop\PhpValueObject\Collection\ArrayList;
130130
use WizDevelop\PhpValueObject\Number\IntegerValue;
131131

132132
// 整数値オブジェクトのコレクション
@@ -136,7 +136,7 @@ $numbers = [
136136
IntegerValue::from(3),
137137
];
138138

139-
$list = ListCollection::from($numbers);
139+
$list = ArrayList::from($numbers);
140140

141141
// マップ操作
142142
$doubled = $list->map(fn ($num) => $num->mul(IntegerValue::from(2)));
@@ -158,11 +158,11 @@ $last = $list->last(); // 3
158158
#### マップコレクション
159159

160160
```php
161-
use WizDevelop\PhpValueObject\Collection\MapCollection;
161+
use WizDevelop\PhpValueObject\Collection\Map;
162162
use WizDevelop\PhpValueObject\String\StringValue;
163163

164164
// 文字列値オブジェクトの連想配列
165-
$map = MapCollection::from([
165+
$map = Map::from([
166166
'name' => StringValue::from('John'),
167167
'email' => StringValue::from('[email protected]'),
168168
]);
@@ -287,14 +287,14 @@ protected static function max(): \BcMath\Number // 最大値
287287
protected static function isValid(\BcMath\Number $value): Result // 追加の検証ロジック
288288
```
289289

290-
### ListCollection API
290+
### ArrayList API
291291

292292
```php
293293
// 静的ファクトリメソッド
294-
ListCollection::from(array $elements): static
295-
ListCollection::tryFrom(array $elements): Result<static, CollectionValueError>
296-
ListCollection::empty(): static
297-
ListCollection::make(iterable $items = []): static
294+
ArrayList::from(array $elements): static
295+
ArrayList::tryFrom(array $elements): Result<static, CollectionValueError>
296+
ArrayList::empty(): static
297+
ArrayList::make(iterable $items = []): static
298298

299299
// 要素の取得
300300
public function first(?Closure $closure = null, $default = null): mixed
@@ -309,8 +309,8 @@ public function slice(int $offset, ?int $length = null): static
309309
public function reverse(): static
310310
public function push(...$values): static
311311
public function add($element): static
312-
public function concat(IListCollection $other): self
313-
public function merge(IListCollection $other): self
312+
public function concat(IArrayList $other): self
313+
public function merge(IArrayList $other): self
314314

315315
// 高階関数
316316
public function map(Closure $closure): self
@@ -332,20 +332,20 @@ public function offsetExists(mixed $offset): bool
332332
public function offsetGet(mixed $offset): mixed
333333
```
334334

335-
### MapCollection API
335+
### Map API
336336

337337
```php
338338
// 静的ファクトリメソッド
339-
MapCollection::from(array $elements): static
340-
MapCollection::tryFrom(array $elements): Result<static, CollectionValueError>
341-
MapCollection::empty(): static
342-
MapCollection::make(iterable $items = []): static
339+
Map::from(array $elements): static
340+
Map::tryFrom(array $elements): Result<static, CollectionValueError>
341+
Map::empty(): static
342+
Map::make(iterable $items = []): static
343343

344344
// 要素の取得
345345
public function get(string $key, $default = null): mixed
346346
public function has(string $key): bool
347-
public function keys(): ListCollection
348-
public function values(): ListCollection
347+
public function keys(): ArrayList
348+
public function values(): ArrayList
349349

350350
// コレクション操作
351351
public function toArray(): array
@@ -455,13 +455,13 @@ final readonly class Age extends IntegerValue
455455
### カスタムコレクションの作成
456456

457457
```php
458-
use WizDevelop\PhpValueObject\Collection\ListCollection;
458+
use WizDevelop\PhpValueObject\Collection\ArrayList;
459459

460460
/**
461461
* @template T of User
462-
* @extends ListCollection<T>
462+
* @extends ArrayList<T>
463463
*/
464-
final readonly class UserCollection extends ListCollection
464+
final readonly class UserCollection extends ArrayList
465465
{
466466
// コレクションサイズの制約をオーバーライド
467467
protected static function minCount(): int

src/Collection/ListCollection.php renamed to src/Collection/ArrayList.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616
use WizDevelop\PhpValueObject\Collection\Base\CountableDefault;
1717
use WizDevelop\PhpValueObject\Collection\Exception\CollectionNotFoundException;
1818
use WizDevelop\PhpValueObject\Collection\Exception\MultipleCollectionsFoundException;
19-
use WizDevelop\PhpValueObject\Collection\List\IListCollection;
20-
use WizDevelop\PhpValueObject\Collection\List\IListCollectionFactory;
19+
use WizDevelop\PhpValueObject\Collection\List\IArrayList;
20+
use WizDevelop\PhpValueObject\Collection\List\IArrayListFactory;
2121

2222
/**
2323
* リストコレクション
2424
* @template TValue
2525
* @extends CollectionBase<int,TValue>
26-
* @implements IListCollection<TValue>
27-
* @implements IListCollectionFactory<TValue>
26+
* @implements IArrayList<TValue>
27+
* @implements IArrayListFactory<TValue>
2828
* @implements ArrayAccess<int,TValue>
2929
*/
30-
readonly class ListCollection extends CollectionBase implements IListCollection, IListCollectionFactory, ArrayAccess
30+
readonly class ArrayList extends CollectionBase implements IArrayList, IArrayListFactory, ArrayAccess
3131
{
3232
/** @use ArrayAccessDefault<int,TValue> */
3333
use ArrayAccessDefault;
@@ -80,7 +80,7 @@ final public function toArray(): array
8080
}
8181

8282
// -------------------------------------------------------------------------
83-
// NOTE: IListCollectionFactory
83+
// NOTE: IArrayListFactory
8484
// -------------------------------------------------------------------------
8585
#[Override]
8686
final public static function from(array $elements): static
@@ -105,7 +105,7 @@ final public static function empty(): static
105105
}
106106

107107
// -------------------------------------------------------------------------
108-
// NOTE: IListCollection
108+
// NOTE: IArrayList
109109
// -------------------------------------------------------------------------
110110
#[Override]
111111
final public static function make(iterable $items = []): static
@@ -225,11 +225,11 @@ final public function push(...$values): static
225225
/**
226226
* @template TValue2
227227
*
228-
* @param IListCollection<TValue2> $other
228+
* @param IArrayList<TValue2> $other
229229
* @return self<TValue|TValue2>
230230
*/
231231
#[Override]
232-
final public function concat(IListCollection $other): self
232+
final public function concat(IArrayList $other): self
233233
{
234234
$elements = $this->elements;
235235

@@ -243,11 +243,11 @@ final public function concat(IListCollection $other): self
243243
/**
244244
* @template TValue2
245245
*
246-
* @param IListCollection<TValue2> $other
246+
* @param IArrayList<TValue2> $other
247247
* @return self<TValue|TValue2>
248248
*/
249249
#[Override]
250-
final public function merge(IListCollection $other): self
250+
final public function merge(IArrayList $other): self
251251
{
252252
return new self(array_merge($this->elements, $other->toArray()));
253253
}

src/Collection/Base/CollectionBase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Override;
88
use WizDevelop\PhpMonad\Result;
9-
use WizDevelop\PhpValueObject\Collection\CollectionValueError;
9+
use WizDevelop\PhpValueObject\Collection\CollectionError;
1010
use WizDevelop\PhpValueObject\IValueObject;
1111

1212
use function count;
@@ -66,7 +66,7 @@ abstract protected static function maxCount(): int;
6666
* @template TIsValidCountKey of TKey|array-key
6767
* @template TIsValidCountValue of TValue
6868
* @param array<TIsValidCountKey,TIsValidCountValue> $elements
69-
* @return Result<bool,CollectionValueError>
69+
* @return Result<bool,CollectionError>
7070
*/
7171
final protected static function isValidCount(array $elements): Result
7272
{
@@ -75,22 +75,22 @@ final protected static function isValidCount(array $elements): Result
7575
$max_count = static::maxCount() < self::MAX_COUNT ? static::maxCount() : self::MAX_COUNT;
7676

7777
if ($element_count < $min_count && $element_count > $max_count) {
78-
return Result\err(CollectionValueError::invalidRange(
78+
return Result\err(CollectionError::invalidRange(
7979
className: static::class,
8080
min: $min_count,
8181
max: $max_count,
8282
count: $element_count,
8383
));
8484
}
8585
if ($element_count < $min_count) {
86-
return Result\err(CollectionValueError::invalidMinCount(
86+
return Result\err(CollectionError::invalidMinCount(
8787
className: static::class,
8888
min: $min_count,
8989
count: $element_count,
9090
));
9191
}
9292
if ($element_count > $max_count) {
93-
return Result\err(CollectionValueError::invalidMaxCount(
93+
return Result\err(CollectionError::invalidMaxCount(
9494
className: static::class,
9595
max: $max_count,
9696
count: $element_count,

src/Collection/CollectionValueError.php renamed to src/Collection/CollectionError.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use WizDevelop\PhpValueObject\ValueObjectError;
99

1010
/**
11-
* CollectionValue エラー
11+
* Collection エラー
1212
* @extends ValueObjectError<CollectionBase<mixed,mixed>>
1313
*/
14-
final readonly class CollectionValueError extends ValueObjectError
14+
final readonly class CollectionError extends ValueObjectError
1515
{
1616
public static function invalid(
1717
string $message,

src/Collection/List/IListCollection.php renamed to src/Collection/List/IArrayList.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
/**
1414
* リストコレクション インターフェース
1515
* @template TValue
16-
* @uses WizDevelop\PhpValueObject\Collection\ListCollection
16+
* @uses WizDevelop\PhpValueObject\Collection\ArrayList
1717
* @extends ICollection<int,TValue>
1818
*/
19-
interface IListCollection extends ICollection
19+
interface IArrayList extends ICollection
2020
{
2121
/**
2222
* Create a new collection instance if the value isn't one already.
@@ -99,8 +99,8 @@ public function push(...$values): static;
9999
*
100100
* @template TValue2
101101
*
102-
* @param IListCollection<TValue2> $other
103-
* @return IListCollection<TValue|TValue2>
102+
* @param IArrayList<TValue2> $other
103+
* @return IArrayList<TValue|TValue2>
104104
*/
105105
public function concat(self $other): self;
106106

@@ -112,16 +112,16 @@ public function concat(self $other): self;
112112
*
113113
* @template TValue2
114114
*
115-
* @param IListCollection<TValue2> $other
116-
* @return IListCollection<TValue|TValue2>
115+
* @param IArrayList<TValue2> $other
116+
* @return IArrayList<TValue|TValue2>
117117
*/
118118
public function merge(self $other): self;
119119

120120
/**
121121
* @template TMapValue
122122
*
123123
* @param Closure(TValue,int): TMapValue $closure
124-
* @return IListCollection<TMapValue>
124+
* @return IArrayList<TMapValue>
125125
*/
126126
public function map(Closure $closure): self;
127127

src/Collection/List/IListCollectionFactory.php renamed to src/Collection/List/IArrayListFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
namespace WizDevelop\PhpValueObject\Collection\List;
66

77
use WizDevelop\PhpMonad\Result;
8-
use WizDevelop\PhpValueObject\Collection\CollectionValueError;
8+
use WizDevelop\PhpValueObject\Collection\CollectionError;
99

1010
/**
1111
* リストコレクション ファクトリインターフェース
12-
* @see WizDevelop\PhpValueObject\Collection\ListCollection
12+
* @see WizDevelop\PhpValueObject\Collection\ArrayList
1313
*
1414
* @template TValue
1515
*/
16-
interface IListCollectionFactory
16+
interface IArrayListFactory
1717
{
1818
/**
1919
* 信頼できるプリミティブ値からインスタンスを生成する
@@ -30,8 +30,8 @@ public static function from(array $elements): static;
3030
*
3131
* @template TTryFromValue of TValue
3232
*
33-
* @param array<int,TTryFromValue> $elements
34-
* @return Result<static<TTryFromValue>,CollectionValueError>
33+
* @param array<int,TTryFromValue> $elements
34+
* @return Result<static<TTryFromValue>,CollectionError>
3535
*/
3636
public static function tryFrom(array $elements): Result;
3737

0 commit comments

Comments
 (0)