@@ -22,53 +22,82 @@ final class SortIterableAggregateTest extends TestCase
22
22
{
23
23
public function testDoNothing (): void
24
24
{
25
- $ input = [ 'a ' , 'b ' , ' c ' , ' d ' , ' e ' , ' f ' ] ;
25
+ $ input = range ( 'a ' , 'f ' ) ;
26
26
27
- $ iterator = (new SortIterableAggregate ($ input , static fn ($ right , $ left ): int => $ left <=> $ right ));
27
+ $ iterator = (new SortIterableAggregate ($ input , static fn (string $ left , string $ right ): int => $ left <=> $ right ));
28
28
29
- $ expected = [];
29
+ $ actual = array_reduce (
30
+ iterator_to_array ($ iterator ),
31
+ static fn (array $ carry , string $ item ): array => [...$ carry , $ item ],
32
+ []
33
+ );
30
34
31
- foreach ($ iterator as $ value ) {
32
- $ expected [] = $ value ;
33
- }
34
-
35
- self ::assertSame ($ expected , $ input );
35
+ self ::assertSame ($ input , $ actual );
36
36
}
37
37
38
38
public function testSimpleSort (): void
39
39
{
40
40
$ input = array_combine (range ('c ' , 'a ' ), range ('c ' , 'a ' ));
41
41
42
- $ iterator = (new SortIterableAggregate ($ input , static fn ($ right , $ left ): int => $ left <=> $ right ));
42
+ $ iterator = (new SortIterableAggregate ($ input , static fn (string $ left , string $ right ): int => $ left <=> $ right ));
43
+
44
+ $ actual = array_reduce (
45
+ iterator_to_array ($ iterator ),
46
+ static fn (array $ carry , string $ item ): array => [...$ carry , $ item ],
47
+ []
48
+ );
49
+
50
+ self ::assertSame (range ('a ' , 'c ' ), $ actual );
51
+ }
52
+
53
+ public function testSortingDirectionMatchUsort (): void
54
+ {
55
+ $ input = [
56
+ self ::createValueObject (id: 1 , weight: 2 ),
57
+ self ::createValueObject (id: 160 , weight: 1 ),
58
+ self ::createValueObject (id: 1600 , weight: 3 ),
59
+ self ::createValueObject (id: 2 , weight: 2 ),
60
+ self ::createValueObject (id: 150 , weight: 1 ),
61
+ self ::createValueObject (id: 1500 , weight: 3 ),
62
+ self ::createValueObject (id: 3 , weight: 2 ),
63
+ ];
43
64
44
- $ expected = [] ;
65
+ $ sortCallback = static fn ( object $ a , object $ b ): int => $ a -> weight <=> $ b -> weight ;
45
66
46
- foreach ($ iterator as $ value ) {
47
- $ expected [] = $ value ;
48
- }
67
+ $ actual = array_reduce (
68
+ iterator_to_array (new SortIterableAggregate ($ input , $ sortCallback )),
69
+ static fn (array $ carry , object $ item ): array => [...$ carry , $ item ->id ],
70
+ []
71
+ );
49
72
50
- self ::assertSame ($ expected , range ('a ' , 'c ' ));
73
+ usort ($ input , $ sortCallback );
74
+ $ expected = array_map (static fn (object $ item ): int => $ item ->id , $ input );
75
+
76
+ self ::assertEquals ($ expected , $ actual );
51
77
}
52
78
53
79
public function testStableSort (): void
54
80
{
55
- $ valueObjectFactory = static fn (int $ id , int $ weight ) => new class ($ id , $ weight ) {
56
- public function __construct (
57
- public readonly int $ id ,
58
- public readonly int $ weight ,
59
- ) {}
60
- };
61
-
62
81
$ input = [
63
- $ valueObjectFactory (id: 1 , weight: 1 ),
64
- $ valueObjectFactory (id: 2 , weight: 1 ),
65
- $ valueObjectFactory (id: 3 , weight: 1 ),
82
+ self :: createValueObject (id: 1 , weight: 1 ),
83
+ self :: createValueObject (id: 2 , weight: 1 ),
84
+ self :: createValueObject (id: 3 , weight: 1 ),
66
85
];
67
86
68
87
$ sort = new SortIterableAggregate ($ input , static fn (object $ a , object $ b ): int => $ a ->weight <=> $ b ->weight );
69
88
70
- $ expected = [ 1 , 2 , 3 ] ;
89
+ $ expected = range ( 1 , 3 ) ;
71
90
72
91
self ::assertSame ($ expected , iterator_to_array (new MapIterableAggregate ($ sort , static fn (object $ value ): int => $ value ->id )));
73
92
}
93
+
94
+ private static function createValueObject (int $ id , int $ weight ): object
95
+ {
96
+ return new class ($ id , $ weight ) {
97
+ public function __construct (
98
+ public int $ id ,
99
+ public int $ weight ,
100
+ ) {}
101
+ };
102
+ }
74
103
}
0 commit comments