EloquentMorphOneIsTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\EloquentMorphOneIsTest;
  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 EloquentMorphOneIsTest 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('attachments', function (Blueprint $table) {
  16. $table->increments('id');
  17. $table->string('attachable_type')->nullable();
  18. $table->integer('attachable_id')->nullable();
  19. });
  20. $post = Post::create();
  21. $post->attachment()->create();
  22. }
  23. public function testChildIsNotNull()
  24. {
  25. $parent = Post::first();
  26. $child = null;
  27. $this->assertFalse($parent->attachment()->is($child));
  28. $this->assertTrue($parent->attachment()->isNot($child));
  29. }
  30. public function testChildIsModel()
  31. {
  32. $parent = Post::first();
  33. $child = Attachment::first();
  34. $this->assertTrue($parent->attachment()->is($child));
  35. $this->assertFalse($parent->attachment()->isNot($child));
  36. }
  37. public function testChildIsNotAnotherModel()
  38. {
  39. $parent = Post::first();
  40. $child = new Attachment;
  41. $child->id = 2;
  42. $this->assertFalse($parent->attachment()->is($child));
  43. $this->assertTrue($parent->attachment()->isNot($child));
  44. }
  45. public function testNullChildIsNotModel()
  46. {
  47. $parent = Post::first();
  48. $child = Attachment::first();
  49. $child->attachable_type = null;
  50. $child->attachable_id = null;
  51. $this->assertFalse($parent->attachment()->is($child));
  52. $this->assertTrue($parent->attachment()->isNot($child));
  53. }
  54. public function testChildIsNotModelWithAnotherTable()
  55. {
  56. $parent = Post::first();
  57. $child = Attachment::first();
  58. $child->setTable('foo');
  59. $this->assertFalse($parent->attachment()->is($child));
  60. $this->assertTrue($parent->attachment()->isNot($child));
  61. }
  62. public function testChildIsNotModelWithAnotherConnection()
  63. {
  64. $parent = Post::first();
  65. $child = Attachment::first();
  66. $child->setConnection('foo');
  67. $this->assertFalse($parent->attachment()->is($child));
  68. $this->assertTrue($parent->attachment()->isNot($child));
  69. }
  70. }
  71. class Attachment extends Model
  72. {
  73. public $timestamps = false;
  74. }
  75. class Post extends Model
  76. {
  77. public function attachment()
  78. {
  79. return $this->morphOne(Attachment::class, 'attachable');
  80. }
  81. }