|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProtoneMedia\LaravelFormComponents\Tests\Feature; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use Illuminate\Support\Facades\Route; |
| 8 | +use ProtoneMedia\LaravelFormComponents\Tests\TestCase; |
| 9 | + |
| 10 | +class PostBelongsToMany extends Model |
| 11 | +{ |
| 12 | + protected $table = 'posts'; |
| 13 | + |
| 14 | + public function comments() |
| 15 | + { |
| 16 | + return $this->belongsToMany(Comment::class, 'comment_post', 'post_id', 'comment_id'); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class PostMorphMany extends Model |
| 21 | +{ |
| 22 | + protected $table = 'posts'; |
| 23 | + |
| 24 | + public function comments() |
| 25 | + { |
| 26 | + return $this->morphMany(Comment::class, 'commentable'); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class PostMorphToMany extends Model |
| 31 | +{ |
| 32 | + protected $table = 'posts'; |
| 33 | + |
| 34 | + public function comments() |
| 35 | + { |
| 36 | + return $this->morphToMany(Comment::class, 'commentable'); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +class Comment extends Model |
| 41 | +{ |
| 42 | +} |
| 43 | + |
| 44 | +class SelectRelationTest extends TestCase |
| 45 | +{ |
| 46 | + use InteractsWithDatabase; |
| 47 | + |
| 48 | + /** @test */ |
| 49 | + public function it_handles_belongs_to_many_relationships() |
| 50 | + { |
| 51 | + $this->setupDatabase(); |
| 52 | + |
| 53 | + $post = PostBelongsToMany::create(['content' => 'Content']); |
| 54 | + |
| 55 | + $commentA = Comment::create(['content' => 'Content A']); |
| 56 | + $commentB = Comment::create(['content' => 'Content B']); |
| 57 | + $commentC = Comment::create(['content' => 'Content C']); |
| 58 | + |
| 59 | + $post->comments()->sync([$commentA->getKey(), $commentC->getKey()]); |
| 60 | + |
| 61 | + $options = Comment::get()->pluck('content', 'id'); |
| 62 | + |
| 63 | + Route::get('select-relation', function () use ($post, $options) { |
| 64 | + return view('select-relation') |
| 65 | + ->with('post', $post) |
| 66 | + ->with('options', $options); |
| 67 | + })->middleware('web'); |
| 68 | + |
| 69 | + DB::enableQueryLog(); |
| 70 | + |
| 71 | + $this->visit('/select-relation') |
| 72 | + ->seeElement('option[value="' . $commentA->getKey() . '"]:selected') |
| 73 | + ->seeElement('option[value="' . $commentB->getKey() . '"]:not(:selected)') |
| 74 | + ->seeElement('option[value="' . $commentC->getKey() . '"]:selected'); |
| 75 | + |
| 76 | + // make sure we cache the result for each option element |
| 77 | + $this->assertCount(1, DB::getQueryLog()); |
| 78 | + } |
| 79 | + |
| 80 | + /** @test */ |
| 81 | + public function it_handles_morph_many_relationships() |
| 82 | + { |
| 83 | + $this->setupDatabase(); |
| 84 | + |
| 85 | + $post = PostMorphMany::create(['content' => 'Content']); |
| 86 | + |
| 87 | + $commentA = $post->comments()->create(['content' => 'Content A']); |
| 88 | + $commentB = Comment::create(['content' => 'Content B']); |
| 89 | + $commentC = $post->comments()->create(['content' => 'Content C']); |
| 90 | + |
| 91 | + $options = Comment::get()->pluck('content', 'id'); |
| 92 | + |
| 93 | + Route::get('select-relation', function () use ($post, $options) { |
| 94 | + return view('select-relation') |
| 95 | + ->with('post', $post) |
| 96 | + ->with('options', $options); |
| 97 | + })->middleware('web'); |
| 98 | + |
| 99 | + DB::enableQueryLog(); |
| 100 | + |
| 101 | + $this->visit('/select-relation') |
| 102 | + ->seeElement('option[value="' . $commentA->getKey() . '"]:selected') |
| 103 | + ->seeElement('option[value="' . $commentB->getKey() . '"]:not(:selected)') |
| 104 | + ->seeElement('option[value="' . $commentC->getKey() . '"]:selected'); |
| 105 | + |
| 106 | + // make sure we cache the result for each option element |
| 107 | + $this->assertCount(1, DB::getQueryLog()); |
| 108 | + } |
| 109 | + |
| 110 | + /** @test */ |
| 111 | + public function it_handles_morph_to_many_relationships() |
| 112 | + { |
| 113 | + $this->setupDatabase(); |
| 114 | + |
| 115 | + $post = PostMorphToMany::create(['content' => 'Content']); |
| 116 | + |
| 117 | + $commentA = $post->comments()->create(['content' => 'Content A']); |
| 118 | + $commentB = Comment::create(['content' => 'Content B']); |
| 119 | + $commentC = $post->comments()->create(['content' => 'Content C']); |
| 120 | + |
| 121 | + $options = Comment::get()->pluck('content', 'id'); |
| 122 | + |
| 123 | + Route::get('select-relation', function () use ($post, $options) { |
| 124 | + return view('select-relation') |
| 125 | + ->with('post', $post) |
| 126 | + ->with('options', $options); |
| 127 | + })->middleware('web'); |
| 128 | + |
| 129 | + DB::enableQueryLog(); |
| 130 | + |
| 131 | + $this->visit('/select-relation') |
| 132 | + ->seeElement('option[value="' . $commentA->getKey() . '"]:selected') |
| 133 | + ->seeElement('option[value="' . $commentB->getKey() . '"]:not(:selected)') |
| 134 | + ->seeElement('option[value="' . $commentC->getKey() . '"]:selected'); |
| 135 | + |
| 136 | + // make sure we cache the result for each option element |
| 137 | + $this->assertCount(1, DB::getQueryLog()); |
| 138 | + } |
| 139 | +} |
0 commit comments