Skip to content

Commit fd238bc

Browse files
fix(Eloquent/Builder): calling the methods on passthru base object should be case-insensitive (#48852)
* fix(Eloquent/Builder): calling the methods on passthru base object should be case-insensitive Fixes: #48825 * Update Builder.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 85a5146 commit fd238bc

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,29 +97,29 @@ class Builder implements BuilderContract
9797
'avg',
9898
'count',
9999
'dd',
100-
'ddRawSql',
101-
'doesntExist',
102-
'doesntExistOr',
100+
'ddrawsql',
101+
'doesntexist',
102+
'doesntexistor',
103103
'dump',
104-
'dumpRawSql',
104+
'dumprawsql',
105105
'exists',
106-
'existsOr',
106+
'existsor',
107107
'explain',
108-
'getBindings',
109-
'getConnection',
110-
'getGrammar',
108+
'getbindings',
109+
'getconnection',
110+
'getgrammar',
111111
'implode',
112112
'insert',
113-
'insertGetId',
114-
'insertOrIgnore',
115-
'insertUsing',
113+
'insertgetid',
114+
'insertorignore',
115+
'insertusing',
116116
'max',
117117
'min',
118118
'raw',
119-
'rawValue',
119+
'rawvalue',
120120
'sum',
121-
'toSql',
122-
'toRawSql',
121+
'tosql',
122+
'torawsql',
123123
];
124124

125125
/**
@@ -1964,7 +1964,7 @@ public function __call($method, $parameters)
19641964
return $this->callNamedScope($method, $parameters);
19651965
}
19661966

1967-
if (in_array($method, $this->passthru)) {
1967+
if (in_array(strtolower($method), $this->passthru)) {
19681968
return $this->toBase()->{$method}(...$parameters);
19691969
}
19701970

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)