Skip to content

Commit 6290de8

Browse files
committed
fix(Eloquent/Builder): calling the methods on passthru base object should be case-insensitive
Fixes: #48825
1 parent 85a5146 commit 6290de8

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class Builder implements BuilderContract
8989
/**
9090
* The methods that should be returned from query builder.
9191
*
92+
* Note that the names in the array need to be lowercase so that PHP's case-insensitivity
93+
* regarding method calls can be achieved when forwarding the call to the base object.
94+
*
9295
* @var string[]
9396
*/
9497
protected $passthru = [
@@ -97,29 +100,29 @@ class Builder implements BuilderContract
97100
'avg',
98101
'count',
99102
'dd',
100-
'ddRawSql',
101-
'doesntExist',
102-
'doesntExistOr',
103+
'ddrawsql',
104+
'doesntexist',
105+
'doesntexistor',
103106
'dump',
104-
'dumpRawSql',
107+
'dumprawsql',
105108
'exists',
106-
'existsOr',
109+
'existsor',
107110
'explain',
108-
'getBindings',
109-
'getConnection',
110-
'getGrammar',
111+
'getbindings',
112+
'getconnection',
113+
'getgrammar',
111114
'implode',
112115
'insert',
113-
'insertGetId',
114-
'insertOrIgnore',
115-
'insertUsing',
116+
'insertgetid',
117+
'insertorignore',
118+
'insertusing',
116119
'max',
117120
'min',
118121
'raw',
119-
'rawValue',
122+
'rawvalue',
120123
'sum',
121-
'toSql',
122-
'toRawSql',
124+
'tosql',
125+
'torawsql',
123126
];
124127

125128
/**
@@ -1964,7 +1967,7 @@ public function __call($method, $parameters)
19641967
return $this->callNamedScope($method, $parameters);
19651968
}
19661969

1967-
if (in_array($method, $this->passthru)) {
1970+
if (in_array(strtolower($method), $this->passthru)) {
19681971
return $this->toBase()->{$method}(...$parameters);
19691972
}
19701973

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,48 @@ public function testToRawSql()
22462246
$this->assertSame('select * from "users" where "email" = \'foo\'', $builder->toRawSql());
22472247
}
22482248

2249+
public function testPassthruMethodsCallsAreNotCaseSensitive()
2250+
{
2251+
$query = m::mock(BaseBuilder::class);
2252+
2253+
$mockResponse = 'select 1';
2254+
$query
2255+
->shouldReceive('toRawSql')
2256+
->andReturn($mockResponse)
2257+
->times(3);
2258+
2259+
$builder = new Builder($query);
2260+
2261+
$this->assertSame('select 1', $builder->TORAWSQL());
2262+
$this->assertSame('select 1', $builder->toRawSql());
2263+
$this->assertSame('select 1', $builder->toRawSQL());
2264+
}
2265+
2266+
public function testPassthruArrayElementsMustAllBeLowercase()
2267+
{
2268+
$builder = new class(m::mock(BaseBuilder::class)) extends Builder
2269+
{
2270+
// expose protected member for test
2271+
public function getPassthru(): array
2272+
{
2273+
return $this->passthru;
2274+
}
2275+
};
2276+
2277+
$passthru = $builder->getPassthru();
2278+
2279+
foreach ($passthru as $method) {
2280+
$lowercaseMethod = strtolower($method);
2281+
2282+
$this->assertSame(
2283+
$lowercaseMethod,
2284+
$method,
2285+
'Eloquent\\Builder relies on lowercase method names in $passthru array to correctly mimic PHP case-insensitivity on method dispatch.'.
2286+
'If you are adding a new method to the $passthru array, make sure the name is lowercased.'
2287+
);
2288+
}
2289+
}
2290+
22492291
protected function mockConnectionForModel($model, $database)
22502292
{
22512293
$grammarClass = 'Illuminate\Database\Query\Grammars\\'.$database.'Grammar';

0 commit comments

Comments
 (0)