-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fc6cd5
commit 27b1652
Showing
4 changed files
with
230 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
tests/Integration/Database/EloquentCollectionLoadMissingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\EloquentCollectionLoadMissingTest; | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Tests\Integration\Database\DatabaseTestCase; | ||
|
||
/** | ||
* @group integration | ||
*/ | ||
class EloquentCollectionLoadMissingTest extends DatabaseTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('users', function (Blueprint $table) { | ||
$table->increments('id'); | ||
}); | ||
|
||
Schema::create('posts', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('user_id'); | ||
}); | ||
|
||
Schema::create('comments', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('parent_id')->nullable(); | ||
$table->unsignedInteger('post_id'); | ||
}); | ||
|
||
Schema::create('revisions', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('comment_id'); | ||
}); | ||
|
||
User::create(); | ||
|
||
Post::create(['user_id' => 1]); | ||
|
||
Comment::create(['parent_id' => null, 'post_id' => 1]); | ||
Comment::create(['parent_id' => 1, 'post_id' => 1]); | ||
|
||
Revision::create(['comment_id' => 1]); | ||
} | ||
|
||
public function testLoadMissing() | ||
{ | ||
$posts = Post::with('comments', 'user')->get(); | ||
|
||
\DB::enableQueryLog(); | ||
|
||
$posts->loadMissing('comments.parent:id.revisions', 'user:id'); | ||
|
||
$this->assertCount(2, \DB::getQueryLog()); | ||
$this->assertTrue($posts[0]->comments[0]->relationLoaded('parent')); | ||
$this->assertTrue($posts[0]->comments[1]->parent->relationLoaded('revisions')); | ||
$this->assertFalse(array_key_exists('post_id', $posts[0]->comments[1]->parent->getAttributes())); | ||
} | ||
} | ||
|
||
class Comment extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
protected $guarded = ['id']; | ||
|
||
public function parent() { | ||
return $this->belongsTo(Comment::class); | ||
} | ||
|
||
public function revisions() { | ||
return $this->hasMany(Revision::class); | ||
} | ||
} | ||
|
||
class Post extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
protected $guarded = ['id']; | ||
|
||
public function comments() { | ||
return $this->hasMany(Comment::class); | ||
} | ||
|
||
public function user() { | ||
return $this->belongsTo(User::class); | ||
} | ||
} | ||
|
||
class Revision extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
protected $guarded = ['id']; | ||
} | ||
|
||
class User extends Model | ||
{ | ||
public $timestamps = false; | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/Integration/Database/EloquentModelLoadMissingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\EloquentModelLoadMissingTest; | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Tests\Integration\Database\DatabaseTestCase; | ||
|
||
/** | ||
* @group integration | ||
*/ | ||
class EloquentModelLoadMissingTest extends DatabaseTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('posts', function (Blueprint $table) { | ||
$table->increments('id'); | ||
}); | ||
|
||
Schema::create('comments', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('parent_id')->nullable(); | ||
$table->unsignedInteger('post_id'); | ||
}); | ||
|
||
Post::create(); | ||
|
||
Comment::create(['parent_id' => null, 'post_id' => 1]); | ||
Comment::create(['parent_id' => 1, 'post_id' => 1]); | ||
} | ||
|
||
public function testLoadMissing() | ||
{ | ||
$post = Post::with('comments')->first(); | ||
|
||
\DB::enableQueryLog(); | ||
|
||
$post->loadMissing('comments.parent'); | ||
|
||
$this->assertCount(1, \DB::getQueryLog()); | ||
$this->assertTrue($post->comments[0]->relationLoaded('parent')); | ||
} | ||
} | ||
|
||
class Comment extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
protected $guarded = ['id']; | ||
|
||
public function parent() { | ||
return $this->belongsTo(Comment::class); | ||
} | ||
} | ||
|
||
class Post extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
public function comments() { | ||
return $this->hasMany(Comment::class); | ||
} | ||
} |