|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Database; |
| 4 | + |
| 5 | +use Illuminate\Database\Capsule\Manager as DB; |
| 6 | +use Illuminate\Database\Eloquent\Model; |
| 7 | +use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
| 8 | +use Illuminate\Database\Eloquent\Relations\MorphToMany; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class DatabaseEloquentBelongsToManyWithAttributesTest extends TestCase |
| 12 | +{ |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + $db = new DB; |
| 16 | + |
| 17 | + $db->addConnection([ |
| 18 | + 'driver' => 'sqlite', |
| 19 | + 'database' => ':memory:', |
| 20 | + ]); |
| 21 | + $db->bootEloquent(); |
| 22 | + $db->setAsGlobal(); |
| 23 | + $this->createSchema(); |
| 24 | + } |
| 25 | + |
| 26 | + public function testCreatesWithAttributesAndPivotValues(): void |
| 27 | + { |
| 28 | + $post = ManyToManyWithAttributesPost::create(); |
| 29 | + $tag = $post->metaTags()->create(['name' => 'long article']); |
| 30 | + |
| 31 | + $this->assertSame('long article', $tag->name); |
| 32 | + $this->assertTrue($tag->visible); |
| 33 | + |
| 34 | + $pivot = DB::table('with_attributes_pivot')->first(); |
| 35 | + $this->assertSame('meta', $pivot->type); |
| 36 | + $this->assertSame($post->id, $pivot->post_id); |
| 37 | + $this->assertSame($tag->id, $pivot->tag_id); |
| 38 | + } |
| 39 | + |
| 40 | + public function testQueriesWithAttributesAndPivotValues(): void |
| 41 | + { |
| 42 | + $post = new ManyToManyWithAttributesPost(['id' => 2]); |
| 43 | + $wheres = $post->metaTags()->toBase()->wheres; |
| 44 | + |
| 45 | + $this->assertContains([ |
| 46 | + 'type' => 'Basic', |
| 47 | + 'column' => 'with_attributes_tags.visible', |
| 48 | + 'operator' => '=', |
| 49 | + 'value' => true, |
| 50 | + 'boolean' => 'and', |
| 51 | + ], $wheres); |
| 52 | + |
| 53 | + $this->assertContains([ |
| 54 | + 'type' => 'Basic', |
| 55 | + 'column' => 'with_attributes_pivot.type', |
| 56 | + 'operator' => '=', |
| 57 | + 'value' => 'meta', |
| 58 | + 'boolean' => 'and', |
| 59 | + ], $wheres); |
| 60 | + } |
| 61 | + |
| 62 | + public function testMorphToManyWithAttributes(): void |
| 63 | + { |
| 64 | + $post = new ManyToManyWithAttributesPost(['id' => 2]); |
| 65 | + $wheres = $post->morphedTags()->toBase()->wheres; |
| 66 | + |
| 67 | + $this->assertContains([ |
| 68 | + 'type' => 'Basic', |
| 69 | + 'column' => 'with_attributes_tags.visible', |
| 70 | + 'operator' => '=', |
| 71 | + 'value' => true, |
| 72 | + 'boolean' => 'and', |
| 73 | + ], $wheres); |
| 74 | + |
| 75 | + $this->assertContains([ |
| 76 | + 'type' => 'Basic', |
| 77 | + 'column' => 'with_attributes_taggables.type', |
| 78 | + 'operator' => '=', |
| 79 | + 'value' => 'meta', |
| 80 | + 'boolean' => 'and', |
| 81 | + ], $wheres); |
| 82 | + |
| 83 | + $this->assertContains([ |
| 84 | + 'type' => 'Basic', |
| 85 | + 'column' => 'with_attributes_taggables.taggable_type', |
| 86 | + 'operator' => '=', |
| 87 | + 'value' => ManyToManyWithAttributesPost::class, |
| 88 | + 'boolean' => 'and', |
| 89 | + ], $wheres); |
| 90 | + |
| 91 | + $this->assertContains([ |
| 92 | + 'type' => 'Basic', |
| 93 | + 'column' => 'with_attributes_taggables.taggable_id', |
| 94 | + 'operator' => '=', |
| 95 | + 'value' => 2, |
| 96 | + 'boolean' => 'and', |
| 97 | + ], $wheres); |
| 98 | + |
| 99 | + $tag = $post->morphedTags()->create(['name' => 'new tag']); |
| 100 | + |
| 101 | + $this->assertTrue($tag->visible); |
| 102 | + $this->assertSame('new tag', $tag->name); |
| 103 | + $this->assertSame($tag->id, $post->morphedTags()->first()->id); |
| 104 | + } |
| 105 | + |
| 106 | + public function testMorphedByManyWithAttributes(): void |
| 107 | + { |
| 108 | + $tag = new ManyToManyWithAttributesTag(['id' => 4]); |
| 109 | + $wheres = $tag->morphedPosts()->toBase()->wheres; |
| 110 | + |
| 111 | + $this->assertContains([ |
| 112 | + 'type' => 'Basic', |
| 113 | + 'column' => 'with_attributes_posts.title', |
| 114 | + 'operator' => '=', |
| 115 | + 'value' => 'Title!', |
| 116 | + 'boolean' => 'and', |
| 117 | + ], $wheres); |
| 118 | + |
| 119 | + $this->assertContains([ |
| 120 | + 'type' => 'Basic', |
| 121 | + 'column' => 'with_attributes_taggables.type', |
| 122 | + 'operator' => '=', |
| 123 | + 'value' => 'meta', |
| 124 | + 'boolean' => 'and', |
| 125 | + ], $wheres); |
| 126 | + |
| 127 | + $this->assertContains([ |
| 128 | + 'type' => 'Basic', |
| 129 | + 'column' => 'with_attributes_taggables.taggable_type', |
| 130 | + 'operator' => '=', |
| 131 | + 'value' => ManyToManyWithAttributesPost::class, |
| 132 | + 'boolean' => 'and', |
| 133 | + ], $wheres); |
| 134 | + |
| 135 | + $this->assertContains([ |
| 136 | + 'type' => 'Basic', |
| 137 | + 'column' => 'with_attributes_taggables.tag_id', |
| 138 | + 'operator' => '=', |
| 139 | + 'value' => 4, |
| 140 | + 'boolean' => 'and', |
| 141 | + ], $wheres); |
| 142 | + |
| 143 | + $post = $tag->morphedPosts()->create(); |
| 144 | + $this->assertSame('Title!', $post->title); |
| 145 | + $this->assertSame($post->id, $tag->morphedPosts()->first()->id); |
| 146 | + } |
| 147 | + |
| 148 | + protected function createSchema() |
| 149 | + { |
| 150 | + $this->schema()->create('with_attributes_posts', function ($table) { |
| 151 | + $table->increments('id'); |
| 152 | + $table->string('title')->nullable(); |
| 153 | + $table->timestamps(); |
| 154 | + }); |
| 155 | + |
| 156 | + $this->schema()->create('with_attributes_tags', function ($table) { |
| 157 | + $table->increments('id'); |
| 158 | + $table->string('name'); |
| 159 | + $table->boolean('visible')->nullable(); |
| 160 | + $table->timestamps(); |
| 161 | + }); |
| 162 | + |
| 163 | + $this->schema()->create('with_attributes_pivot', function ($table) { |
| 164 | + $table->integer('post_id'); |
| 165 | + $table->integer('tag_id'); |
| 166 | + $table->string('type'); |
| 167 | + }); |
| 168 | + |
| 169 | + $this->schema()->create('with_attributes_taggables', function ($table) { |
| 170 | + $table->integer('tag_id'); |
| 171 | + $table->integer('taggable_id'); |
| 172 | + $table->string('taggable_type'); |
| 173 | + $table->string('type'); |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Tear down the database schema. |
| 179 | + * |
| 180 | + * @return void |
| 181 | + */ |
| 182 | + protected function tearDown(): void |
| 183 | + { |
| 184 | + $this->schema()->drop('with_attributes_posts'); |
| 185 | + $this->schema()->drop('with_attributes_tags'); |
| 186 | + $this->schema()->drop('with_attributes_pivot'); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Get a database connection instance. |
| 191 | + * |
| 192 | + * @return \Illuminate\Database\Connection |
| 193 | + */ |
| 194 | + protected function connection($connection = 'default') |
| 195 | + { |
| 196 | + return Model::getConnectionResolver()->connection($connection); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Get a schema builder instance. |
| 201 | + * |
| 202 | + * @return \Illuminate\Database\Schema\Builder |
| 203 | + */ |
| 204 | + protected function schema($connection = 'default') |
| 205 | + { |
| 206 | + return $this->connection($connection)->getSchemaBuilder(); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +class ManyToManyWithAttributesPost extends Model |
| 211 | +{ |
| 212 | + protected $guarded = []; |
| 213 | + protected $table = 'with_attributes_posts'; |
| 214 | + |
| 215 | + public function tags(): BelongsToMany |
| 216 | + { |
| 217 | + return $this->belongsToMany( |
| 218 | + ManyToManyWithAttributesTag::class, |
| 219 | + 'with_attributes_pivot', |
| 220 | + 'tag_id', |
| 221 | + 'post_id', |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | + public function metaTags(): BelongsToMany |
| 226 | + { |
| 227 | + return $this->tags() |
| 228 | + ->withAttributes('visible', true) |
| 229 | + ->withPivotValue('type', 'meta'); |
| 230 | + } |
| 231 | + |
| 232 | + public function morphedTags(): MorphToMany |
| 233 | + { |
| 234 | + return $this |
| 235 | + ->morphToMany( |
| 236 | + ManyToManyWithAttributesTag::class, |
| 237 | + 'taggable', |
| 238 | + 'with_attributes_taggables', |
| 239 | + relatedPivotKey: 'tag_id' |
| 240 | + ) |
| 241 | + ->withAttributes('visible', true) |
| 242 | + ->withPivotValue('type', 'meta'); |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +class ManyToManyWithAttributesTag extends Model |
| 247 | +{ |
| 248 | + protected $guarded = []; |
| 249 | + protected $table = 'with_attributes_tags'; |
| 250 | + |
| 251 | + public function morphedPosts(): MorphToMany |
| 252 | + { |
| 253 | + return $this |
| 254 | + ->morphedByMany( |
| 255 | + ManyToManyWithAttributesPost::class, |
| 256 | + 'taggable', |
| 257 | + 'with_attributes_taggables', |
| 258 | + 'tag_id', |
| 259 | + ) |
| 260 | + ->withAttributes('title', 'Title!') |
| 261 | + ->withPivotValue('type', 'meta'); |
| 262 | + } |
| 263 | +} |
0 commit comments