mirror of https://github.com/kcal-app/kcal.git
Test custom filters for JSON API endpoints
This commit is contained in:
parent
852cce67e8
commit
9d8a851066
|
@ -26,4 +26,29 @@ class FoodApiTest extends JsonApiTestCase
|
|||
return 'foods';
|
||||
}
|
||||
|
||||
public function testSearchFilter(): void {
|
||||
$attributes = [
|
||||
'name' => 'Popcorn',
|
||||
'detail' => 'buttered',
|
||||
'brand' => 'Orville Redenbacher',
|
||||
];
|
||||
$this->factory()->create($attributes);
|
||||
$this->factory()->create([
|
||||
'name' => 'tomatoes',
|
||||
'detail' => 'canned',
|
||||
'brand' => 'Kroger',
|
||||
]);
|
||||
|
||||
foreach ($attributes as $attribute => $value) {
|
||||
$partial = substr($value, rand(0, 3), 3);
|
||||
$search_route = route($this->indexRouteName, [
|
||||
'filter' => ['search' => $partial]
|
||||
]);
|
||||
$response = $this->get($search_route);
|
||||
$response->assertOk();
|
||||
$response->assertJsonCount(1, 'data');
|
||||
$response->assertJsonFragment([$attribute => $value]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,9 +10,9 @@ abstract class JsonApiTestCase extends LoggedInTestCase
|
|||
{
|
||||
|
||||
/**
|
||||
* Route prefix for queries.
|
||||
* API index route name.
|
||||
*/
|
||||
private string $routePrefix;
|
||||
protected string $indexRouteName;
|
||||
|
||||
/**
|
||||
* Get the factory of the model to be tested.
|
||||
|
@ -27,7 +27,8 @@ abstract class JsonApiTestCase extends LoggedInTestCase
|
|||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->routePrefix = config('json-api-v1.url.name');
|
||||
$route_prefix = config('json-api-v1.url.name');
|
||||
$this->indexRouteName = "{$route_prefix}{$this->resourceName()}.index";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,7 +41,7 @@ abstract class JsonApiTestCase extends LoggedInTestCase
|
|||
public function testCanGetIndex(): void
|
||||
{
|
||||
$this->createInstances(10);
|
||||
$index_url = route("{$this->routePrefix}{$this->resourceName()}.index");
|
||||
$index_url = route($this->indexRouteName);
|
||||
$response = $this->get($index_url);
|
||||
$response->assertOk();
|
||||
$response->assertJson(['data' => true]);
|
||||
|
|
|
@ -26,4 +26,29 @@ class RecipeApiTest extends JsonApiTestCase
|
|||
return 'recipes';
|
||||
}
|
||||
|
||||
public function testSearchFilter(): void {
|
||||
$attributes = [
|
||||
'name' => 'Chocolate Chip Cookies',
|
||||
'description' => 'Buttery, delicious cookies.',
|
||||
'source' => "America's Test Kitchen",
|
||||
];
|
||||
$this->factory()->create($attributes);
|
||||
$this->factory()->create([
|
||||
'name' => 'Eggplant Parmesan',
|
||||
'description' => 'Veggies and cheese!',
|
||||
'source' => 'Joy of Baking',
|
||||
]);
|
||||
|
||||
foreach ($attributes as $attribute => $value) {
|
||||
$partial = substr($value, rand(0, 5), 5);
|
||||
$search_route = route($this->indexRouteName, [
|
||||
'filter' => ['search' => $partial]
|
||||
]);
|
||||
$response = $this->get($search_route);
|
||||
$response->assertOk();
|
||||
$response->assertJsonCount(1, 'data');
|
||||
$response->assertJsonFragment([$attribute => $value]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,4 +26,22 @@ class TagApiTest extends JsonApiTestCase
|
|||
return 'tags';
|
||||
}
|
||||
|
||||
public function testNameFilter(): void {
|
||||
$names = ['sweet', 'salty', 'spicy'];
|
||||
foreach ($names as $name) {
|
||||
$this->factory()->create(['name' => $name]);
|
||||
}
|
||||
|
||||
foreach ($names as $name) {
|
||||
$partial = substr($name, rand(0, 2), 3);
|
||||
$search_route = route($this->indexRouteName, [
|
||||
'filter' => ['name' => $partial]
|
||||
]);
|
||||
$response = $this->get($search_route);
|
||||
$response->assertOk();
|
||||
$response->assertJsonCount(1, 'data');
|
||||
$response->assertJsonFragment(['name' => $name]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue