EloquentModelRefreshTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\EloquentModelRefreshTest;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Database\Schema\Blueprint;
  7. use Illuminate\Support\Facades\Schema;
  8. use Illuminate\Tests\Integration\Database\DatabaseTestCase;
  9. class EloquentModelRefreshTest extends DatabaseTestCase
  10. {
  11. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  12. {
  13. Schema::create('posts', function (Blueprint $table) {
  14. $table->increments('id');
  15. $table->string('title');
  16. $table->timestamps();
  17. $table->softDeletes();
  18. });
  19. }
  20. public function testItRefreshesModelExcludedByGlobalScope()
  21. {
  22. $post = Post::create(['title' => 'mohamed']);
  23. $post->refresh();
  24. }
  25. public function testItRefreshesASoftDeletedModel()
  26. {
  27. $post = Post::create(['title' => 'said']);
  28. Post::find($post->id)->delete();
  29. $this->assertFalse($post->trashed());
  30. $post->refresh();
  31. $this->assertTrue($post->trashed());
  32. }
  33. public function testItSyncsOriginalOnRefresh()
  34. {
  35. $post = Post::create(['title' => 'pat']);
  36. Post::find($post->id)->update(['title' => 'patrick']);
  37. $post->refresh();
  38. $this->assertEmpty($post->getDirty());
  39. $this->assertSame('patrick', $post->getOriginal('title'));
  40. }
  41. public function testAsPivot()
  42. {
  43. Schema::create('post_posts', function (Blueprint $table) {
  44. $table->increments('id');
  45. $table->bigInteger('foreign_id');
  46. $table->bigInteger('related_id');
  47. });
  48. $post = AsPivotPost::create(['title' => 'parent']);
  49. $child = AsPivotPost::create(['title' => 'child']);
  50. $post->children()->attach($child->getKey());
  51. $this->assertEquals(1, $post->children->count());
  52. $post->children->first()->refresh();
  53. }
  54. }
  55. class Post extends Model
  56. {
  57. public $table = 'posts';
  58. public $timestamps = true;
  59. protected $guarded = [];
  60. use SoftDeletes;
  61. protected static function boot()
  62. {
  63. parent::boot();
  64. static::addGlobalScope('age', function ($query) {
  65. $query->where('title', '!=', 'mohamed');
  66. });
  67. }
  68. }
  69. class AsPivotPost extends Post
  70. {
  71. public function children()
  72. {
  73. return $this
  74. ->belongsToMany(static::class, (new AsPivotPostPivot)->getTable(), 'foreign_id', 'related_id')
  75. ->using(AsPivotPostPivot::class);
  76. }
  77. }
  78. class AsPivotPostPivot extends Model
  79. {
  80. use AsPivot;
  81. protected $table = 'post_posts';
  82. }