Skip to content

Commit bd6f1c3

Browse files
authored
Fix compatibility with Laravel 10.30.0+ (#2661)
lowercase the `$passthru` array values
1 parent 25bd203 commit bd6f1c3

File tree

4 files changed

+127
-9
lines changed

4 files changed

+127
-9
lines changed

phpcs.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@
3434
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
3535
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>
3636
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>
37+
38+
<exclude name="Generic.Formatting.MultipleStatementAlignment" />
3739
</rule>
3840
</ruleset>

src/Eloquent/Builder.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,33 @@ class Builder extends EloquentBuilder
3030
'avg',
3131
'count',
3232
'dd',
33-
'doesntExist',
33+
'doesntexist',
3434
'dump',
3535
'exists',
36-
'getBindings',
37-
'getConnection',
38-
'getGrammar',
36+
'getbindings',
37+
'getconnection',
38+
'getgrammar',
3939
'insert',
40-
'insertGetId',
41-
'insertOrIgnore',
42-
'insertUsing',
40+
'insertgetid',
41+
'insertorignore',
42+
'insertusing',
4343
'max',
4444
'min',
4545
'pluck',
4646
'pull',
4747
'push',
4848
'raw',
4949
'sum',
50-
'toSql',
50+
'tomql',
51+
// Kept for compatibility with Laravel < 10.3
52+
'doesntExist',
53+
'getBindings',
54+
'getConnection',
55+
'getGrammar',
56+
'insertGetId',
57+
'insertOrIgnore',
58+
'insertUsing',
59+
'toMql',
5160
];
5261

5362
/** @inheritdoc */

tests/Eloquent/CallBuilderTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Eloquent;
6+
7+
use BadMethodCallException;
8+
use Generator;
9+
use MongoDB\Laravel\Eloquent\Builder;
10+
use MongoDB\Laravel\Query\Builder as QueryBuilder;
11+
use MongoDB\Laravel\Tests\Models\User;
12+
use MongoDB\Laravel\Tests\TestCase;
13+
use PHPUnit\Framework\Attributes\DataProvider;
14+
use PHPUnit\Framework\Attributes\Test;
15+
use RuntimeException;
16+
17+
use function assert;
18+
19+
final class CallBuilderTest extends TestCase
20+
{
21+
protected function tearDown(): void
22+
{
23+
User::truncate();
24+
}
25+
26+
#[Dataprovider('provideFunctionNames')]
27+
public function testCallingABuilderMethodDoesNotReturnTheBuilderInstance(string $method, string $className, $parameters = []): void
28+
{
29+
$builder = User::query()->newQuery();
30+
assert($builder instanceof Builder);
31+
32+
self::assertNotInstanceOf(expected: $className, actual: $builder->{$method}(...$parameters));
33+
}
34+
35+
public static function provideFunctionNames(): Generator
36+
{
37+
yield 'does not exist' => ['doesntExist', Builder::class];
38+
yield 'get bindings' => ['getBindings', Builder::class];
39+
yield 'get connection' => ['getConnection', Builder::class];
40+
yield 'get grammar' => ['getGrammar', Builder::class];
41+
yield 'insert get id' => ['insertGetId', Builder::class, [['user' => 'foo']]];
42+
yield 'to Mql' => ['toMql', Builder::class];
43+
yield 'average' => ['average', Builder::class, ['name']];
44+
yield 'avg' => ['avg', Builder::class, ['name']];
45+
yield 'count' => ['count', Builder::class, ['name']];
46+
yield 'exists' => ['exists', Builder::class];
47+
yield 'insert' => ['insert', Builder::class, [['name']]];
48+
yield 'max' => ['max', Builder::class, ['name']];
49+
yield 'min' => ['min', Builder::class, ['name']];
50+
yield 'pluck' => ['pluck', Builder::class, ['name']];
51+
yield 'pull' => ['pull', Builder::class, ['name']];
52+
yield 'push' => ['push', Builder::class, ['name']];
53+
yield 'raw' => ['raw', Builder::class];
54+
yield 'sum' => ['sum', Builder::class, ['name']];
55+
}
56+
57+
#[Test]
58+
#[DataProvider('provideUnsupportedMethods')]
59+
public function callingUnsupportedMethodThrowsAnException(string $method, string $exceptionClass, string $exceptionMessage, $parameters = []): void
60+
{
61+
$builder = User::query()->newQuery();
62+
assert($builder instanceof Builder);
63+
64+
$this->expectException($exceptionClass);
65+
$this->expectExceptionMessage($exceptionMessage);
66+
67+
$builder->{$method}(...$parameters);
68+
}
69+
70+
public static function provideUnsupportedMethods(): Generator
71+
{
72+
yield 'insert or ignore' => [
73+
'insertOrIgnore',
74+
RuntimeException::class,
75+
'This database engine does not support inserting while ignoring errors',
76+
[['name' => 'Jane']],
77+
];
78+
79+
yield 'insert using' => [
80+
'insertUsing',
81+
BadMethodCallException::class,
82+
'This method is not supported by MongoDB. Try "toMql()" instead',
83+
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
84+
];
85+
86+
yield 'to sql' => [
87+
'toSql',
88+
BadMethodCallException::class,
89+
'This method is not supported by MongoDB. Try "toMql()" instead',
90+
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
91+
];
92+
93+
yield 'dd' => [
94+
'dd',
95+
BadMethodCallException::class,
96+
'This method is not supported by MongoDB. Try "toMql()" instead',
97+
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
98+
];
99+
100+
yield 'dump' => [
101+
'dump',
102+
BadMethodCallException::class,
103+
'This method is not supported by MongoDB. Try "toMql()" instead',
104+
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
105+
];
106+
}
107+
}

tests/Eloquent/MassPrunableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Eloquent;
5+
namespace MongoDB\Laravel\Tests\Eloquent;
66

77
use Illuminate\Database\Console\PruneCommand;
88
use Illuminate\Database\Eloquent\MassPrunable;

0 commit comments

Comments
 (0)