-
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.
Fixes missing
lazy()
and lazyById()
on BelongsToMany and HasManyT…
…hrough relation query builder. Signed-off-by: Mior Muhammad Zaki <[email protected]>
- Loading branch information
Showing
5 changed files
with
278 additions
and
7 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
134 changes: 134 additions & 0 deletions
134
tests/Database/DatabaseEloquentBelongsToManyLazyByIdTest.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,134 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Database; | ||
|
||
use Illuminate\Database\Capsule\Manager as DB; | ||
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DatabaseEloquentBelongsToManyLazyByIdTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$db = new DB; | ||
|
||
$db->addConnection([ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
]); | ||
|
||
$db->bootEloquent(); | ||
$db->setAsGlobal(); | ||
|
||
$this->createSchema(); | ||
} | ||
|
||
/** | ||
* Setup the database schema. | ||
* | ||
* @return void | ||
*/ | ||
public function createSchema() | ||
{ | ||
$this->schema()->create('users', function ($table) { | ||
$table->increments('id'); | ||
$table->string('email')->unique(); | ||
}); | ||
|
||
$this->schema()->create('articles', function ($table) { | ||
$table->increments('aid'); | ||
$table->string('title'); | ||
}); | ||
|
||
$this->schema()->create('article_user', function ($table) { | ||
$table->integer('article_id')->unsigned(); | ||
$table->foreign('article_id')->references('aid')->on('articles'); | ||
$table->integer('user_id')->unsigned(); | ||
$table->foreign('user_id')->references('id')->on('users'); | ||
}); | ||
} | ||
|
||
public function testBelongsToLazyById() | ||
{ | ||
$this->seedData(); | ||
|
||
$user = BelongsToManyLazyByIdTestTestUser::query()->first(); | ||
$i = 0; | ||
|
||
$user->articles()->lazyById(1)->each(function ($model) use (&$i) { | ||
$i++; | ||
$this->assertEquals($i, $model->aid); | ||
}); | ||
|
||
$this->assertSame(3, $i); | ||
} | ||
|
||
/** | ||
* Tear down the database schema. | ||
* | ||
* @return void | ||
*/ | ||
protected function tearDown(): void | ||
{ | ||
$this->schema()->drop('users'); | ||
$this->schema()->drop('articles'); | ||
$this->schema()->drop('article_user'); | ||
} | ||
|
||
/** | ||
* Helpers... | ||
*/ | ||
protected function seedData() | ||
{ | ||
$user = BelongsToManyLazyByIdTestTestUser::create(['id' => 1, 'email' => '[email protected]']); | ||
BelongsToManyLazyByIdTestTestArticle::query()->insert([ | ||
['aid' => 1, 'title' => 'Another title'], | ||
['aid' => 2, 'title' => 'Another title'], | ||
['aid' => 3, 'title' => 'Another title'], | ||
]); | ||
|
||
$user->articles()->sync([3, 1, 2]); | ||
} | ||
|
||
/** | ||
* Get a database connection instance. | ||
* | ||
* @return \Illuminate\Database\ConnectionInterface | ||
*/ | ||
protected function connection() | ||
{ | ||
return Eloquent::getConnectionResolver()->connection(); | ||
} | ||
|
||
/** | ||
* Get a schema builder instance. | ||
* | ||
* @return \Illuminate\Database\Schema\Builder | ||
*/ | ||
protected function schema() | ||
{ | ||
return $this->connection()->getSchemaBuilder(); | ||
} | ||
} | ||
|
||
class BelongsToManyLazyByIdTestTestUser extends Eloquent | ||
{ | ||
protected $table = 'users'; | ||
protected $fillable = ['id', 'email']; | ||
public $timestamps = false; | ||
|
||
public function articles() | ||
{ | ||
return $this->belongsToMany(BelongsToManyLazyByIdTestTestArticle::class, 'article_user', 'user_id', 'article_id'); | ||
} | ||
} | ||
|
||
class BelongsToManyLazyByIdTestTestArticle extends Eloquent | ||
{ | ||
protected $primaryKey = 'aid'; | ||
protected $table = 'articles'; | ||
protected $keyType = 'string'; | ||
public $incrementing = false; | ||
public $timestamps = false; | ||
protected $fillable = ['aid', 'title']; | ||
} |
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