Skip to content

Commit a459b9b

Browse files
authored
[7.x] Add better test coverage for skip, skipUntil and skipWhile methods of Collection (#33548)
* Add better test coverage for `skip`, `skipUntil` and `skipWhile` methods of Collections - Case where the number of items to skip is more than the total length of the collection was missing - Few cases where item is at the beginning and item is not found in the collection were missing for skipUntil and skipWhile * Apply changes from StyleCI
1 parent 1ab0409 commit a459b9b

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

tests/Support/SupportCollectionTest.php

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,11 @@ public function testSkipMethod($collection)
207207
{
208208
$data = new $collection([1, 2, 3, 4, 5, 6]);
209209

210-
$data = $data->skip(4)->values();
210+
// Total items to skip is smaller than collection length
211+
$this->assertSame([5, 6], $data->skip(4)->values()->all());
211212

212-
$this->assertSame([5, 6], $data->all());
213+
// Total items to skip is more than collection length
214+
$this->assertSame([], $data->skip(10)->values()->all());
213215
}
214216

215217
/**
@@ -219,15 +221,35 @@ public function testSkipUntil($collection)
219221
{
220222
$data = new $collection([1, 1, 2, 2, 3, 3, 4, 4]);
221223

222-
$data = $data->skipUntil(3)->values();
224+
// Item at the beginning of the collection
225+
$this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->skipUntil(1)->values()->all());
226+
227+
// Item at the middle of the collection
228+
$this->assertSame([3, 3, 4, 4], $data->skipUntil(3)->values()->all());
229+
230+
// Item not in the collection
231+
$this->assertSame([], $data->skipUntil(5)->values()->all());
232+
233+
// Item at the beginning of the collection
234+
$data = $data->skipUntil(function ($value, $key) {
235+
return $value <= 1;
236+
})->values();
237+
238+
$this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->all());
239+
240+
// Item at the middle of the collection
241+
$data = $data->skipUntil(function ($value, $key) {
242+
return $value >= 3;
243+
})->values();
223244

224245
$this->assertSame([3, 3, 4, 4], $data->all());
225246

247+
// Item not in the collection
226248
$data = $data->skipUntil(function ($value, $key) {
227-
return $value > 3;
249+
return $value >= 5;
228250
})->values();
229251

230-
$this->assertSame([4, 4], $data->all());
252+
$this->assertSame([], $data->all());
231253
}
232254

233255
/**
@@ -237,10 +259,30 @@ public function testSkipWhile($collection)
237259
{
238260
$data = new $collection([1, 1, 2, 2, 3, 3, 4, 4]);
239261

240-
$data = $data->skipWhile(1)->values();
262+
// Item at the beginning of the collection
263+
$this->assertSame([2, 2, 3, 3, 4, 4], $data->skipWhile(1)->values()->all());
264+
265+
// Item not in the collection
266+
$this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->skipWhile(5)->values()->all());
267+
268+
// Item in the collection but not at the beginning
269+
$this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->skipWhile(2)->values()->all());
270+
271+
// Item not in the collection
272+
$data = $data->skipWhile(function ($value, $key) {
273+
return $value >= 5;
274+
})->values();
275+
276+
$this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->all());
277+
278+
// Item in the collection but not at the beginning
279+
$data = $data->skipWhile(function ($value, $key) {
280+
return $value >= 2;
281+
})->values();
241282

242-
$this->assertSame([2, 2, 3, 3, 4, 4], $data->all());
283+
$this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->all());
243284

285+
// Item at the beginning of the collection
244286
$data = $data->skipWhile(function ($value, $key) {
245287
return $value < 3;
246288
})->values();

0 commit comments

Comments
 (0)