Skip to content

Commit

Permalink
add relation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abbasudo committed Oct 13, 2024
1 parent 687973c commit 650a031
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
3 changes: 3 additions & 0 deletions tests/App/Migrations/0001_01_01_000000_create_test_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Abbasudo\Purity\Tests\App\Models\Author;
use Abbasudo\Purity\Tests\App\Models\Post;
use Abbasudo\Purity\Tests\App\Models\Product;
use Abbasudo\Purity\Tests\App\Models\Tag;
use Abbasudo\Purity\Tests\App\Models\User;
use Illuminate\Database\Migrations\Migration;
Expand Down Expand Up @@ -31,6 +32,7 @@ public function up(): void
$table->id();
$table->foreignIdFor(User::class)->nullable();
$table->string('title')->nullable();
$table->nullableMorphs('postable');
$table->timestamps();
});

Expand Down Expand Up @@ -60,6 +62,7 @@ public function up(): void
$table->foreignIdFor(Author::class)->nullable();
$table->string('name');
$table->string('description');
$table->foreignIdFor(Product::class)->nullable();
$table->timestamps();
});

Expand Down
6 changes: 6 additions & 0 deletions tests/App/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Abbasudo\Purity\Traits\Sortable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Book extends Model
{
Expand All @@ -26,4 +27,9 @@ public function author(): BelongsTo
{
return $this->belongsTo(Author::class);
}

public function posts(): MorphMany
{
return $this->morphMany(Post::class, 'postable');
}
}
7 changes: 7 additions & 0 deletions tests/App/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Post extends Model
{
Expand All @@ -18,6 +19,7 @@ class Post extends Model

protected $fillable = [
'title',
'user_id'
];

public function comments(): HasMany
Expand All @@ -34,4 +36,9 @@ public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class);
}

public function postable(): MorphTo
{
return $this->morphTo();
}
}
12 changes: 12 additions & 0 deletions tests/App/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Abbasudo\Purity\Traits\Sortable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Product extends Model
{
Expand All @@ -26,4 +28,14 @@ protected static function newFactory()
'description',
'is_available',
];

public function posts(): MorphMany
{
return $this->morphMany(Post::class, 'postable');
}

public function book(): HasOne
{
return $this->hasOne(Book::class);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/FilterableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public function it_can_filter_with_and_operator(): void
]);

$response = $this->getJson(
'/posts?filters[$and][0][name][$eq]=laravel purity is the best&filters[$and][1][description][$eq]=laravel purity is the best'
'/products?filters[$and][0][name][$eq]=laravel purity&filters[$and][1][description][$eq]=laravel purity is the best'
)
->assertOk()
->assertJsonCount(1);
Expand Down
98 changes: 98 additions & 0 deletions tests/Feature/RelationFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

use Abbasudo\Purity\Tests\App\Models\Book;
use Abbasudo\Purity\Tests\App\Models\Post;
use Abbasudo\Purity\Tests\App\Models\Product;
use Abbasudo\Purity\Tests\App\Models\User;
use Abbasudo\Purity\Tests\TestCase;
use Illuminate\Support\Facades\Route;

class RelationFilterTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

Route::get('/posts', function () {
return Post::filter()->get();
});

Route::get('/products', function () {
return Product::filter()->get();
});

Post::create([
'title' => 'laravel purity is the best',
]);
}

/** @test */
public function it_can_filter_by_has_many_relation(): void
{
$post = Post::first();

$post->comments()->create([
'content' => 'first comment',
]);

$post->comments()->create([
'content' => 'second comment',
]);

$response = $this->getJson('/posts?filters[comments][content][$eq]=first comment');

$response->assertOk();
$response->assertJsonCount(1);
}

/** @test */
public function it_can_filter_by_belongs_to_relation(): void
{
$user = User::create([
'name' => 'Test',
]);

$post = Post::create([
'title' => 'laravel purity is the best',
'user_id' => $user->id,
]);

$response = $this->getJson('/posts?filters[user][name][$eq]=Test');

$response->assertOk();
$response->assertJsonCount(1);
}

/** @test */
public function it_can_filter_by_belongs_to_many_relation(): void
{
$post = Post::first();

$post->tags()->create([
'name' => 'Laravel',
]);

$response = $this->getJson('/posts?filters[tags][name][$eq]=Laravel');

$response->assertOk();
$response->assertJsonCount(1);
}

/** @test */
public function it_can_filter_by_has_one_relation(): void
{
$product = Product::factory([
'name' => 'Laravel Purity',
])->create();

$product->book()->create([
'name' => 'book',
'description' => 'book for product'
]);

$response = $this->getJson('/products?filters[book][name][$eq]=book');

$response->assertOk();
$response->assertJsonCount(1);
}
}

0 comments on commit 650a031

Please sign in to comment.