-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
9557960
commit 29ee0fc
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
tests/Integration/Database/EloquentModelLoadMinTest.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\EloquentModelLoadMinTest; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Tests\Integration\Database\DatabaseTestCase; | ||
|
||
class EloquentModelLoadMinTest extends DatabaseTestCase | ||
{ | ||
protected function afterRefreshingDatabase() | ||
{ | ||
Schema::create('base_models', function (Blueprint $table) { | ||
$table->increments('id'); | ||
}); | ||
|
||
Schema::create('related1s', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('base_model_id'); | ||
$table->integer('number'); | ||
}); | ||
|
||
Schema::create('related2s', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('base_model_id'); | ||
$table->integer('number'); | ||
}); | ||
|
||
BaseModel::create(); | ||
|
||
Related1::create(['base_model_id' => 1, 'number' => 10]); | ||
Related1::create(['base_model_id' => 1, 'number' => 11]); | ||
Related2::create(['base_model_id' => 1, 'number' => 12]); | ||
Related2::create(['base_model_id' => 1, 'number' => 13]); | ||
} | ||
|
||
public function testLoadMinSingleRelation() | ||
{ | ||
$model = BaseModel::first(); | ||
|
||
DB::enableQueryLog(); | ||
|
||
$model->loadMin('related1', 'number'); | ||
|
||
$this->assertCount(1, DB::getQueryLog()); | ||
$this->assertEquals(10, $model->related1_min_number); | ||
} | ||
|
||
public function testLoadMinMultipleRelations() | ||
{ | ||
$model = BaseModel::first(); | ||
|
||
DB::enableQueryLog(); | ||
|
||
$model->loadMin(['related1', 'related2'], 'number'); | ||
|
||
$this->assertCount(1, DB::getQueryLog()); | ||
$this->assertEquals(10, $model->related1_min_number); | ||
$this->assertEquals(12, $model->related2_min_number); | ||
} | ||
} | ||
|
||
class BaseModel extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
protected $guarded = []; | ||
|
||
public function related1() | ||
{ | ||
return $this->hasMany(Related1::class); | ||
} | ||
|
||
public function related2() | ||
{ | ||
return $this->hasMany(Related2::class); | ||
} | ||
} | ||
|
||
class Related1 extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
protected $fillable = ['base_model_id', 'number']; | ||
|
||
public function parent() | ||
{ | ||
return $this->belongsTo(BaseModel::class); | ||
} | ||
} | ||
|
||
class Related2 extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
protected $fillable = ['base_model_id', 'number']; | ||
|
||
public function parent() | ||
{ | ||
return $this->belongsTo(BaseModel::class); | ||
} | ||
} |