diff --git a/tests/Integration/Database/EloquentModelLoadMaxTest.php b/tests/Integration/Database/EloquentModelLoadMaxTest.php new file mode 100644 index 000000000000..cf6a5d7bd8c7 --- /dev/null +++ b/tests/Integration/Database/EloquentModelLoadMaxTest.php @@ -0,0 +1,104 @@ +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 testLoadMaxSingleRelation() + { + $model = BaseModel::first(); + + DB::enableQueryLog(); + + $model->loadMax('related1', 'number'); + + $this->assertCount(1, DB::getQueryLog()); + $this->assertEquals(11, $model->related1_max_number); + } + + public function testLoadMaxMultipleRelations() + { + $model = BaseModel::first(); + + DB::enableQueryLog(); + + $model->loadMax(['related1', 'related2'], 'number'); + + $this->assertCount(1, DB::getQueryLog()); + $this->assertEquals(11, $model->related1_max_number); + $this->assertEquals(13, $model->related2_max_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); + } +} diff --git a/tests/Integration/Database/EloquentModelLoadMinTest.php b/tests/Integration/Database/EloquentModelLoadMinTest.php new file mode 100644 index 000000000000..b63e2ec2dc47 --- /dev/null +++ b/tests/Integration/Database/EloquentModelLoadMinTest.php @@ -0,0 +1,104 @@ +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); + } +} diff --git a/tests/Integration/Database/EloquentModelLoadSumTest.php b/tests/Integration/Database/EloquentModelLoadSumTest.php new file mode 100644 index 000000000000..0e4e5fa84b8d --- /dev/null +++ b/tests/Integration/Database/EloquentModelLoadSumTest.php @@ -0,0 +1,103 @@ +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]); + } + + public function testLoadSumSingleRelation() + { + $model = BaseModel::first(); + + DB::enableQueryLog(); + + $model->loadSum('related1', 'number'); + + $this->assertCount(1, DB::getQueryLog()); + $this->assertEquals(21, $model->related1_sum_number); + } + + public function testLoadSumMultipleRelations() + { + $model = BaseModel::first(); + + DB::enableQueryLog(); + + $model->loadSum(['related1', 'related2'], 'number'); + + $this->assertCount(1, DB::getQueryLog()); + $this->assertEquals(21, $model->related1_sum_number); + $this->assertEquals(12, $model->related2_sum_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); + } +}