Skip to content

Commit

Permalink
Add tests for sub relation
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Apr 26, 2024
1 parent e1bc8da commit 18c0063
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->foreignId('post_id')->constrained()->cascadeOnDelete();
$table->string('body');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
15 changes: 15 additions & 0 deletions tests/Database/Models/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Datalogix\BuilderMacros\Tests\Database\Models;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
protected $guarded = [];

public function post()
{
return $this->belongsTo(Post::class);
}
}
5 changes: 5 additions & 0 deletions tests/Database/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public function user()
{
return $this->belongsTo(User::class);
}

public function comments()
{
return $this->hasMany(Comment::class);
}
}
8 changes: 8 additions & 0 deletions tests/Macros/WhereLikeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function testQueryWithRelation()
$this->assertEquals($expected, $actual);
}

public function testQueryWithSubRelation()
{
$expected = 'select * from "users" where ("users"."name" LIKE ? or "users"."email" LIKE ? or exists (select * from "posts" where "users"."id" = "posts"."user_id" and exists (select * from "comments" where "posts"."id" = "comments"."post_id" and "body" LIKE ?)))';
$actual = User::whereLike(['name', 'email', 'posts.comments.body'], 'foo')->toSql();

$this->assertEquals($expected, $actual);
}

public function testQueryWithColumnKey()
{
$expected = 'select * from "users" where ("users"."name_id" = ?)';
Expand Down

0 comments on commit 18c0063

Please sign in to comment.