8
8
use Illuminate \Support \Collection ;
9
9
use PHPUnit \Framework \ExpectationFailedException ;
10
10
use Soyhuce \Testing \Concerns \LaravelAssertions ;
11
+ use Soyhuce \Testing \Tests \Fixtures \SimpleData ;
11
12
use Soyhuce \Testing \Tests \TestCase ;
12
13
13
14
/**
@@ -42,6 +43,56 @@ public static function sameCollection(): array
42
43
];
43
44
}
44
45
46
+ public static function sameUnorderedCollection (): array
47
+ {
48
+ Model::unguard ();
49
+
50
+ return [
51
+ [new Collection ([1 , 2 , 3 ]), new Collection ([2 , 1 , 3 ])],
52
+ [[1 , 2 , 3 ], new Collection ([1 , 3 , 2 ])],
53
+ [new Collection ([1 => 1 , 0 => 2 , 3 => 3 ]), new Collection ([0 => 2 , 1 => 1 , 3 => 3 ])],
54
+ [[1 => 1 , 0 => 2 , 3 => 3 ], new Collection ([1 => 1 , 3 => 3 , 0 => 2 ])],
55
+ [new Collection (['a ' => 1 , 'b ' => 2 , 'c ' => 3 ]), new Collection (['a ' => 1 , 'c ' => 3 , 'b ' => 2 ])],
56
+ [['c ' => 3 , 'a ' => 1 , 'b ' => 2 ], new Collection (['a ' => 1 , 'b ' => 2 , 'c ' => 3 ])],
57
+ [
58
+ new Collection ([new User (['id ' => 1 , 'name ' => 'John ' ]), new User (['id ' => 2 , 'name ' => 'Jack ' ])]),
59
+ new Collection ([new User (['id ' => 2 , 'name ' => 'Jack ' ]), new User (['id ' => 1 , 'name ' => 'Peter ' ])]),
60
+ ],
61
+ [
62
+ new Collection ([
63
+ 'John ' => new User (['id ' => 1 , 'name ' => 'John ' ]),
64
+ 'Jack ' => new User (['id ' => 2 , 'name ' => 'Jack ' ]),
65
+ ]),
66
+ new Collection ([
67
+ 'Jack ' => new User (['id ' => 2 , 'name ' => 'Jack ' ]),
68
+ 'John ' => new User (['id ' => 1 , 'name ' => 'John ' ]),
69
+ ]),
70
+ ],
71
+ [
72
+ new Collection ([
73
+ CarbonImmutable::create (2022 , 2 , 11 ),
74
+ CarbonImmutable::create (2022 , 5 , 11 ),
75
+ ]),
76
+ new Collection ([
77
+ CarbonImmutable::createFromFormat ('!Y-m-d ' , '2022-05-11 ' ),
78
+ CarbonImmutable::createFromFormat ('!Y-m-d ' , '2022-02-11 ' ),
79
+ ]),
80
+ ],
81
+ [
82
+ new Collection ([new Collection ([1 , 2 , 3 ]), new Collection ([2 , 3 , 1 ])]),
83
+ new Collection ([new Collection ([2 , 3 , 1 ]), new Collection ([1 , 2 , 3 ])]),
84
+ ],
85
+ [
86
+ new Collection ([[1 , 2 , 3 ], [2 , 1 , 3 ]]),
87
+ new Collection ([[2 , 1 , 3 ], [1 , 2 , 3 ]]),
88
+ ],
89
+ [
90
+ [new SimpleData ('John ' , 25 ), new SimpleData ('Jack ' , 30 )],
91
+ new Collection ([new SimpleData ('Jack ' , 30 ), new SimpleData ('John ' , 25 )]),
92
+ ],
93
+ ];
94
+ }
95
+
45
96
/**
46
97
* @test
47
98
* @dataProvider sameCollection
@@ -51,6 +102,15 @@ public function collectionsAreEqual(mixed $first, mixed $second): void
51
102
$ this ->assertCollectionEquals ($ first , $ second );
52
103
}
53
104
105
+ /**
106
+ * @test
107
+ * @dataProvider sameUnorderedCollection
108
+ */
109
+ public function collectionsAreEqualCanonicalizing (mixed $ first , mixed $ second ): void
110
+ {
111
+ $ this ->assertCollectionEqualsCanonicalizing ($ first , $ second );
112
+ }
113
+
54
114
public static function differentCollections (): array
55
115
{
56
116
Model::unguard ();
@@ -78,6 +138,52 @@ public static function differentCollections(): array
78
138
];
79
139
}
80
140
141
+ public static function differentUnorderedCollections (): array
142
+ {
143
+ Model::unguard ();
144
+
145
+ return [
146
+ [new Collection ([1 , 2 ]), new Collection ([1 , 2 , 3 ])],
147
+ [new Collection ([1 , 2 , 3 ]), new Collection ([1 , 2 ])],
148
+ [new Collection ([1 , 2 , 3 ]), new Collection ([3 , 1 , 4 ])],
149
+ [new Collection ([1 , 2 , 3 ]), new Collection ([3 , 1 , 5 ])],
150
+ [new Collection ([1 , 2 , 3 ]), new Collection ([1 , 2 , '3 ' ])],
151
+ [new Collection ([1 => 1 , 0 => 2 , 3 => 3 ]), new Collection ([0 => 2 , 1 => 1 , 3 => 6 ])],
152
+ [[1 => 1 , 0 => 2 , 3 => 3 ], new Collection ([1 => 1 , 4 => 3 , 0 => 2 ])],
153
+ [new Collection (['a ' => 1 , 'b ' => 2 , 'c ' => 3 ]), new Collection (['a ' => 1 , 'b ' => 2 ])],
154
+ [new Collection (['a ' => 1 , 'b ' => 2 , 'c ' => 3 ]), new Collection (['a ' => 1 , 'b ' => 2 , 'c ' => 4 ])],
155
+ [new Collection (['a ' => 1 , 'b ' => 2 , 'c ' => 3 ]), new Collection (['a ' => 1 , 'b ' => 2 , 'd ' => 3 ])],
156
+ [new Collection (['a ' => 1 , 'b ' => 2 , 'c ' => 3 ]), new Collection (['a ' => 1 , 'c ' => 6 , 'b ' => 2 ])],
157
+ [new Collection (['a ' => 1 , 'b ' => 2 , 'c ' => 3 ]), new Collection (['a ' => 1 , 'b ' => 2 , 3 ])],
158
+ [
159
+ new Collection ([new User (['id ' => 1 , 'name ' => 'John ' ]), new User (['id ' => 2 , 'name ' => 'Jack ' ])]),
160
+ new Collection ([new User (['id ' => 2 , 'name ' => 'Jack ' ]), new User (['id ' => 5 , 'name ' => 'Peter ' ])]),
161
+ ],
162
+ [
163
+ new Collection ([
164
+ 'id1 ' => new User (['id ' => 1 , 'name ' => 'John ' ]),
165
+ 'id2 ' => new User (['id ' => 2 , 'name ' => 'Jack ' ]),
166
+ ]),
167
+ new Collection ([
168
+ 'id2 ' => new User (['id ' => 4 , 'name ' => 'Jack ' ]),
169
+ 'id1 ' => new User (['id ' => 1 , 'name ' => 'John ' ]),
170
+ ]),
171
+ ],
172
+ [
173
+ new Collection ([new Collection ([1 , 2 , 3 ])]),
174
+ new Collection ([new Collection ([1 , '2 ' , 3 ])]),
175
+ ],
176
+ [
177
+ new Collection ([[1 , 2 , 3 ]]),
178
+ new Collection ([[1 , '2 ' , 3 ]]),
179
+ ],
180
+ [
181
+ [new SimpleData ('John ' , 25 ), new SimpleData ('Jack ' , 30 )],
182
+ new Collection ([new SimpleData ('Jack ' , 30 ), new SimpleData ('Jim ' , 35 )]),
183
+ ]
184
+ ];
185
+ }
186
+
81
187
/**
82
188
* @test
83
189
* @dataProvider differentCollections
@@ -88,4 +194,15 @@ public function collectionsAreDifferent(mixed $first, mixed $second): void
88
194
89
195
$ this ->assertCollectionEquals ($ first , $ second );
90
196
}
197
+
198
+ /**
199
+ * @test
200
+ * @dataProvider differentUnorderedCollections
201
+ */
202
+ public function collectionsAreDifferentCanonicalizing (mixed $ first , mixed $ second ): void
203
+ {
204
+ $ this ->expectException (ExpectationFailedException::class);
205
+
206
+ $ this ->assertCollectionEqualsCanonicalizing ($ first , $ second );
207
+ }
91
208
}
0 commit comments