Skip to content
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

[3.9] Fix/stringable sort #2420

Merged
merged 7 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public function orderBy($column, $direction = 'asc')
if ($column == 'natural') {
$this->orders['$natural'] = $direction;
} else {
$this->orders[$column] = $direction;
$this->orders[(string) $column] = $direction;
}

return $this;
Expand Down
28 changes: 28 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ public function testOrder(): void
$this->assertEquals(35, $user->age);
}

public function testStringableOrder(): void
{
$age = new stringableObject('age');
apeisa marked this conversation as resolved.
Show resolved Hide resolved

$user = User::whereNotNull('age')->orderBy($age, 'asc')->first();
$this->assertEquals(13, $user->age);

$user = User::whereNotNull('age')->orderBy($age, 'desc')->first();
$this->assertEquals(37, $user->age);
}

public function testGroupBy(): void
{
$users = User::groupBy('title')->get();
Expand Down Expand Up @@ -470,3 +481,20 @@ public function testMultipleSortOrder(): void
$this->assertEquals('Brett Boe', $subset[2]->name);
}
}

/**
* Mockup class to test stringable objects.
*/
class stringableObject implements Stringable {
private string $string;

public function __construct(string $string)
{
$this->string = $string;
}

public function __toString()
{
return $this->string;
}
}
apeisa marked this conversation as resolved.
Show resolved Hide resolved