}); }); describe("->to()", function () { it("converts a collection to an array", function () { $collection = new Collection([1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5]); expect($collection->to('array', ['key' => false]))->toBe([1, 2, 3, 4, 5]); }); it("converts a collection to an array by preserving keys", function () { $collection = new Collection([1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5]); $result = $collection->to('array'); expect($result)->toBe([1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5]); }); it("converts according a registered closure", function () { $collection = new Collection([1, 2, 3]); Collection::formats('json', function ($collection) { return json_encode($collection->to('array')); }); expect($collection->to('json'))->toBe("[1,2,3]"); }); it("converts according a closure", function () { $collection = new Collection(['hello', 'world', '!']); $result = $collection->to(function ($collection) { return join(' ', $collection->to('array')); }); expect($result)->toBe('hello world !'); }); it("converts objects which support __toString", function () { $stringable = Double::classname(); allow($stringable)->toReceive('__toString')->andReturn('hello'); $collection = new Collection([new $stringable()]); expect($collection->to('array'))->toBe(['hello']);