-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
0345b92
commit 76823e6
Showing
5 changed files
with
194 additions
and
10 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
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,38 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Bloodline extends Descendants | ||
{ | ||
/** | ||
* Create a new bloodline relationship instance. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param \Illuminate\Database\Eloquent\Model $parent | ||
* @param string $foreignKey | ||
* @param string $localKey | ||
* @return void | ||
*/ | ||
public function __construct(Builder $query, Model $parent, $foreignKey, $localKey) | ||
{ | ||
parent::__construct($query, $parent, $foreignKey, $localKey, true); | ||
} | ||
|
||
/** | ||
* Add a recursive expression to the query. | ||
* | ||
* @param callable $constraint | ||
* @param \Illuminate\Database\Eloquent\Builder|null $query | ||
* @param string|null $from | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
protected function addExpression(callable $constraint, Builder $query = null, $from = null) | ||
{ | ||
$query = $query ?: $this->query; | ||
|
||
return $query->withRelationshipExpression('both', $constraint, 0, $from); | ||
} | ||
} |
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,86 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Illuminate\Database\Capsule\Manager as DB; | ||
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline; | ||
use Tests\Models\User; | ||
|
||
class BloodlineTest extends TestCase | ||
{ | ||
public function testLazyLoading() | ||
{ | ||
$bloodline = User::find(5)->bloodline()->breadthFirst()->get(); | ||
|
||
$this->assertEquals([1, 2, 5, 8], $bloodline->pluck('id')->all()); | ||
$this->assertEquals([-2, -1, 0, 1], $bloodline->pluck('depth')->all()); | ||
$this->assertEquals(['5.2.1', '5.2', '5', '5.8'], $bloodline->pluck('path')->all()); | ||
$this->assertEquals(['user-5/user-2/user-1', 'user-5/user-2', 'user-5', 'user-5/user-8'], $bloodline->pluck('slug_path')->all()); | ||
} | ||
|
||
public function testEagerLoading() | ||
{ | ||
$users = User::with( | ||
[ | ||
'bloodline' => function (Bloodline $relation) { | ||
$relation->breadthFirst()->orderBy('id'); | ||
}, | ||
] | ||
)->get(); | ||
|
||
$this->assertEquals(range(1, 9), $users[0]->bloodline->pluck('id')->all()); | ||
$this->assertEquals([1, 2, 5, 8], $users[1]->bloodline->pluck('id')->all()); | ||
$this->assertEquals([1, 2, 5, 8], $users[4]->bloodline->pluck('id')->all()); | ||
$this->assertEquals(['5.2.1', '5.2', '5', '5.8'], $users[4]->bloodline->pluck('path')->all()); | ||
$this->assertEquals(['user-5/user-2/user-1', 'user-5/user-2', 'user-5', 'user-5/user-8'], $users[4]->bloodline->pluck('slug_path')->all()); | ||
} | ||
|
||
public function testLazyEagerLoading() | ||
{ | ||
$users = User::all()->load( | ||
[ | ||
'bloodline' => function (Bloodline $relation) { | ||
$relation->breadthFirst()->orderBy('id'); | ||
}, | ||
] | ||
); | ||
|
||
$this->assertEquals(range(1, 9), $users[0]->bloodline->pluck('id')->all()); | ||
$this->assertEquals([1, 2, 5, 8], $users[1]->bloodline->pluck('id')->all()); | ||
$this->assertEquals([1, 2, 5, 8], $users[4]->bloodline->pluck('id')->all()); | ||
$this->assertEquals(['5.2.1', '5.2', '5', '5.8'], $users[4]->bloodline->pluck('path')->all()); | ||
$this->assertEquals(['user-5/user-2/user-1', 'user-5/user-2', 'user-5', 'user-5/user-8'], $users[4]->bloodline->pluck('slug_path')->all()); | ||
} | ||
|
||
public function testExistenceQuery() | ||
{ | ||
if (DB::connection()->getDriverName() === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$descendants = User::first()->descendants()->has('bloodline', '<', 4)->get(); | ||
|
||
$this->assertEquals([4, 7], $descendants->pluck('id')->all()); | ||
} | ||
|
||
public function testExistenceQueryForSelfRelation() | ||
{ | ||
if (DB::connection()->getDriverName() === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$users = User::has('bloodline', '<', 4)->get(); | ||
|
||
$this->assertEquals([4, 7, 11, 12], $users->pluck('id')->all()); | ||
} | ||
|
||
public function testUpdate() | ||
{ | ||
$affected = User::find(5)->bloodline()->delete(); | ||
|
||
$this->assertEquals(4, $affected); | ||
$this->assertNotNull(User::withTrashed()->find(1)->deleted_at); | ||
$this->assertNotNull(User::withTrashed()->find(8)->deleted_at); | ||
$this->assertNull(User::find(3)->deleted_at); | ||
} | ||
} |