EloquentMorphToIsTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\EloquentMorphToIsTest;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6. use Illuminate\Tests\Integration\Database\DatabaseTestCase;
  7. class EloquentMorphToIsTest extends DatabaseTestCase
  8. {
  9. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  10. {
  11. Schema::create('posts', function (Blueprint $table) {
  12. $table->increments('id');
  13. $table->timestamps();
  14. });
  15. Schema::create('comments', function (Blueprint $table) {
  16. $table->increments('id');
  17. $table->string('commentable_type');
  18. $table->integer('commentable_id');
  19. });
  20. $post = Post::create();
  21. (new Comment)->commentable()->associate($post)->save();
  22. }
  23. public function testParentIsNotNull()
  24. {
  25. $child = Comment::first();
  26. $parent = null;
  27. $this->assertFalse($child->commentable()->is($parent));
  28. $this->assertTrue($child->commentable()->isNot($parent));
  29. }
  30. public function testParentIsModel()
  31. {
  32. $child = Comment::first();
  33. $parent = Post::first();
  34. $this->assertTrue($child->commentable()->is($parent));
  35. $this->assertFalse($child->commentable()->isNot($parent));
  36. }
  37. public function testParentIsNotAnotherModel()
  38. {
  39. $child = Comment::first();
  40. $parent = new Post;
  41. $parent->id = 2;
  42. $this->assertFalse($child->commentable()->is($parent));
  43. $this->assertTrue($child->commentable()->isNot($parent));
  44. }
  45. public function testNullParentIsNotModel()
  46. {
  47. $child = Comment::first();
  48. $child->commentable()->dissociate();
  49. $parent = Post::first();
  50. $this->assertFalse($child->commentable()->is($parent));
  51. $this->assertTrue($child->commentable()->isNot($parent));
  52. }
  53. public function testParentIsNotModelWithAnotherTable()
  54. {
  55. $child = Comment::first();
  56. $parent = Post::first();
  57. $parent->setTable('foo');
  58. $this->assertFalse($child->commentable()->is($parent));
  59. $this->assertTrue($child->commentable()->isNot($parent));
  60. }
  61. public function testParentIsNotModelWithAnotherConnection()
  62. {
  63. $child = Comment::first();
  64. $parent = Post::first();
  65. $parent->setConnection('foo');
  66. $this->assertFalse($child->commentable()->is($parent));
  67. $this->assertTrue($child->commentable()->isNot($parent));
  68. }
  69. }
  70. class Comment extends Model
  71. {
  72. public $timestamps = false;
  73. public function commentable()
  74. {
  75. return $this->morphTo();
  76. }
  77. }
  78. class Post extends Model
  79. {
  80. //
  81. }