1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
<?php
namespace Illuminate\Tests\Integration\Database;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* @group integration
*/
class EloquentPushTest extends DatabaseTestCase
{
protected function setUp(): void
{
parent::setUp();
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->unsignedInteger('user_id');
});
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('comment');
$table->unsignedInteger('post_id');
});
}
public function testPushMethodSavesTheRelationshipsRecursively()
{
$user = new UserX;
$user->name = 'Test';
$user->save();
$user->posts()->create(['title' => 'Test title']);
$post = PostX::firstOrFail();
$post->comments()->create(['comment' => 'Test comment']);
$user = $user->fresh();
$user->name = 'Test 1';
$user->posts[0]->title = 'Test title 1';
$user->posts[0]->comments[0]->comment = 'Test comment 1';
$user->push();
$this->assertSame(1, UserX::count());
$this->assertSame('Test 1', UserX::firstOrFail()->name);
$this->assertSame(1, PostX::count());
$this->assertSame('Test title 1', PostX::firstOrFail()->title);
$this->assertSame(1, CommentX::count());
$this->assertSame('Test comment 1', CommentX::firstOrFail()->comment);
}
}
class UserX extends Model
{
public $timestamps = false;
protected $guarded = [];
protected $table = 'users';
public function posts()
{
return $this->hasMany(PostX::class, 'user_id');
}
}
class PostX extends Model
{
public $timestamps = false;
protected $guarded = [];
protected $table = 'posts';
public function comments()
{
return $this->hasMany(CommentX::class, 'post_id');
}
}
class CommentX extends Model
{
public $timestamps = false;
protected $guarded = [];
protected $table = 'comments';
}
|