Skip to content

make sure $column is string #2593

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
Sep 5, 2023
Merged

make sure $column is string #2593

merged 1 commit into from
Sep 5, 2023

Conversation

wivaku
Copy link
Contributor

@wivaku wivaku commented Sep 1, 2023

Native Eloquent allows $column to be stringable(). Not possible when using as array key.

Is issue when using e.g. Filament column search.

@GromNaN
Copy link
Member

GromNaN commented Sep 1, 2023

Hello @wivaku, thanks for your contribution. Would you like to add a test for this feature to avoid regression.

@wivaku
Copy link
Contributor Author

wivaku commented Sep 2, 2023

Hi @GromNaN , you're welcome. Do you feel a test is needed for casting to a string?
The result should be a string, and now we're making sure of it.

The casting is similar to other places in the code, e.g.

$this->orders[(string) $column] = $direction;

@GromNaN
Copy link
Member

GromNaN commented Sep 2, 2023

This is a feature and we need to be sure this feature keeps working when we work on the codebase. It's not about testing $column is stringified, but testing that other types are accepted. The error could happen somewhere else.
So yes, a test is necessary.

@wivaku
Copy link
Contributor Author

wivaku commented Sep 2, 2023

I don't mind adding a test, but it seems I am not exactly sure what + how.

I tried running the tests using docker compose up, but the tests container exits with:

tests    | /usr/local/bin/docker-php-entrypoint: 9: exec: ./vendor/bin/phpunit: not found
tests exited with code 127

This is the test that could be added to tests/QueryBuilderTest.php:

use Illuminate\Support\Str;

// ...

    public function testStringableColumn()
    {
        DB::collection('users')->insert([
            ['name' => 'Jane Doe'],
            ['name' => 'John Doe'],
        ]);

        $stringableColumn = Str::of('name');
        $this->assertEquals('John Doe', DB::collection('users')->where($stringableColumn, 'John Doe')->first());
    }

Native Eloquent allows $column to be stringable(). Not possible when using as array key.
@GromNaN
Copy link
Member

GromNaN commented Sep 4, 2023

Thank you @wivaku, I added more tests and refactored the change so every operations have a stringified $column name.

@GromNaN GromNaN requested review from jmikola and alcaeus September 4, 2023 08:46
@@ -1004,8 +1004,14 @@ protected function compileWheres(): array
$where['boolean'] = 'or'.(str_ends_with($where['boolean'], 'not') ? ' not' : '');
}

// Column name can be a Stringable object.
if (isset($where['column']) && $where['column'] instanceof \Stringable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the check for Stringable here but not in other places? Is there a reason why we can't always cast to string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed a if statement to check if column is set, so I added the instanceof to avoid reassigning if the value is already a string. In other places, we know $column is set and we are not assigning the casted value.

@GromNaN GromNaN merged commit 86aa9c7 into mongodb:master Sep 5, 2023
@GromNaN
Copy link
Member

GromNaN commented Sep 5, 2023

Thank you @wivaku

@wivaku wivaku deleted the patch-1 branch September 5, 2023 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants