Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added test for morphToMany relationship
  • Loading branch information
pascalbaljet committed Dec 22, 2020
commit 4b714a9c584b3f4b539a31762971fbab2d788fad
2 changes: 2 additions & 0 deletions tests/Feature/InteractsWithDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ protected function setupDatabase()
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();
}
}
40 changes: 40 additions & 0 deletions tests/Feature/SelectRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public function comments()
}
}

class PostMorphToMany extends Model
{
protected $table = 'posts';

public function comments()
{
return $this->morphToMany(Comment::class, 'commentable');
}
}

class Comment extends Model
{
}
Expand Down Expand Up @@ -96,4 +106,34 @@ public function it_handles_morph_many_relationships()
// 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());
}
}
31 changes: 31 additions & 0 deletions tests/Feature/database/create_commentables_table.php
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');
}
}