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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
<?php
namespace Illuminate\Tests\Integration\Database\EloquentModelRefreshTest;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
class EloquentModelRefreshTest extends DatabaseTestCase
{
protected function afterRefreshingDatabase()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
$table->softDeletes();
});
}
public function testItRefreshesModelExcludedByGlobalScope()
{
$post = Post::create(['title' => 'mohamed']);
$post->refresh();
}
public function testItRefreshesASoftDeletedModel()
{
$post = Post::create(['title' => 'said']);
Post::find($post->id)->delete();
$this->assertFalse($post->trashed());
$post->refresh();
$this->assertTrue($post->trashed());
}
public function testItSyncsOriginalOnRefresh()
{
$post = Post::create(['title' => 'pat']);
Post::find($post->id)->update(['title' => 'patrick']);
$post->refresh();
$this->assertEmpty($post->getDirty());
$this->assertSame('patrick', $post->getOriginal('title'));
}
public function testAsPivot()
{
Schema::create('post_posts', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('foreign_id');
$table->bigInteger('related_id');
});
$post = AsPivotPost::create(['title' => 'parent']);
$child = AsPivotPost::create(['title' => 'child']);
$post->children()->attach($child->getKey());
$this->assertEquals(1, $post->children->count());
$post->children->first()->refresh();
}
}
class Post extends Model
{
public $table = 'posts';
public $timestamps = true;
protected $guarded = [];
use SoftDeletes;
protected static function boot()
{
parent::boot();
static::addGlobalScope('age', function ($query) {
$query->where('title', '!=', 'mohamed');
});
}
}
class AsPivotPost extends Post
{
public function children()
{
return $this
->belongsToMany(static::class, (new AsPivotPostPivot)->getTable(), 'foreign_id', 'related_id')
->using(AsPivotPostPivot::class);
}
}
class AsPivotPostPivot extends Model
{
use AsPivot;
protected $table = 'post_posts';
}
|