Skip to content

tests: for sorting #2039

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

Merged
merged 1 commit into from
May 29, 2020
Merged
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
47 changes: 47 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,51 @@ public function testUpdate(): void
$this->assertEquals(2, Scoped::withoutGlobalScopes()->update(['name' => 'Jimmy']));
$this->assertCount(2, Scoped::withoutGlobalScopes()->where(['name' => 'Jimmy'])->get());
}

public function testUnsorted(): void
{
$unsortedResults = User::get();

$unsortedSubset = $unsortedResults->where('age', 35)->values();

$this->assertEquals('John Doe', $unsortedSubset[0]->name);
$this->assertEquals('Brett Boe', $unsortedSubset[1]->name);
$this->assertEquals('Yvonne Yoe', $unsortedSubset[2]->name);
}

public function testSort(): void
{
$results = User::orderBy('age')->get();

$this->assertEquals($results->sortBy('age')->pluck('age')->all(), $results->pluck('age')->all());
}

public function testSortOrder(): void
{
$results = User::orderBy('age', 'desc')->get();

$this->assertEquals($results->sortByDesc('age')->pluck('age')->all(), $results->pluck('age')->all());
}

public function testMultipleSort(): void
{
$results = User::orderBy('age')->orderBy('name')->get();

$subset = $results->where('age', 35)->values();

$this->assertEquals('Brett Boe', $subset[0]->name);
$this->assertEquals('John Doe', $subset[1]->name);
$this->assertEquals('Yvonne Yoe', $subset[2]->name);
}

public function testMultipleSortOrder(): void
{
$results = User::orderBy('age')->orderBy('name', 'desc')->get();

$subset = $results->where('age', 35)->values();

$this->assertEquals('Yvonne Yoe', $subset[0]->name);
$this->assertEquals('John Doe', $subset[1]->name);
$this->assertEquals('Brett Boe', $subset[2]->name);
}
}