123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- namespace Illuminate\Tests\Integration\Database\EloquentWhereHasMorphTest;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\Relation;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Tests\Integration\Database\DatabaseTestCase;
- class EloquentWhereHasMorphTest extends DatabaseTestCase
- {
- protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
- {
- Schema::create('posts', function (Blueprint $table) {
- $table->increments('id');
- $table->string('title');
- $table->softDeletes();
- });
- Schema::create('videos', function (Blueprint $table) {
- $table->increments('id');
- $table->string('title');
- });
- Schema::create('comments', function (Blueprint $table) {
- $table->increments('id');
- $table->morphs('commentable');
- $table->softDeletes();
- });
- $models = [];
- $models[] = Post::create(['title' => 'foo']);
- $models[] = Post::create(['title' => 'bar']);
- $models[] = Post::create(['title' => 'baz']);
- end($models)->delete();
- $models[] = Video::create(['title' => 'foo']);
- $models[] = Video::create(['title' => 'bar']);
- $models[] = Video::create(['title' => 'baz']);
- foreach ($models as $model) {
- (new Comment)->commentable()->associate($model)->save();
- }
- }
- public function testWhereHasMorph()
- {
- $comments = Comment::whereHasMorph('commentable', [Post::class, Video::class], function (Builder $query) {
- $query->where('title', 'foo');
- })->orderBy('id')->get();
- $this->assertEquals([1, 4], $comments->pluck('id')->all());
- }
- public function testWhereHasMorphWithMorphMap()
- {
- Relation::morphMap(['posts' => Post::class]);
- Comment::where('commentable_type', Post::class)->update(['commentable_type' => 'posts']);
- try {
- $comments = Comment::whereHasMorph('commentable', [Post::class, Video::class], function (Builder $query) {
- $query->where('title', 'foo');
- })->orderBy('id')->get();
- $this->assertEquals([1, 4], $comments->pluck('id')->all());
- } finally {
- Relation::morphMap([], false);
- }
- }
- public function testWhereHasMorphWithWildcard()
- {
- // Test newModelQuery() without global scopes.
- Comment::where('commentable_type', Video::class)->delete();
- $comments = Comment::withTrashed()
- ->whereHasMorph('commentable', '*', function (Builder $query) {
- $query->where('title', 'foo');
- })->orderBy('id')->get();
- $this->assertEquals([1, 4], $comments->pluck('id')->all());
- }
- public function testWhereHasMorphWithWildcardAndMorphMap()
- {
- Relation::morphMap(['posts' => Post::class]);
- Comment::where('commentable_type', Post::class)->update(['commentable_type' => 'posts']);
- try {
- $comments = Comment::whereHasMorph('commentable', '*', function (Builder $query) {
- $query->where('title', 'foo');
- })->orderBy('id')->get();
- $this->assertEquals([1, 4], $comments->pluck('id')->all());
- } finally {
- Relation::morphMap([], false);
- }
- }
- public function testWhereHasMorphWithRelationConstraint()
- {
- $comments = Comment::whereHasMorph('commentableWithConstraint', Video::class, function (Builder $query) {
- $query->where('title', 'like', 'ba%');
- })->orderBy('id')->get();
- $this->assertEquals([5], $comments->pluck('id')->all());
- }
- public function testWhereHasMorphWitDifferentConstraints()
- {
- $comments = Comment::whereHasMorph('commentable', [Post::class, Video::class], function (Builder $query, $type) {
- if ($type === Post::class) {
- $query->where('title', 'foo');
- }
- if ($type === Video::class) {
- $query->where('title', 'bar');
- }
- })->orderBy('id')->get();
- $this->assertEquals([1, 5], $comments->pluck('id')->all());
- }
- public function testWhereHasMorphWithOwnerKey()
- {
- Schema::table('posts', function (Blueprint $table) {
- $table->string('slug')->nullable();
- });
- Schema::table('comments', function (Blueprint $table) {
- $table->dropIndex('comments_commentable_type_commentable_id_index');
- });
- Schema::table('comments', function (Blueprint $table) {
- $table->string('commentable_id')->change();
- });
- Post::where('id', 1)->update(['slug' => 'foo']);
- Comment::where('id', 1)->update(['commentable_id' => 'foo']);
- $comments = Comment::whereHasMorph('commentableWithOwnerKey', Post::class, function (Builder $query) {
- $query->where('title', 'foo');
- })->orderBy('id')->get();
- $this->assertEquals([1], $comments->pluck('id')->all());
- }
- public function testHasMorph()
- {
- $comments = Comment::hasMorph('commentable', Post::class)->orderBy('id')->get();
- $this->assertEquals([1, 2], $comments->pluck('id')->all());
- }
- public function testOrHasMorph()
- {
- $comments = Comment::where('id', 1)->orHasMorph('commentable', Video::class)->orderBy('id')->get();
- $this->assertEquals([1, 4, 5, 6], $comments->pluck('id')->all());
- }
- public function testDoesntHaveMorph()
- {
- $comments = Comment::doesntHaveMorph('commentable', Post::class)->orderBy('id')->get();
- $this->assertEquals([3], $comments->pluck('id')->all());
- }
- public function testOrDoesntHaveMorph()
- {
- $comments = Comment::where('id', 1)->orDoesntHaveMorph('commentable', Post::class)->orderBy('id')->get();
- $this->assertEquals([1, 3], $comments->pluck('id')->all());
- }
- public function testOrWhereHasMorph()
- {
- $comments = Comment::where('id', 1)
- ->orWhereHasMorph('commentable', Video::class, function (Builder $query) {
- $query->where('title', 'foo');
- })->orderBy('id')->get();
- $this->assertEquals([1, 4], $comments->pluck('id')->all());
- }
- public function testWhereDoesntHaveMorph()
- {
- $comments = Comment::whereDoesntHaveMorph('commentable', Post::class, function (Builder $query) {
- $query->where('title', 'foo');
- })->orderBy('id')->get();
- $this->assertEquals([2, 3], $comments->pluck('id')->all());
- }
- public function testOrWhereDoesntHaveMorph()
- {
- $comments = Comment::where('id', 1)
- ->orWhereDoesntHaveMorph('commentable', Post::class, function (Builder $query) {
- $query->where('title', 'foo');
- })->orderBy('id')->get();
- $this->assertEquals([1, 2, 3], $comments->pluck('id')->all());
- }
- public function testModelScopesAreAccessible()
- {
- $comments = Comment::whereHasMorph('commentable', [Post::class, Video::class], function (Builder $query) {
- $query->someSharedModelScope();
- })->orderBy('id')->get();
- $this->assertEquals([1, 4], $comments->pluck('id')->all());
- }
- }
- class Comment extends Model
- {
- use SoftDeletes;
- public $timestamps = false;
- protected $guarded = [];
- public function commentable()
- {
- return $this->morphTo();
- }
- public function commentableWithConstraint()
- {
- return $this->morphTo('commentable')->where('title', 'bar');
- }
- public function commentableWithOwnerKey()
- {
- return $this->morphTo('commentable', null, null, 'slug');
- }
- }
- class Post extends Model
- {
- use SoftDeletes;
- public $timestamps = false;
- protected $guarded = [];
- public function scopeSomeSharedModelScope($query)
- {
- $query->where('title', '=', 'foo');
- }
- }
- class Video extends Model
- {
- public $timestamps = false;
- protected $guarded = [];
- public function scopeSomeSharedModelScope($query)
- {
- $query->where('title', '=', 'foo');
- }
- }
|