EloquentHasOneIsTest.php 2.6 KB

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