This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Target binding: support BelongsToMany relationships (#34)
- Loading branch information
1 parent
2fc22e1
commit a7d9d2f
Showing
12 changed files
with
413 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace ProtoneMedia\LaravelFormComponents\Tests\Feature; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
trait InteractsWithDatabase | ||
{ | ||
protected function setupDatabase() | ||
{ | ||
Model::unguard(); | ||
|
||
$this->app['config']->set('database.default', 'sqlite'); | ||
$this->app['config']->set('database.connections.sqlite', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
|
||
include_once __DIR__ . '/database/create_posts_table.php'; | ||
include_once __DIR__ . '/database/create_comments_table.php'; | ||
include_once __DIR__ . '/database/create_comment_post_table.php'; | ||
include_once __DIR__ . '/database/create_commentables_table.php'; | ||
|
||
(new \CreatePostsTable)->up(); | ||
(new \CreateCommentsTable)->up(); | ||
(new \CreateCommentPostTable)->up(); | ||
(new \CreateCommentablesTable)->up(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
namespace ProtoneMedia\LaravelFormComponents\Tests\Feature; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Route; | ||
use ProtoneMedia\LaravelFormComponents\Tests\TestCase; | ||
|
||
class PostBelongsToMany extends Model | ||
{ | ||
protected $table = 'posts'; | ||
|
||
public function comments() | ||
{ | ||
return $this->belongsToMany(Comment::class, 'comment_post', 'post_id', 'comment_id'); | ||
} | ||
} | ||
|
||
class PostMorphMany extends Model | ||
{ | ||
protected $table = 'posts'; | ||
|
||
public function comments() | ||
{ | ||
return $this->morphMany(Comment::class, 'commentable'); | ||
} | ||
} | ||
|
||
class PostMorphToMany extends Model | ||
{ | ||
protected $table = 'posts'; | ||
|
||
public function comments() | ||
{ | ||
return $this->morphToMany(Comment::class, 'commentable'); | ||
} | ||
} | ||
|
||
class Comment extends Model | ||
{ | ||
} | ||
|
||
class SelectRelationTest extends TestCase | ||
{ | ||
use InteractsWithDatabase; | ||
|
||
/** @test */ | ||
public function it_handles_belongs_to_many_relationships() | ||
{ | ||
$this->setupDatabase(); | ||
|
||
$post = PostBelongsToMany::create(['content' => 'Content']); | ||
|
||
$commentA = Comment::create(['content' => 'Content A']); | ||
$commentB = Comment::create(['content' => 'Content B']); | ||
$commentC = Comment::create(['content' => 'Content C']); | ||
|
||
$post->comments()->sync([$commentA->getKey(), $commentC->getKey()]); | ||
|
||
$options = Comment::get()->pluck('content', 'id'); | ||
|
||
Route::get('select-relation', function () use ($post, $options) { | ||
return view('select-relation') | ||
->with('post', $post) | ||
->with('options', $options); | ||
})->middleware('web'); | ||
|
||
DB::enableQueryLog(); | ||
|
||
$this->visit('/select-relation') | ||
->seeElement('option[value="' . $commentA->getKey() . '"]:selected') | ||
->seeElement('option[value="' . $commentB->getKey() . '"]:not(:selected)') | ||
->seeElement('option[value="' . $commentC->getKey() . '"]:selected'); | ||
|
||
// make sure we cache the result for each option element | ||
$this->assertCount(1, DB::getQueryLog()); | ||
} | ||
|
||
/** @test */ | ||
public function it_handles_morph_many_relationships() | ||
{ | ||
$this->setupDatabase(); | ||
|
||
$post = PostMorphMany::create(['content' => 'Content']); | ||
|
||
$commentA = $post->comments()->create(['content' => 'Content A']); | ||
$commentB = Comment::create(['content' => 'Content B']); | ||
$commentC = $post->comments()->create(['content' => 'Content C']); | ||
|
||
$options = Comment::get()->pluck('content', 'id'); | ||
|
||
Route::get('select-relation', function () use ($post, $options) { | ||
return view('select-relation') | ||
->with('post', $post) | ||
->with('options', $options); | ||
})->middleware('web'); | ||
|
||
DB::enableQueryLog(); | ||
|
||
$this->visit('/select-relation') | ||
->seeElement('option[value="' . $commentA->getKey() . '"]:selected') | ||
->seeElement('option[value="' . $commentB->getKey() . '"]:not(:selected)') | ||
->seeElement('option[value="' . $commentC->getKey() . '"]:selected'); | ||
|
||
// make sure we cache the result for each option element | ||
$this->assertCount(1, DB::getQueryLog()); | ||
} | ||
|
||
/** @test */ | ||
public function it_handles_morph_to_many_relationships() | ||
{ | ||
$this->setupDatabase(); | ||
|
||
$post = PostMorphToMany::create(['content' => 'Content']); | ||
|
||
$commentA = $post->comments()->create(['content' => 'Content A']); | ||
$commentB = Comment::create(['content' => 'Content B']); | ||
$commentC = $post->comments()->create(['content' => 'Content C']); | ||
|
||
$options = Comment::get()->pluck('content', 'id'); | ||
|
||
Route::get('select-relation', function () use ($post, $options) { | ||
return view('select-relation') | ||
->with('post', $post) | ||
->with('options', $options); | ||
})->middleware('web'); | ||
|
||
DB::enableQueryLog(); | ||
|
||
$this->visit('/select-relation') | ||
->seeElement('option[value="' . $commentA->getKey() . '"]:selected') | ||
->seeElement('option[value="' . $commentB->getKey() . '"]:not(:selected)') | ||
->seeElement('option[value="' . $commentC->getKey() . '"]:selected'); | ||
|
||
// make sure we cache the result for each option element | ||
$this->assertCount(1, DB::getQueryLog()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateCommentPostTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('comment_post', function (Blueprint $table) { | ||
$table->unsignedBigInteger('post_id'); | ||
$table->unsignedBigInteger('comment_id'); | ||
}); | ||
} | ||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('comment_post'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateCommentablesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('commentables', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->unsignedBigInteger('comment_id'); | ||
$table->morphs('commentable'); | ||
}); | ||
} | ||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('comments'); | ||
} | ||
} |
Oops, something went wrong.