Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Nov 30, 2023
1 parent 2a28291 commit 00f5205
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 231 deletions.
24 changes: 12 additions & 12 deletions tests/Graph/DescendantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ public function testLazyLoadingAndSelfWithCycleDetectionAndStart()

public function testEagerLoading()
{
$nodes = Node::with(['descendants' => function (Descendants $query) {
$query->orderBy('id')->orderBy('depth');
}])->get();
$nodes = Node::with([
'descendants' => fn (Descendants $query) => $query->orderBy('id')->orderBy('depth'),
])->get();

$this->assertEquals(
[2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 8],
Expand Down Expand Up @@ -213,9 +213,9 @@ public function testEagerLoadingAndSelf()
$this->markTestSkipped();
}

$nodes = Node::with(['descendantsAndSelf' => function (Descendants $query) {
$query->orderBy('id')->orderBy('depth');
}])->get();
$nodes = Node::with([
'descendantsAndSelf' => fn (Descendants $query) => $query->orderBy('id')->orderBy('depth'),
])->get();

$this->assertEquals(
[1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 8],
Expand Down Expand Up @@ -285,9 +285,9 @@ public function testEagerLoadingAndSelfWithCycleDetectionAndStart()

public function testLazyEagerLoading()
{
$nodes = Node::all()->load(['descendants' => function (Descendants $query) {
$query->orderBy('id')->orderBy('depth');
}]);
$nodes = Node::all()->load([
'descendants' => fn (Descendants $query) => $query->orderBy('id')->orderBy('depth')
]);

$this->assertEquals(
[2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 8],
Expand Down Expand Up @@ -321,9 +321,9 @@ public function testLazyEagerLoadingAndSelf()
$this->markTestSkipped();
}

$nodes = Node::all()->load(['descendantsAndSelf' => function (Descendants $query) {
$query->orderBy('id')->orderBy('depth');
}]);
$nodes = Node::all()->load([
'descendantsAndSelf' => fn (Descendants $query) => $query->orderBy('id')->orderBy('depth')
]);

$this->assertEquals(
[1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 8],
Expand Down
37 changes: 17 additions & 20 deletions tests/Graph/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public function testScopeSubgraph()
$this->markTestSkipped();
}

$constraint = function (Builder $query) {
$query->whereIn('id', [3, 5]);
};
$constraint = fn (Builder $query) => $query->whereIn('id', [3, 5]);

$graph = Node::subgraph($constraint)->orderBy('id')->get();

Expand All @@ -28,9 +26,7 @@ public function testScopeSubgraphWithMaxDepth()
$this->markTestSkipped();
}

$constraint = function (Builder $query) {
$query->whereIn('id', [3, 5]);
};
$constraint = fn (Builder $query) => $query->whereIn('id', [3, 5]);

$graph = Node::subgraph($constraint, 1)->orderBy('id')->get();

Expand Down Expand Up @@ -125,9 +121,9 @@ public function testScopeDepthFirst()

public function testSetRecursiveQueryConstraint()
{
Node::setRecursiveQueryConstraint(function (Builder $query) {
$query->where('pivot_weight', '<', 7);
});
Node::setRecursiveQueryConstraint(
fn (Builder $query) => $query->where('pivot_weight', '<', 7)
);

$nodes = Node::find(2)->descendants()->orderBy('id')->get();

Expand All @@ -142,11 +138,10 @@ public function testSetRecursiveQueryConstraint()

public function testWithRecursiveQueryConstraint()
{
$nodes = Node::withRecursiveQueryConstraint(function (Builder $query) {
$query->where('pivot_weight', '<', 7);
}, function () {
return Node::find(2)->descendants()->orderBy('id')->get();
});
$nodes = Node::withRecursiveQueryConstraint(
fn (Builder $query) => $query->where('pivot_weight', '<', 7),
fn () => Node::find(2)->descendants()->orderBy('id')->get()
);

$this->assertEquals([5, 7, 8], $nodes->pluck('id')->all());

Expand All @@ -157,18 +152,20 @@ public function testWithRecursiveQueryConstraint()

public function testWithMaxDepth()
{
$nodes = Node::withMaxDepth(2, function () {
return Node::find(2)->descendants;
});
$nodes = Node::withMaxDepth(
2,
fn () => Node::find(2)->descendants
);

$this->assertEquals([5, 7, 8], $nodes->pluck('id')->all());
}

public function testWithMaxDepthWithNegativeDepth()
{
$nodes = Node::withMaxDepth(-1, function () {
return Node::find(5)->ancestors;
});
$nodes = Node::withMaxDepth(
-1,
fn () => Node::find(5)->ancestors
);

$this->assertEquals([1, 2, 10], $nodes->pluck('id')->all());
}
Expand Down
37 changes: 20 additions & 17 deletions tests/Graph/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Staudenmeir\LaravelAdjacencyList\Tests\Graph;

use Carbon\Carbon;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use PHPUnit\Framework\TestCase as Base;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase as Base;
use Staudenmeir\LaravelAdjacencyList\Tests\Graph\Models\Node;
use Staudenmeir\LaravelAdjacencyList\Tests\Graph\Models\NodeWithCycleDetection;
use Staudenmeir\LaravelAdjacencyList\Tests\Graph\Models\NodeWithCycleDetectionAndStart;
Expand All @@ -19,20 +20,13 @@ abstract class TestCase extends Base

protected function setUp(): void
{
parent::setUp();

$this->database = getenv('DATABASE') ?: 'sqlite';

$config = require __DIR__.'/../config/database.php';

$db = new DB();
$db->addConnection($config[$this->database]);
$db->setAsGlobal();
$db->bootEloquent();
parent::setUp();

$this->migrate();
$this->migrateDatabase();

$this->seed();
$this->seedDatabase();
}

protected function tearDown(): void
Expand All @@ -42,11 +36,11 @@ protected function tearDown(): void
parent::tearDown();
}

protected function migrate(): void
protected function migrateDatabase(): void
{
DB::schema()->dropAllTables();
Schema::dropAllTables();

DB::schema()->create(
Schema::create(
'nodes',
function (Blueprint $table) {
$table->id();
Expand All @@ -57,7 +51,7 @@ function (Blueprint $table) {
}
);

DB::schema()->create(
Schema::create(
'edges',
function (Blueprint $table) {
$table->unsignedBigInteger('parent_id');
Expand All @@ -72,7 +66,7 @@ function (Blueprint $table) {
);
}

protected function seed(): void
protected function seedDatabase(): void
{
Carbon::setTestNow(
Carbon::now()->roundSecond()
Expand Down Expand Up @@ -289,4 +283,13 @@ public static function cycleDetectionAndStartClassProvider(): array
[NodeWithUuidAndCycleDetectionAndStart::class, ['sqlsrv']],
];
}

protected function getEnvironmentSetUp($app)
{
$config = require __DIR__.'/../config/database.php';

$app['config']->set('database.default', 'testing');

$app['config']->set('database.connections.testing', $config[$this->database]);
}
}
55 changes: 32 additions & 23 deletions tests/Tree/AncestorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Staudenmeir\LaravelAdjacencyList\Tests\Tree;

use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors;
use Staudenmeir\LaravelAdjacencyList\Tests\Tree\Models\User;

class AncestorsTest extends TestCase
Expand Down Expand Up @@ -29,13 +30,13 @@ public function testLazyLoadingWithRoot()

public function testLazyLoadingAndSelf()
{
$ancestorsAndSelf = User::find(8)->ancestorsAndSelf;
$ancestorsAndSelf = User::find(8)->ancestorsAndSelf()->orderBy('id')->get();

$this->assertEquals([8, 5, 2, 1], $ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([0, -1, -2, -3], $ancestorsAndSelf->pluck('depth')->all());
$this->assertEquals(['8', '8.5', '8.5.2', '8.5.2.1'], $ancestorsAndSelf->pluck('path')->all());
$this->assertEquals([1, 2, 5, 8], $ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([-3, -2, -1, 0], $ancestorsAndSelf->pluck('depth')->all());
$this->assertEquals(['8.5.2.1', '8.5.2', '8.5', '8', ], $ancestorsAndSelf->pluck('path')->all());
$this->assertEquals(
['user-8', 'user-8/user-5', 'user-8/user-5/user-2', 'user-8/user-5/user-2/user-1'],
['user-8/user-5/user-2/user-1', 'user-8/user-5/user-2', 'user-8/user-5', 'user-8'],
$ancestorsAndSelf->pluck('slug_path')->all()
);
}
Expand All @@ -49,46 +50,54 @@ public function testLazyLoadingAndSelfWithRoot()

public function testEagerLoading()
{
$users = User::with('ancestors')->get();
$users = User::with([
'ancestors' => fn (Ancestors $query) => $query->orderBy('id'),
])->orderBy('id')->get();

$this->assertEquals([], $users[0]->ancestors->pluck('id')->all());
$this->assertEquals([1], $users[1]->ancestors->pluck('id')->all());
$this->assertEquals([5, 2, 1], $users[7]->ancestors->pluck('id')->all());
$this->assertEquals([-1, -2, -3], $users[7]->ancestors->pluck('depth')->all());
$this->assertEquals(['5', '5.2', '5.2.1'], $users[7]->ancestors->pluck('path')->all());
$this->assertEquals([1, 2, 5], $users[7]->ancestors->pluck('id')->all());
$this->assertEquals([-3, -2, -1], $users[7]->ancestors->pluck('depth')->all());
$this->assertEquals(['5.2.1', '5.2', '5'], $users[7]->ancestors->pluck('path')->all());
}

public function testEagerLoadingAndSelf()
{
$users = User::with('ancestorsAndSelf')->get();
$users = User::with([
'ancestorsAndSelf' => fn (Ancestors $query) => $query->orderBy('id'),
])->orderBy('id')->get();

$this->assertEquals([1], $users[0]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([2, 1], $users[1]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([8, 5, 2, 1], $users[7]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([0, -1, -2, -3], $users[7]->ancestorsAndSelf->pluck('depth')->all());
$this->assertEquals(['8', '8.5', '8.5.2', '8.5.2.1'], $users[7]->ancestorsAndSelf->pluck('path')->all());
$this->assertEquals([1, 2], $users[1]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([1, 2, 5, 8], $users[7]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([-3, -2, -1, 0], $users[7]->ancestorsAndSelf->pluck('depth')->all());
$this->assertEquals(['8.5.2.1', '8.5.2', '8.5', '8'], $users[7]->ancestorsAndSelf->pluck('path')->all());
}

public function testLazyEagerLoading()
{
$users = User::all()->load('ancestors');
$users = User::orderBy('id')->get()->load([
'ancestors' => fn (Ancestors $query) => $query->orderBy('id'),
]);

$this->assertEquals([], $users[0]->ancestors->pluck('id')->all());
$this->assertEquals([1], $users[1]->ancestors->pluck('id')->all());
$this->assertEquals([5, 2, 1], $users[7]->ancestors->pluck('id')->all());
$this->assertEquals([-1, -2, -3], $users[7]->ancestors->pluck('depth')->all());
$this->assertEquals(['5', '5.2', '5.2.1'], $users[7]->ancestors->pluck('path')->all());
$this->assertEquals([1, 2, 5], $users[7]->ancestors->pluck('id')->all());
$this->assertEquals([-3, -2, -1], $users[7]->ancestors->pluck('depth')->all());
$this->assertEquals(['5.2.1', '5.2', '5'], $users[7]->ancestors->pluck('path')->all());
}

public function testLazyEagerLoadingAndSelf()
{
$users = User::all()->load('ancestorsAndSelf');
$users = User::orderBy('id')->get()->load([
'ancestorsAndSelf' => fn (Ancestors $query) => $query->orderBy('id'),
]);

$this->assertEquals([1], $users[0]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([2, 1], $users[1]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([8, 5, 2, 1], $users[7]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([0, -1, -2, -3], $users[7]->ancestorsAndSelf->pluck('depth')->all());
$this->assertEquals(['8', '8.5', '8.5.2', '8.5.2.1'], $users[7]->ancestorsAndSelf->pluck('path')->all());
$this->assertEquals([1, 2], $users[1]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([1, 2, 5, 8], $users[7]->ancestorsAndSelf->pluck('id')->all());
$this->assertEquals([-3, -2, -1, 0], $users[7]->ancestorsAndSelf->pluck('depth')->all());
$this->assertEquals(['8.5.2.1', '8.5.2', '8.5', '8'], $users[7]->ancestorsAndSelf->pluck('path')->all());
}

public function testExistenceQuery()
Expand Down
24 changes: 12 additions & 12 deletions tests/Tree/BelongsToManyOfDescendantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function testLazyLoadingAndSelf()

public function testEagerLoading()
{
$users = User::with(['roles' => function (BelongsToManyOfDescendants $query) {
$query->orderBy('id');
}])->get();
$users = User::with([
'roles' => fn (BelongsToManyOfDescendants $query) => $query->orderBy('id'),
])->orderBy('id')->get();

$this->assertEquals([21, 31, 41, 51, 61, 71, 81], $users[0]->roles->pluck('id')->all());
$this->assertEquals([51, 81], $users[1]->roles->pluck('id')->all());
Expand All @@ -39,9 +39,9 @@ public function testEagerLoading()

public function testEagerLoadingAndSelf()
{
$users = User::with(['rolesAndSelf' => function (BelongsToManyOfDescendants $query) {
$query->orderBy('id');
}])->get();
$users = User::with([
'rolesAndSelf' => fn (BelongsToManyOfDescendants $query) => $query->orderBy('id'),
])->orderBy('id')->get();

$this->assertEquals([11, 21, 31, 41, 51, 61, 71, 81], $users[0]->rolesAndSelf->pluck('id')->all());
$this->assertEquals([21, 51, 81], $users[1]->rolesAndSelf->pluck('id')->all());
Expand All @@ -52,9 +52,9 @@ public function testEagerLoadingAndSelf()

public function testLazyEagerLoading()
{
$users = User::all()->load(['roles' => function (BelongsToManyOfDescendants $query) {
$query->orderBy('id');
}]);
$users = User::orderBy('id')->get()->load([
'roles' => fn (BelongsToManyOfDescendants $query) => $query->orderBy('id'),
]);

$this->assertEquals([21, 31, 41, 51, 61, 71, 81], $users[0]->roles->pluck('id')->all());
$this->assertEquals([51, 81], $users[1]->roles->pluck('id')->all());
Expand All @@ -65,9 +65,9 @@ public function testLazyEagerLoading()

public function testLazyEagerLoadingAndSelf()
{
$users = User::all()->load(['rolesAndSelf' => function (BelongsToManyOfDescendants $query) {
$query->orderBy('id');
}]);
$users = User::orderBy('id')->get()->load([
'rolesAndSelf' => fn (BelongsToManyOfDescendants $query) => $query->orderBy('id'),
]);

$this->assertEquals([11, 21, 31, 41, 51, 61, 71, 81], $users[0]->rolesAndSelf->pluck('id')->all());
$this->assertEquals([21, 51, 81], $users[1]->rolesAndSelf->pluck('id')->all());
Expand Down
Loading

0 comments on commit 00f5205

Please sign in to comment.