嘲笑雄辩的收藏

Jim*_*988 3 php unit-testing laravel eloquent

我想疯狂地解释原因无意间创建一个模拟的Eloquent Collection

我已经试过了:

        $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
Run Code Online (Sandbox Code Playgroud)

但事实证明,此集合没有->get()像您通常那样使用的方法,例如:

User::take( 2 )->get();
Run Code Online (Sandbox Code Playgroud)

最终是因为\ Illuminate \ Database \ Eloquent \ Collection只是一个命名空间并使用了\ Illuminate \ Database \ Collection

任何想法如何模拟一个能正常工作的口才典范?

Jim*_*988 5

我发现您可以采用您要创建的模型类型并对其进行调用newCollection

$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 ) )
    )
);
Run Code Online (Sandbox Code Playgroud)

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