2222final class ArrayListTest extends TestCase
2323{
2424 /**
25- * @return array<string, array{array< mixed>}>
25+ * @param array<int, mixed> $elements
2626 */
27- public static function 様々な要素のコレクションを提供 (): array
27+ #[Test]
28+ #[DataProvider('様々な要素のコレクションを提供 ' )]
29+ public function from静的メソッドでインスタンスが作成できる (array $ elements ): void
2830 {
29- return [
30- 'プリミティブ値の配列 ' => [[1 , 2 , 3 , 4 , 5 ]],
31- '文字列の配列 ' => [['apple ' , 'banana ' , 'cherry ' ]],
32- '空の配列 ' => [[]],
33- '混合型の配列 ' => [[1 , 'string ' , true , 3.14 ]],
34- ];
31+ $ collection = ArrayList::from ($ elements );
32+
33+ $ this ->assertInstanceOf (ArrayList::class, $ collection );
34+ $ this ->assertEquals ($ elements , $ collection ->toArray ());
35+ }
36+
37+ /**
38+ * @param array<int,mixed> $elements
39+ */
40+ #[Test]
41+ #[DataProvider('provide独自クラスのコレクションが作成できるCases ' )]
42+ public function 独自クラスのコレクションが作成できる (array $ elements ): void
43+ {
44+ $ collection = ArrayList::from ($ elements );
45+
46+ $ this ->assertInstanceOf (ArrayList::class, $ collection );
47+ $ this ->assertEquals ($ elements , $ collection ->toArray ());
3548 }
3649
3750 /**
3851 * @return array<string, array{array<mixed>}>
3952 */
40- public static function 独自クラスを含むコレクションを提供 (): array
53+ public static function provide独自クラスのコレクションが作成できるCases (): iterable
4154 {
4255 return [
4356 'StringValue配列 ' => [[
@@ -63,32 +76,6 @@ public static function 独自クラスを含むコレクションを提供(): ar
6376 ];
6477 }
6578
66- /**
67- * @param array<int,mixed> $elements
68- */
69- #[Test]
70- #[DataProvider('様々な要素のコレクションを提供 ' )]
71- public function from静的メソッドでインスタンスが作成できる (array $ elements ): void
72- {
73- $ collection = ArrayList::from ($ elements );
74-
75- $ this ->assertInstanceOf (ArrayList::class, $ collection );
76- $ this ->assertEquals ($ elements , $ collection ->toArray ());
77- }
78-
79- /**
80- * @param array<int,mixed> $elements
81- */
82- #[Test]
83- #[DataProvider('独自クラスを含むコレクションを提供 ' )]
84- public function 独自クラスのコレクションが作成できる (array $ elements ): void
85- {
86- $ collection = ArrayList::from ($ elements );
87-
88- $ this ->assertInstanceOf (ArrayList::class, $ collection );
89- $ this ->assertEquals ($ elements , $ collection ->toArray ());
90- }
91-
9279 #[Test]
9380 public function empty静的メソッドで空のコレクションが作成できる (): void
9481 {
@@ -137,6 +124,19 @@ public function tryFrom静的メソッドで有効な配列から成功結果が
137124 $ this ->assertEquals ($ elements , $ collection ->toArray ());
138125 }
139126
127+ /**
128+ * @return array<string, array{array<mixed>}>
129+ */
130+ public static function 様々な要素のコレクションを提供 (): iterable
131+ {
132+ return [
133+ 'プリミティブ値の配列 ' => [[1 , 2 , 3 , 4 , 5 ]],
134+ '文字列の配列 ' => [['apple ' , 'banana ' , 'cherry ' ]],
135+ '空の配列 ' => [[]],
136+ '混合型の配列 ' => [[1 , 'string ' , true , 3.14 ]],
137+ ];
138+ }
139+
140140 #[Test]
141141 public function first関数で先頭要素が取得できる (): void
142142 {
@@ -539,4 +539,42 @@ public function flatMap関数で各要素を変換して平坦化できる(): vo
539539 // 元のコレクションは変更されない(イミュータブル)
540540 $ this ->assertEquals ([ArrayList::from ([1 , 2 ]), ArrayList::from ([3 , 4 ]), ArrayList::from ([5 , 6 ])], $ collection5 ->toArray ());
541541 }
542+
543+ #[Test]
544+ public function filterAs関数で特定のクラスのインスタンスのみを含むコレクションが取得できる (): void
545+ {
546+ $ collection = ArrayList::from ([
547+ StringValue::from ('apple ' ),
548+ IntegerValue::from (10 ),
549+ StringValue::from ('banana ' ),
550+ DecimalValue::from (new Number ('2.5 ' )),
551+ ]);
552+
553+ $ filtered = $ collection
554+ ->filterAs (StringValue::class)
555+ ->values ();
556+
557+ // @phpstan-ignore-next-line
558+ $ this ->assertContainsOnlyInstancesOf (StringValue::class, $ filtered );
559+
560+ $ this ->assertCount (2 , $ filtered );
561+ $ this ->assertEquals ('apple ' , $ filtered [0 ]->value );
562+ $ this ->assertEquals ('banana ' , $ filtered [1 ]->value );
563+ }
564+
565+ #[Test]
566+ public function values関数でキーが連続した整数にリセットされた新しいコレクションが取得できる (): void
567+ {
568+ $ collection = ArrayList::from ([10 , 20 , 30 ]);
569+
570+ $ filteredCollection = $ collection ->filter (static fn ($ x ) => $ x >= 20 );
571+ $ this ->assertCount (2 , $ filteredCollection );
572+ $ this ->assertEquals ($ filteredCollection [1 ], 20 );
573+ $ this ->assertEquals ($ filteredCollection [2 ], 30 );
574+
575+ $ valuesCollection = $ filteredCollection ->values ();
576+ $ this ->assertCount (2 , $ valuesCollection );
577+ $ this ->assertEquals ($ valuesCollection [0 ], 20 );
578+ $ this ->assertEquals ($ valuesCollection [1 ], 30 );
579+ }
542580}
0 commit comments