EloquentWhereHasMorphTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\EloquentWhereHasMorphTest;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\Relation;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. use Illuminate\Database\Schema\Blueprint;
  8. use Illuminate\Support\Facades\Schema;
  9. use Illuminate\Tests\Integration\Database\DatabaseTestCase;
  10. class EloquentWhereHasMorphTest extends DatabaseTestCase
  11. {
  12. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  13. {
  14. Schema::create('posts', function (Blueprint $table) {
  15. $table->increments('id');
  16. $table->string('title');
  17. $table->softDeletes();
  18. });
  19. Schema::create('videos', function (Blueprint $table) {
  20. $table->increments('id');
  21. $table->string('title');
  22. });
  23. Schema::create('comments', function (Blueprint $table) {
  24. $table->increments('id');
  25. $table->morphs('commentable');
  26. $table->softDeletes();
  27. });
  28. $models = [];
  29. $models[] = Post::create(['title' => 'foo']);
  30. $models[] = Post::create(['title' => 'bar']);
  31. $models[] = Post::create(['title' => 'baz']);
  32. end($models)->delete();
  33. $models[] = Video::create(['title' => 'foo']);
  34. $models[] = Video::create(['title' => 'bar']);
  35. $models[] = Video::create(['title' => 'baz']);
  36. foreach ($models as $model) {
  37. (new Comment)->commentable()->associate($model)->save();
  38. }
  39. }
  40. public function testWhereHasMorph()
  41. {
  42. $comments = Comment::whereHasMorph('commentable', [Post::class, Video::class], function (Builder $query) {
  43. $query->where('title', 'foo');
  44. })->orderBy('id')->get();
  45. $this->assertEquals([1, 4], $comments->pluck('id')->all());
  46. }
  47. public function testWhereHasMorphWithMorphMap()
  48. {
  49. Relation::morphMap(['posts' => Post::class]);
  50. Comment::where('commentable_type', Post::class)->update(['commentable_type' => 'posts']);
  51. try {
  52. $comments = Comment::whereHasMorph('commentable', [Post::class, Video::class], function (Builder $query) {
  53. $query->where('title', 'foo');
  54. })->orderBy('id')->get();
  55. $this->assertEquals([1, 4], $comments->pluck('id')->all());
  56. } finally {
  57. Relation::morphMap([], false);
  58. }
  59. }
  60. public function testWhereHasMorphWithWildcard()
  61. {
  62. // Test newModelQuery() without global scopes.
  63. Comment::where('commentable_type', Video::class)->delete();
  64. $comments = Comment::withTrashed()
  65. ->whereHasMorph('commentable', '*', function (Builder $query) {
  66. $query->where('title', 'foo');
  67. })->orderBy('id')->get();
  68. $this->assertEquals([1, 4], $comments->pluck('id')->all());
  69. }
  70. public function testWhereHasMorphWithWildcardAndMorphMap()
  71. {
  72. Relation::morphMap(['posts' => Post::class]);
  73. Comment::where('commentable_type', Post::class)->update(['commentable_type' => 'posts']);
  74. try {
  75. $comments = Comment::whereHasMorph('commentable', '*', function (Builder $query) {
  76. $query->where('title', 'foo');
  77. })->orderBy('id')->get();
  78. $this->assertEquals([1, 4], $comments->pluck('id')->all());
  79. } finally {
  80. Relation::morphMap([], false);
  81. }
  82. }
  83. public function testWhereHasMorphWithRelationConstraint()
  84. {
  85. $comments = Comment::whereHasMorph('commentableWithConstraint', Video::class, function (Builder $query) {
  86. $query->where('title', 'like', 'ba%');
  87. })->orderBy('id')->get();
  88. $this->assertEquals([5], $comments->pluck('id')->all());
  89. }
  90. public function testWhereHasMorphWitDifferentConstraints()
  91. {
  92. $comments = Comment::whereHasMorph('commentable', [Post::class, Video::class], function (Builder $query, $type) {
  93. if ($type === Post::class) {
  94. $query->where('title', 'foo');
  95. }
  96. if ($type === Video::class) {
  97. $query->where('title', 'bar');
  98. }
  99. })->orderBy('id')->get();
  100. $this->assertEquals([1, 5], $comments->pluck('id')->all());
  101. }
  102. public function testWhereHasMorphWithOwnerKey()
  103. {
  104. Schema::table('posts', function (Blueprint $table) {
  105. $table->string('slug')->nullable();
  106. });
  107. Schema::table('comments', function (Blueprint $table) {
  108. $table->dropIndex('comments_commentable_type_commentable_id_index');
  109. });
  110. Schema::table('comments', function (Blueprint $table) {
  111. $table->string('commentable_id')->change();
  112. });
  113. Post::where('id', 1)->update(['slug' => 'foo']);
  114. Comment::where('id', 1)->update(['commentable_id' => 'foo']);
  115. $comments = Comment::whereHasMorph('commentableWithOwnerKey', Post::class, function (Builder $query) {
  116. $query->where('title', 'foo');
  117. })->orderBy('id')->get();
  118. $this->assertEquals([1], $comments->pluck('id')->all());
  119. }
  120. public function testHasMorph()
  121. {
  122. $comments = Comment::hasMorph('commentable', Post::class)->orderBy('id')->get();
  123. $this->assertEquals([1, 2], $comments->pluck('id')->all());
  124. }
  125. public function testOrHasMorph()
  126. {
  127. $comments = Comment::where('id', 1)->orHasMorph('commentable', Video::class)->orderBy('id')->get();
  128. $this->assertEquals([1, 4, 5, 6], $comments->pluck('id')->all());
  129. }
  130. public function testDoesntHaveMorph()
  131. {
  132. $comments = Comment::doesntHaveMorph('commentable', Post::class)->orderBy('id')->get();
  133. $this->assertEquals([3], $comments->pluck('id')->all());
  134. }
  135. public function testOrDoesntHaveMorph()
  136. {
  137. $comments = Comment::where('id', 1)->orDoesntHaveMorph('commentable', Post::class)->orderBy('id')->get();
  138. $this->assertEquals([1, 3], $comments->pluck('id')->all());
  139. }
  140. public function testOrWhereHasMorph()
  141. {
  142. $comments = Comment::where('id', 1)
  143. ->orWhereHasMorph('commentable', Video::class, function (Builder $query) {
  144. $query->where('title', 'foo');
  145. })->orderBy('id')->get();
  146. $this->assertEquals([1, 4], $comments->pluck('id')->all());
  147. }
  148. public function testWhereDoesntHaveMorph()
  149. {
  150. $comments = Comment::whereDoesntHaveMorph('commentable', Post::class, function (Builder $query) {
  151. $query->where('title', 'foo');
  152. })->orderBy('id')->get();
  153. $this->assertEquals([2, 3], $comments->pluck('id')->all());
  154. }
  155. public function testOrWhereDoesntHaveMorph()
  156. {
  157. $comments = Comment::where('id', 1)
  158. ->orWhereDoesntHaveMorph('commentable', Post::class, function (Builder $query) {
  159. $query->where('title', 'foo');
  160. })->orderBy('id')->get();
  161. $this->assertEquals([1, 2, 3], $comments->pluck('id')->all());
  162. }
  163. public function testModelScopesAreAccessible()
  164. {
  165. $comments = Comment::whereHasMorph('commentable', [Post::class, Video::class], function (Builder $query) {
  166. $query->someSharedModelScope();
  167. })->orderBy('id')->get();
  168. $this->assertEquals([1, 4], $comments->pluck('id')->all());
  169. }
  170. }
  171. class Comment extends Model
  172. {
  173. use SoftDeletes;
  174. public $timestamps = false;
  175. protected $guarded = [];
  176. public function commentable()
  177. {
  178. return $this->morphTo();
  179. }
  180. public function commentableWithConstraint()
  181. {
  182. return $this->morphTo('commentable')->where('title', 'bar');
  183. }
  184. public function commentableWithOwnerKey()
  185. {
  186. return $this->morphTo('commentable', null, null, 'slug');
  187. }
  188. }
  189. class Post extends Model
  190. {
  191. use SoftDeletes;
  192. public $timestamps = false;
  193. protected $guarded = [];
  194. public function scopeSomeSharedModelScope($query)
  195. {
  196. $query->where('title', '=', 'foo');
  197. }
  198. }
  199. class Video extends Model
  200. {
  201. public $timestamps = false;
  202. protected $guarded = [];
  203. public function scopeSomeSharedModelScope($query)
  204. {
  205. $query->where('title', '=', 'foo');
  206. }
  207. }