123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- namespace Illuminate\Tests\Database\EloquentRelationshipsTest;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\HasManyThrough;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Database\Eloquent\Relations\HasOneThrough;
- use Illuminate\Database\Eloquent\Relations\MorphMany;
- use Illuminate\Database\Eloquent\Relations\MorphOne;
- use Illuminate\Database\Eloquent\Relations\MorphTo;
- use Illuminate\Database\Eloquent\Relations\MorphToMany;
- use PHPUnit\Framework\TestCase;
- class DatabaseEloquentRelationshipsTest extends TestCase
- {
- public function testStandardRelationships()
- {
- $post = new Post;
- $this->assertInstanceOf(HasOne::class, $post->attachment());
- $this->assertInstanceOf(BelongsTo::class, $post->author());
- $this->assertInstanceOf(HasMany::class, $post->comments());
- $this->assertInstanceOf(MorphOne::class, $post->owner());
- $this->assertInstanceOf(MorphMany::class, $post->likes());
- $this->assertInstanceOf(BelongsToMany::class, $post->viewers());
- $this->assertInstanceOf(HasManyThrough::class, $post->lovers());
- $this->assertInstanceOf(HasOneThrough::class, $post->contract());
- $this->assertInstanceOf(MorphToMany::class, $post->tags());
- $this->assertInstanceOf(MorphTo::class, $post->postable());
- }
- public function testOverriddenRelationships()
- {
- $post = new CustomPost;
- $this->assertInstanceOf(CustomHasOne::class, $post->attachment());
- $this->assertInstanceOf(CustomBelongsTo::class, $post->author());
- $this->assertInstanceOf(CustomHasMany::class, $post->comments());
- $this->assertInstanceOf(CustomMorphOne::class, $post->owner());
- $this->assertInstanceOf(CustomMorphMany::class, $post->likes());
- $this->assertInstanceOf(CustomBelongsToMany::class, $post->viewers());
- $this->assertInstanceOf(CustomHasManyThrough::class, $post->lovers());
- $this->assertInstanceOf(CustomHasOneThrough::class, $post->contract());
- $this->assertInstanceOf(CustomMorphToMany::class, $post->tags());
- $this->assertInstanceOf(CustomMorphTo::class, $post->postable());
- }
- public function testAlwaysUnsetBelongsToRelationWhenReceivedModelId()
- {
- // create users
- $user1 = (new FakeRelationship)->forceFill(['id' => 1]);
- $user2 = (new FakeRelationship)->forceFill(['id' => 2]);
- // sync user 1 using Model
- $post = new Post;
- $post->author()->associate($user1);
- $post->syncOriginal();
- // associate user 2 using Model
- $post->author()->associate($user2);
- $this->assertTrue($post->isDirty());
- $this->assertTrue($post->relationLoaded('author'));
- $this->assertSame($user2, $post->author);
- // associate user 1 using model ID
- $post->author()->associate($user1->id);
- $this->assertTrue($post->isClean());
- // we must unset relation even if attributes are clean
- $this->assertFalse($post->relationLoaded('author'));
- }
- }
- class FakeRelationship extends Model
- {
- //
- }
- class Post extends Model
- {
- public function attachment()
- {
- return $this->hasOne(FakeRelationship::class);
- }
- public function author()
- {
- return $this->belongsTo(FakeRelationship::class);
- }
- public function comments()
- {
- return $this->hasMany(FakeRelationship::class);
- }
- public function likes()
- {
- return $this->morphMany(FakeRelationship::class, 'actionable');
- }
- public function owner()
- {
- return $this->morphOne(FakeRelationship::class, 'property');
- }
- public function viewers()
- {
- return $this->belongsToMany(FakeRelationship::class);
- }
- public function lovers()
- {
- return $this->hasManyThrough(FakeRelationship::class, FakeRelationship::class);
- }
- public function contract()
- {
- return $this->hasOneThrough(FakeRelationship::class, FakeRelationship::class);
- }
- public function tags()
- {
- return $this->morphToMany(FakeRelationship::class, 'taggable');
- }
- public function postable()
- {
- return $this->morphTo();
- }
- }
- class CustomPost extends Post
- {
- protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
- {
- return new CustomBelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
- }
- protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
- {
- return new CustomHasMany($query, $parent, $foreignKey, $localKey);
- }
- protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
- {
- return new CustomHasOne($query, $parent, $foreignKey, $localKey);
- }
- protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
- {
- return new CustomMorphOne($query, $parent, $type, $id, $localKey);
- }
- protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
- {
- return new CustomMorphMany($query, $parent, $type, $id, $localKey);
- }
- protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey,
- $parentKey, $relatedKey, $relationName = null
- ) {
- return new CustomBelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
- }
- protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey,
- $secondKey, $localKey, $secondLocalKey
- ) {
- return new CustomHasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
- }
- protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey,
- $secondKey, $localKey, $secondLocalKey
- ) {
- return new CustomHasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
- }
- protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey,
- $relatedPivotKey, $parentKey, $relatedKey, $relationName = null, $inverse = false)
- {
- return new CustomMorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey,
- $relationName, $inverse);
- }
- protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation)
- {
- return new CustomMorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation);
- }
- }
- class CustomHasOne extends HasOne
- {
- //
- }
- class CustomBelongsTo extends BelongsTo
- {
- //
- }
- class CustomHasMany extends HasMany
- {
- //
- }
- class CustomMorphOne extends MorphOne
- {
- //
- }
- class CustomMorphMany extends MorphMany
- {
- //
- }
- class CustomBelongsToMany extends BelongsToMany
- {
- //
- }
- class CustomHasManyThrough extends HasManyThrough
- {
- //
- }
- class CustomHasOneThrough extends HasOneThrough
- {
- //
- }
- class CustomMorphToMany extends MorphToMany
- {
- //
- }
- class CustomMorphTo extends MorphTo
- {
- //
- }
|