diff --git a/tests/Feature/JsonApi/FoodApiTest.php b/tests/Feature/JsonApi/FoodApiTest.php index 17a639a..541a2e6 100644 --- a/tests/Feature/JsonApi/FoodApiTest.php +++ b/tests/Feature/JsonApi/FoodApiTest.php @@ -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]); + } + } + } diff --git a/tests/Feature/JsonApi/JsonApiTestCase.php b/tests/Feature/JsonApi/JsonApiTestCase.php index 32413d6..1512e0a 100644 --- a/tests/Feature/JsonApi/JsonApiTestCase.php +++ b/tests/Feature/JsonApi/JsonApiTestCase.php @@ -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]); diff --git a/tests/Feature/JsonApi/RecipeApiTest.php b/tests/Feature/JsonApi/RecipeApiTest.php index 9750062..c559159 100644 --- a/tests/Feature/JsonApi/RecipeApiTest.php +++ b/tests/Feature/JsonApi/RecipeApiTest.php @@ -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]); + } + } + } diff --git a/tests/Feature/JsonApi/TagApiTest.php b/tests/Feature/JsonApi/TagApiTest.php index c37120b..80590af 100644 --- a/tests/Feature/JsonApi/TagApiTest.php +++ b/tests/Feature/JsonApi/TagApiTest.php @@ -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]); + } + } + }