-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
bce0d20
commit 2b2ec91
Showing
3 changed files
with
330 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\EloquentJsonRelations; | ||
|
||
class JsonKey | ||
{ | ||
public function __construct(protected string $column) | ||
{ | ||
// | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->column; | ||
} | ||
} |
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,173 @@ | ||
<?php | ||
|
||
namespace Tests\Concatenation; | ||
|
||
use Illuminate\Database\Capsule\Manager as DB; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Relations\Pivot; | ||
use Tests\Models\Role; | ||
use Tests\Models\Project; | ||
use Tests\TestCase; | ||
|
||
class HasManyThroughJsonTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if (DB::connection()->getDriverName() === 'sqlite') { | ||
$this->markTestSkipped(); | ||
} | ||
} | ||
|
||
public function testLazyLoading() | ||
{ | ||
$projects = Role::find(2)->projects; | ||
|
||
$this->assertEquals([71, 73], $projects->pluck('id')->all()); | ||
} | ||
|
||
public function testLazyLoadingWithObjects() | ||
{ | ||
if (DB::connection()->getDriverName() === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$projects = Role::find(2)->projects2; | ||
|
||
$this->assertEquals([71, 73], $projects->pluck('id')->all()); | ||
$pivot = $projects[0]->pivot; | ||
$this->assertInstanceOf(Pivot::class, $pivot); | ||
$this->assertTrue($pivot->exists); | ||
$this->assertEquals(['role' => ['active' => false]], $pivot->getAttributes()); | ||
} | ||
|
||
public function testLazyLoadingWithReverseRelationship() | ||
{ | ||
$roles = Project::find(71)->roles; | ||
|
||
$this->assertEquals([1, 2], $roles->pluck('id')->all()); | ||
} | ||
|
||
public function testLazyLoadingWithReverseRelationshipAndObjects() | ||
{ | ||
if (DB::connection()->getDriverName() === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$roles = Project::find(71)->roles2; | ||
|
||
$this->assertEquals([1, 2], $roles->pluck('id')->all()); | ||
$pivot = $roles[0]->pivot; | ||
$this->assertInstanceOf(Pivot::class, $pivot); | ||
$this->assertTrue($pivot->exists); | ||
$this->assertEquals(['role' => ['active' => true]], $pivot->getAttributes()); | ||
} | ||
|
||
public function testEmptyLazyLoading() | ||
{ | ||
$projects = (new Role())->projects()->get(); | ||
|
||
$this->assertEmpty($projects); | ||
} | ||
|
||
public function testEmptyLazyLoadingWithReverseRelationship() | ||
{ | ||
DB::enableQueryLog(); | ||
|
||
$roles = (new Project())->roles; | ||
|
||
$this->assertInstanceOf(Collection::class, $roles); | ||
$this->assertEmpty(DB::getQueryLog()); | ||
} | ||
|
||
public function testEagerLoading() | ||
{ | ||
$roles = Role::with('projects')->get(); | ||
|
||
$this->assertEquals([71], $roles[0]->projects->pluck('id')->all()); | ||
$this->assertEquals([71, 73], $roles[1]->projects->pluck('id')->all()); | ||
$this->assertEquals([73], $roles[2]->projects->pluck('id')->all()); | ||
$this->assertEquals([], $roles[3]->projects->pluck('id')->all()); | ||
} | ||
|
||
public function testEagerLoadingWithObjects() | ||
{ | ||
if (DB::connection()->getDriverName() === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$roles = Role::with('projects2')->get(); | ||
|
||
$this->assertEquals([71], $roles[0]->projects2->pluck('id')->all()); | ||
$this->assertEquals([71, 73], $roles[1]->projects2->pluck('id')->all()); | ||
$this->assertEquals([73], $roles[2]->projects2->pluck('id')->all()); | ||
$this->assertEquals([], $roles[3]->projects2->pluck('id')->all()); | ||
$pivot = $roles[1]->projects2[0]->pivot; | ||
$this->assertInstanceOf(Pivot::class, $pivot); | ||
$this->assertTrue($pivot->exists); | ||
$this->assertEquals(['role' => ['active' => false]], $pivot->getAttributes()); | ||
} | ||
|
||
public function testEagerLoadingWithReverseRelationship() | ||
{ | ||
$projects = Project::with('roles')->get(); | ||
|
||
$this->assertEquals([1, 2], $projects[0]->roles->pluck('id')->all()); | ||
$this->assertEquals([], $projects[1]->roles->pluck('id')->all()); | ||
$this->assertEquals([2, 3], $projects[2]->roles->pluck('id')->all()); | ||
} | ||
|
||
public function testEagerLoadingWithReverseRelationshipAndObjects() | ||
{ | ||
if (DB::connection()->getDriverName() === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$projects = Project::with('roles2')->get(); | ||
|
||
$this->assertEquals([1, 2], $projects[0]->roles2->pluck('id')->all()); | ||
$this->assertEquals([], $projects[1]->roles2->pluck('id')->all()); | ||
$this->assertEquals([2, 3], $projects[2]->roles2->pluck('id')->all()); | ||
$pivot = $projects[0]->roles2[0]->pivot; | ||
$this->assertInstanceOf(Pivot::class, $pivot); | ||
$this->assertTrue($pivot->exists); | ||
$this->assertEquals(['role' => ['active' => true]], $pivot->getAttributes()); | ||
} | ||
|
||
public function testExistenceQuery() | ||
{ | ||
$roles = Role::has('projects')->get(); | ||
|
||
$this->assertEquals([1, 2, 3], $roles->pluck('id')->all()); | ||
} | ||
|
||
public function testExistenceQueryWithObjects() | ||
{ | ||
if (DB::connection()->getDriverName() === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$roles = Role::has('projects2')->get(); | ||
|
||
$this->assertEquals([1, 2, 3], $roles->pluck('id')->all()); | ||
} | ||
|
||
public function testExistenceQueryWithReverseRelationship() | ||
{ | ||
$projects = Project::has('roles')->get(); | ||
|
||
$this->assertEquals([71, 73], $projects->pluck('id')->all()); | ||
} | ||
|
||
public function testExistenceQueryWithReverseRelationshipAndObjects() | ||
{ | ||
if (DB::connection()->getDriverName() === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$projects = Project::has('roles2')->get(); | ||
|
||
$this->assertEquals([71, 73], $projects->pluck('id')->all()); | ||
} | ||
} |