Update IngredientPicker tests

This commit is contained in:
Christopher C. Wells 2021-04-06 12:07:12 -07:00
parent beb69b4a0e
commit a1b91c7330
2 changed files with 53 additions and 11 deletions

View File

@ -77,6 +77,6 @@ class IngredientPickerController extends Controller
->orWhere('recipes.description', 'like', "%{$term}%")
->orWhere('recipes.source', 'like', "%{$term}%")
->get();
return $foods->merge($recipes);
return new Collection([...$foods, ...$recipes]);
}
}

View File

@ -3,30 +3,72 @@
namespace Tests\Feature\Http\Controllers;
use App\Http\Controllers\IngredientPickerController;
use App\Models\Food;
use App\Models\Recipe;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Tests\LoggedInTestCase;
/**
* Class IngredientPickerControllerTest
*
* Most of the functionality of this test is has been removed because it is too
* unstable, particularly for parallel testing.
*
* @todo Find a stable way to test Elasticsearch querying.
*
* @package Tests\Feature\Http\Controllers
* @todo Improve testing of Algolia and ElasticSearch drivers.
*/
class IngredientPickerControllerTest extends LoggedInTestCase
{
use RefreshDatabase;
private function buildUrl(array $parameters = []): string {
private function buildUrl(array $parameters = []): string
{
return action([IngredientPickerController::class, 'search'], $parameters);
}
public function testSearchUrlLoads(): void {
public function testSearchUrlLoads(): void
{
$response = $this->get($this->buildUrl());
$response->assertOk();
}
public function testCanSearchWithDatabase(): void
{
Config::set('scout.driver', 'null');
$food = ['name' => 'Butter'];
Food::factory()->createOne($food);
$recipe = ['name' => 'Buttered Toast'];
Recipe::factory()->createOne($recipe);
$response = $this->get($this->buildUrl(['term' => 'butter']));
$response->assertOk();
$response->assertJsonCount(2);
$response->assertJsonFragment($food);
$response->assertJsonFragment($recipe);
}
/**
* Essentially only confirms that the Algolia search method is used.
*/
public function testCanSearchWithAlgolia(): void
{
$this->expectException(ConnectException::class);
$this->expectExceptionMessageMatches("/Could not resolve host: \-dsn\.algolia\.net/");
Config::set('scout.driver', 'algolia');
$response = $this->get($this->buildUrl(['term' => 'butter']));
$response->assertStatus(500);
if ($response->exception) {
throw $response->exception;
}
}
/**
* Essentially only confirms that the ElasticSearch search method is used.
*/
public function testCanSearchWithElasticSearch(): void
{
Config::set('scout.driver', 'elastic');
$response = $this->get($this->buildUrl(['term' => 'butter']));
$response->assertOk();
$response->assertJson([]);
}
}