Test custom filters for JSON API endpoints

This commit is contained in:
Christopher C. Wells 2021-04-02 19:49:46 -07:00 committed by Christopher Charbonneau Wells
parent 852cce67e8
commit 9d8a851066
4 changed files with 73 additions and 4 deletions

View File

@ -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]);
}
}
}

View File

@ -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]);

View File

@ -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]);
}
}
}

View File

@ -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]);
}
}
}