mirror of https://github.com/kcal-app/kcal.git
Update IngredientPicker tests
This commit is contained in:
parent
beb69b4a0e
commit
a1b91c7330
|
@ -77,6 +77,6 @@ class IngredientPickerController extends Controller
|
||||||
->orWhere('recipes.description', 'like', "%{$term}%")
|
->orWhere('recipes.description', 'like', "%{$term}%")
|
||||||
->orWhere('recipes.source', 'like', "%{$term}%")
|
->orWhere('recipes.source', 'like', "%{$term}%")
|
||||||
->get();
|
->get();
|
||||||
return $foods->merge($recipes);
|
return new Collection([...$foods, ...$recipes]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,30 +3,72 @@
|
||||||
namespace Tests\Feature\Http\Controllers;
|
namespace Tests\Feature\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Controllers\IngredientPickerController;
|
use App\Http\Controllers\IngredientPickerController;
|
||||||
|
use App\Models\Food;
|
||||||
|
use App\Models\Recipe;
|
||||||
|
use GuzzleHttp\Exception\ConnectException;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
use Tests\LoggedInTestCase;
|
use Tests\LoggedInTestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class IngredientPickerControllerTest
|
* @todo Improve testing of Algolia and ElasticSearch drivers.
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*/
|
*/
|
||||||
class IngredientPickerControllerTest extends LoggedInTestCase
|
class IngredientPickerControllerTest extends LoggedInTestCase
|
||||||
{
|
{
|
||||||
use RefreshDatabase;
|
use RefreshDatabase;
|
||||||
|
|
||||||
private function buildUrl(array $parameters = []): string {
|
private function buildUrl(array $parameters = []): string
|
||||||
|
{
|
||||||
return action([IngredientPickerController::class, 'search'], $parameters);
|
return action([IngredientPickerController::class, 'search'], $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSearchUrlLoads(): void {
|
public function testSearchUrlLoads(): void
|
||||||
|
{
|
||||||
$response = $this->get($this->buildUrl());
|
$response = $this->get($this->buildUrl());
|
||||||
$response->assertOk();
|
$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([]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue