Skip to content

Fix compatibility with Laravel 10.30 — lowercase the $passthru array values #2661

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
Nov 2, 2023
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
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>

<exclude name="Generic.Formatting.MultipleStatementAlignment" />
</rule>
</ruleset>
25 changes: 17 additions & 8 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,33 @@ class Builder extends EloquentBuilder
'avg',
'count',
'dd',
'doesntExist',
'doesntexist',
'dump',
'exists',
'getBindings',
'getConnection',
'getGrammar',
'getbindings',
'getconnection',
'getgrammar',
'insert',
'insertGetId',
'insertOrIgnore',
'insertUsing',
'insertgetid',
Copy link
Member

@GromNaN GromNaN Nov 2, 2023

Choose a reason for hiding this comment

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

If #2659 is merged, this method will never be called. Would it be better to remove it? Possibly by leaving a comment.
Or maybe discard #2659 and keep this PR as a fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What you said in the comment in #2659 makes sense, the latter part.
Personally I like to be specific/remove magic, but that's the way Laravel works and we need to align with that.

In short, close #2659 in favour of this one.

It seems to be fixed by #2661. Both solutions will work, yours removes a layer of magics but it is more fragile if the implementation changes in Laravel.

'insertorignore',
'insertusing',
'max',
'min',
'pluck',
'pull',
'push',
'raw',
'sum',
'toSql',
'tomql',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

replaced as it is not supported

// Kept for compatibility with Laravel < 10.3
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And here I was trying to conditionally set the method names casing 😆

Copy link
Member

Choose a reason for hiding this comment

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

Simple and verbose solutions are often good enough.

'doesntExist',
'getBindings',
'getConnection',
'getGrammar',
'insertGetId',
'insertOrIgnore',
'insertUsing',
'toMql',
];

/** @inheritdoc */
Expand Down
107 changes: 107 additions & 0 deletions tests/Eloquent/CallBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Eloquent;

use BadMethodCallException;
use Generator;
use MongoDB\Laravel\Eloquent\Builder;
use MongoDB\Laravel\Query\Builder as QueryBuilder;
use MongoDB\Laravel\Tests\Models\User;
use MongoDB\Laravel\Tests\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use RuntimeException;

use function assert;

final class CallBuilderTest extends TestCase
{
protected function tearDown(): void
{
User::truncate();
}

#[Dataprovider('provideFunctionNames')]
public function testCallingABuilderMethodDoesNotReturnTheBuilderInstance(string $method, string $className, $parameters = []): void
{
$builder = User::query()->newQuery();
assert($builder instanceof Builder);

self::assertNotInstanceOf(expected: $className, actual: $builder->{$method}(...$parameters));
}

public static function provideFunctionNames(): Generator
Copy link
Contributor Author

Choose a reason for hiding this comment

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

provide supported methods, which should not return a Builder instance.

{
yield 'does not exist' => ['doesntExist', Builder::class];
yield 'get bindings' => ['getBindings', Builder::class];
yield 'get connection' => ['getConnection', Builder::class];
yield 'get grammar' => ['getGrammar', Builder::class];
yield 'insert get id' => ['insertGetId', Builder::class, [['user' => 'foo']]];
yield 'to Mql' => ['toMql', Builder::class];
yield 'average' => ['average', Builder::class, ['name']];
yield 'avg' => ['avg', Builder::class, ['name']];
yield 'count' => ['count', Builder::class, ['name']];
yield 'exists' => ['exists', Builder::class];
yield 'insert' => ['insert', Builder::class, [['name']]];
yield 'max' => ['max', Builder::class, ['name']];
yield 'min' => ['min', Builder::class, ['name']];
yield 'pluck' => ['pluck', Builder::class, ['name']];
yield 'pull' => ['pull', Builder::class, ['name']];
yield 'push' => ['push', Builder::class, ['name']];
yield 'raw' => ['raw', Builder::class];
yield 'sum' => ['sum', Builder::class, ['name']];
}

#[Test]
#[DataProvider('provideUnsupportedMethods')]
public function callingUnsupportedMethodThrowsAnException(string $method, string $exceptionClass, string $exceptionMessage, $parameters = []): void
{
$builder = User::query()->newQuery();
assert($builder instanceof Builder);

$this->expectException($exceptionClass);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

verify that certain methods are not supported, checking both the exception type and its message

$this->expectExceptionMessage($exceptionMessage);

$builder->{$method}(...$parameters);
}

public static function provideUnsupportedMethods(): Generator
{
yield 'insert or ignore' => [
'insertOrIgnore',
RuntimeException::class,
'This database engine does not support inserting while ignoring errors',
[['name' => 'Jane']],
];

yield 'insert using' => [
'insertUsing',
BadMethodCallException::class,
'This method is not supported by MongoDB. Try "toMql()" instead',
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
];

yield 'to sql' => [
'toSql',
BadMethodCallException::class,
'This method is not supported by MongoDB. Try "toMql()" instead',
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
];

yield 'dd' => [
'dd',
Copy link
Member

Choose a reason for hiding this comment

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

We can create a separate issue to support this, once this PR get merged.

BadMethodCallException::class,
'This method is not supported by MongoDB. Try "toMql()" instead',
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
];

yield 'dump' => [
'dump',
BadMethodCallException::class,
'This method is not supported by MongoDB. Try "toMql()" instead',
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
];
}
}
2 changes: 1 addition & 1 deletion tests/Eloquent/MassPrunableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Eloquent;
namespace MongoDB\Laravel\Tests\Eloquent;

use Illuminate\Database\Console\PruneCommand;
use Illuminate\Database\Eloquent\MassPrunable;
Expand Down