Jimmyt1988
Jimmyt1988

Reputation: 21136

mocking an Eloquent collection

I would like, for crazy no point in explaining reasons, to create a mock Eloquent Collection

I have tried this:

        $collection = new \Illuminate\Database\Eloquent\Collection(
            array(
                new User( array( "id" => 1 ) ),
                new User( array( "id" => 2 ) ),
                new User( array( "id" => 3 ) ),
                new User( array( "id" => 4 ) ),
                new User( array( "id" => 5 ) )
            )
        );

        $collection->get(); // Fails

but it turns out that this collection does not have a ->get() method like it normally does when you'd do, something like:

User::take( 2 )->get();

this ends up being because the \Illuminate\Database\Eloquent\Collection is just a namespaced and used \Illuminate\Database\Collection

any ideas how to mock an Eloquent Collection that actually works properly?

Upvotes: 3

Views: 3705

Answers (1)

Jimmyt1988
Jimmyt1988

Reputation: 21136

I found out you can take the type of model you want to create and call newCollection on it.

$collection = new \User();
$collection->newCollection(
    array(
        new User( array( "id" => 1 ) ),
        new User( array( "id" => 2 ) ),
        new User( array( "id" => 3 ) ),
        new User( array( "id" => 4 ) ),
        new User( array( "id" => 5 ) )
    )
);

http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_newCollection

Upvotes: 6

Related Questions