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

[10.x] fix(Eloquent/Builder): calling the methods on passthru base object should be case-insensitive #48852

Merged
Show file tree
Hide file tree
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
30 changes: 15 additions & 15 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,29 @@ class Builder implements BuilderContract
'avg',
'count',
'dd',
'ddRawSql',
'doesntExist',
'doesntExistOr',
'ddrawsql',
'doesntexist',
'doesntexistor',
'dump',
'dumpRawSql',
'dumprawsql',
'exists',
'existsOr',
'existsor',
'explain',
'getBindings',
'getConnection',
'getGrammar',
'getbindings',
'getconnection',
'getgrammar',
'implode',
'insert',
'insertGetId',
'insertOrIgnore',
'insertUsing',
'insertgetid',
'insertorignore',
'insertusing',
'max',
'min',
'raw',
'rawValue',
'rawvalue',
'sum',
'toSql',
'toRawSql',
'tosql',
'torawsql',
];

/**
Expand Down Expand Up @@ -1964,7 +1964,7 @@ public function __call($method, $parameters)
return $this->callNamedScope($method, $parameters);
}

if (in_array($method, $this->passthru)) {
if (in_array(strtolower($method), $this->passthru)) {
return $this->toBase()->{$method}(...$parameters);
}

Expand Down
42 changes: 42 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,48 @@ public function testToRawSql()
$this->assertSame('select * from "users" where "email" = \'foo\'', $builder->toRawSql());
}

public function testPassthruMethodsCallsAreNotCaseSensitive()
{
$query = m::mock(BaseBuilder::class);

$mockResponse = 'select 1';
$query
->shouldReceive('toRawSql')
->andReturn($mockResponse)
->times(3);

$builder = new Builder($query);

$this->assertSame('select 1', $builder->TORAWSQL());
$this->assertSame('select 1', $builder->toRawSql());
$this->assertSame('select 1', $builder->toRawSQL());
}

public function testPassthruArrayElementsMustAllBeLowercase()
{
$builder = new class(m::mock(BaseBuilder::class)) extends Builder
{
// expose protected member for test
public function getPassthru(): array
{
return $this->passthru;
}
};

$passthru = $builder->getPassthru();

foreach ($passthru as $method) {
$lowercaseMethod = strtolower($method);

$this->assertSame(
$lowercaseMethod,
$method,
'Eloquent\\Builder relies on lowercase method names in $passthru array to correctly mimic PHP case-insensitivity on method dispatch.'.
'If you are adding a new method to the $passthru array, make sure the name is lowercased.'
);
}
}

protected function mockConnectionForModel($model, $database)
{
$grammarClass = 'Illuminate\Database\Query\Grammars\\'.$database.'Grammar';
Expand Down