Skip to content

Dependency on Request class in Searchable::process() makes unit testing difficult #22

@toh-karashi

Description

@toh-karashi

public function process($builder, $query = null)
{
if (is_null($query)) {
$query = Request::query();
}

I suggest changing it to like this:

class Searchable {

    protected $params = [];
    protected $request;

    public function __construct(Request $request = null)
    {
        $this->request = $request ?? Request::instance();
    }

    public function process($builder, $query = null)
    {
        if (is_null($query)) {
            $query = $this->request->query();
        }

...

You can probably use it as before,

$users = User::query()
   ...
   ->where(...)
   ->search(new UserSearch())
   ->get();

or you can pass an instance of the Request class.

$users = User::query()
   ...
   ->where(...)
   ->search(new UserSearch($request))
   ->get();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions