public function testSync() { $collection = new Collection([1, 2, 3, 4]); $callback = function ($item) { return is_int($item) ? $item * 10 : 'foo'; }; $actual = $collection->sync(4, $callback); $this->assertEquals(new Collection([10, 20, 30, 40]), $actual); $this->assertNotSame($collection, $actual); $this->assertEquals(new Collection([10, 20]), $collection->sync(2, $callback)); /** Overflow with no callable */ $extra = new Collection(); $actual = $collection->sync(2, null, function ($item) use($extra) { $extra->push($item); }); $this->assertEquals(new Collection([1, 2]), $actual); $this->assertEquals(new Collection([3, 4]), $extra); /** Callable and overflow */ $extra = new Collection(); $actual = $collection->sync(2, $callback, function ($item) use($extra) { $extra->push($item); }); $this->assertEquals(new Collection([10, 20]), $actual); $this->assertEquals(new Collection([3, 4]), $extra); /** Size is less than current length */ $this->assertEquals(new Collection([10, 20, 30, 40, 'foo', 'foo']), $collection->sync(6, $callback, function () { $this->fail('Overflow callback should not be invoked when there is no overflow.'); })); }