diff --git a/tests/App/Migrations/0001_01_01_000000_create_test_tables.php b/tests/App/Migrations/0001_01_01_000000_create_test_tables.php index 1beb45b..4e3c7f9 100644 --- a/tests/App/Migrations/0001_01_01_000000_create_test_tables.php +++ b/tests/App/Migrations/0001_01_01_000000_create_test_tables.php @@ -62,6 +62,7 @@ public function up(): void $table->foreignIdFor(Author::class)->nullable(); $table->string('name'); $table->string('description'); + $table->string('page_count'); $table->foreignIdFor(Product::class)->nullable(); $table->timestamps(); }); diff --git a/tests/App/Models/Book.php b/tests/App/Models/Book.php index 5f1b675..55b71fb 100644 --- a/tests/App/Models/Book.php +++ b/tests/App/Models/Book.php @@ -16,11 +16,13 @@ class Book extends Model protected $filterFields = [ 'name', // field 'description', // field + 'page_count', // field ]; protected $fillable = [ 'name', 'description', + 'page_count', ]; public function author(): BelongsTo diff --git a/tests/Feature/FilterableByMultipleFieldInRelationTest.php b/tests/Feature/FilterableByMultipleFieldInRelationTest.php index bd4e300..a5e51de 100644 --- a/tests/Feature/FilterableByMultipleFieldInRelationTest.php +++ b/tests/Feature/FilterableByMultipleFieldInRelationTest.php @@ -22,30 +22,37 @@ public function setUp(): void $author->books()->create([ 'name' => 'A Game of Thrones', 'description' => 'A Game of Thrones is the first novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.', + 'page_count' => 100, ]); $author->books()->create([ 'name' => 'A Clash of Kings', 'description' => 'A Clash of Kings is the second novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.', + 'page_count' => 200, ]); $author->books()->create([ 'name' => 'A Storm of Swords', 'description' => 'A Storm of Swords is the third novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.', + 'page_count' => 300, ]); $author->books()->create([ 'name' => 'A Feast for Crows', 'description' => 'A Feast for Crows is the fourth novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.', + 'page_count' => 400, ]); $author->books()->create([ 'name' => 'A Dance with Dragons', 'description' => 'A Dance with Dragons is the fifth novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.', + 'page_count' => 500, ]); $author->books()->create([ 'name' => 'The Winds of Winter', 'description' => 'The Winds of Winter is the planned sixth novel in the epic fantasy series A Song of Ice and Fire by American writer George R. R. Martin.', + 'page_count' => 600, ]); $author->books()->create([ 'name' => 'A Dream of Spring', 'description' => 'A Dream of Spring is the planned seventh novel in the epic fantasy series A Song of Ice and Fire by American writer George R. R. Martin.', + 'page_count' => 700, ]); // author @@ -57,14 +64,17 @@ public function setUp(): void $author->books()->create([ 'name' => 'The Hobbit', 'description' => 'The Hobbit, or There and Back Again is a children\'s fantasy novel by English author J. R. R. Tolkien.', + 'page_count' => 100, ]); $author->books()->create([ 'name' => 'The Lord of the Rings', 'description' => 'The Lord of the Rings is an epic high-fantasy novel by the English author and scholar J. R. R. Tolkien.', + 'page_count' => 200, ]); $author->books()->create([ 'name' => 'The Silmarillion', 'description' => 'The Silmarillion is a collection of mythopoeic works by English writer J. R. R. Tolkien.', + 'page_count' => 300, ]); } diff --git a/tests/Feature/RelationFilterTest.php b/tests/Feature/RelationFilterTest.php index 2ee34d7..ec5248a 100644 --- a/tests/Feature/RelationFilterTest.php +++ b/tests/Feature/RelationFilterTest.php @@ -87,6 +87,7 @@ public function it_can_filter_by_has_one_relation(): void $product->book()->create([ 'name' => 'book', 'description' => 'book for product', + 'page_count' => 100, ]); $response = $this->getJson('/products?filters[book][name][$eq]=book'); @@ -94,4 +95,42 @@ public function it_can_filter_by_has_one_relation(): void $response->assertOk(); $response->assertJsonCount(1); } + + /** @test */ + public function it_can_filter_by_multiple_fields_on_one_relationship(): void + { + $product1 = Product::factory([ + 'name' => 'Laravel Purity', + ])->create(); + + $product2 = Product::factory([ + 'name' => 'Laravel Purity', + ])->create(); + + //exact match, should return + $product1->book()->create([ + 'name' => 'book', + 'description' => 'book for product', + 'page_count' => 100, + ]); + + //only matching name should not return + $product2->book()->create([ + 'name' => 'book', + 'description' => 'book for product', + 'page_count' => 200, + ]); + + //only matching page_count should not return + $product2->book()->create([ + 'name' => 'book2', + 'description' => 'book2 for product2', + 'page_count' => 100, + ]); + + $response = $this->getJson('/products?filters[book][name][$eq]=book&filters[book][page_count][$eq]=100'); + + $response->assertOk(); + $response->assertJsonCount(1); + } } diff --git a/tests/Feature/RenameFilterableFieldsTest.php b/tests/Feature/RenameFilterableFieldsTest.php index 93c623c..4c82e24 100644 --- a/tests/Feature/RenameFilterableFieldsTest.php +++ b/tests/Feature/RenameFilterableFieldsTest.php @@ -99,9 +99,11 @@ public function available_filter_fields_work_with_renamed_filter_fields(): void $book->create([ 'name' => 'name_1', 'description' => 'description_1', + 'page_count' => 100, ])->create([ 'name' => 'name_1', 'description' => 'description_2', + 'page_count' => 200, ]); Route::get('/books', function () use ($book) {