Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add tests for Eloquent methods #49673

Merged
merged 3 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions tests/Integration/Database/EloquentModelLoadMaxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentModelLoadMaxTest;

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 EloquentModelLoadMaxTest 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 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);
}
}
104 changes: 104 additions & 0 deletions tests/Integration/Database/EloquentModelLoadMinTest.php
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);
}
}
103 changes: 103 additions & 0 deletions tests/Integration/Database/EloquentModelLoadSumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentModelLoadSumTest;

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 EloquentModelLoadSumTest 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]);
}

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);
}
}