*/ public static function badQueryProvider(): array { return [ 'categories bad direction' => ['/api/blog/categories?sort_direction=foo'], 'categories unknown column' => ['/api/blog/categories?sort_by=no_such_column'], 'categories oversized page' => ['/api/blog/categories?per_page=100000'], 'categories zero page size' => ['/api/blog/categories?per_page=0'], 'posts unknown sort column' => ['/api/blog/posts?sort_field=password'], 'posts oversized page' => ['/api/blog/posts?per_page=9999'], 'tags bad direction' => ['/api/blog/tags?sort_direction=sideways'], ]; } #[DataProvider('badQueryProvider')] public function test_invalid_list_parameters_are_rejected(string $url): void { $this->getJson($url)->assertStatus(422); } /** * @return array */ public static function goodQueryProvider(): array { return [ 'categories default' => ['/api/blog/categories'], 'categories max page size' => ['/api/blog/categories?per_page=50'], 'categories valid sort' => ['/api/blog/categories?sort_by=name&sort_direction=asc'], 'categories filter' => ['/api/blog/categories?filter=web'], 'posts default' => ['/api/blog/posts'], 'posts valid sort' => ['/api/blog/posts?sort_field=views&sort_direction=asc'], 'tags default' => ['/api/blog/tags'], ]; } #[DataProvider('goodQueryProvider')] public function test_valid_list_parameters_are_accepted(string $url): void { $this->getJson($url)->assertOk(); } public function test_page_size_is_capped(): void { Category::factory()->count(60)->create(); $response = $this->getJson('/api/blog/categories?per_page=50'); $response->assertOk() ->assertJsonPath('meta.per_page', 50) ->assertJsonCount(50, 'data'); } public function test_defaults_are_unchanged_for_existing_consumers(): void { $response = $this->getJson('/api/blog/categories'); $response->assertOk()->assertJsonPath('meta.per_page', 10); } }