0

I have a piece of code like this:

 public function index(Request $request, Runner $runnerParam)
{
    $name = $request->input('name');
    $fromDate = $request->input('from_date');
    $toDate = $request->input('to_date');

    $runners = Runner::query();

    if ($name) {
        $runners =  $runnerParam::search($name);
    }

    if ($fromDate && $toDate) {
       $runners->where('created_at', '<=',$toDate )
       ->where('created_at', '>=', $fromDate);
    }

    switch ($type) {
        case 1:
            $runners->where('role', '=', runner::PRO);
            break;
        case 2:
            $runners->where('role', '=', runner::AMATEUR);
            break;          
    }

    $runners = $runners->get();

    foreach($runners as $runner){
     $runner->distance = $runner->stats->sum('distance');
    }

    return $runners;    
}

The question is, how do I write test for this? If I just try to provide 'name' in test, it will return nothing like search() function isn't working at all while testing. Tried really hard to find anything on this, but the info is scarce and I only ended up with something like 'set Algolia driver to null', which I managed to do, but to no effect since I don't know what's the point of doing so and how do you apply it in tests. There are absolutely no examples of successful tests out there, just a few questions with short answer that didn't quite help.

A piece of test:

public function testNameFilter()
{

    $this->logIn();

    $runners = factory(runner::class, 30)->create();

    $name = $runners[0]->name;


    $response = $this->json('get', route('api::runners.get'), ['name' => $name]);

    $responseContent = $response->getContent();

    ...
}

So, what I get in the end is empty responseContent, which means this is not the right way to test this. Any thoughts?

Gishas
  • 380
  • 6
  • 21

1 Answers1

1

Why not just test that you've properly configured your class to use Laravel Scout, vs. testing that Laravel Scout works as expected?

public function class_uses_scout() 
{
    $this->assertTrue(in_array('Laravel\Scout\Searchable', class_uses('App\FooModel')));
}

public function class_has_searchable_array()
{
    // compare the searchable array with a hardcoded array here
}

Be sure to set your disable Laravel Scout in your test environment.

zkolnik
  • 882
  • 2
  • 7
  • 11