Skip to content

Commit bd7af52

Browse files
committed
add test to deal with the change in method casing
1 parent 35c0fc9 commit bd7af52

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

tests/Eloquent/CallBuilderTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
#[Test]
22+
#[Dataprovider('provideFunctionNames')]
23+
public function callingABuilderMethodDoesNotReturnTheBuilderInstance(string $method, string $className, $parameters = []): void
24+
{
25+
$builder = User::query()->newQuery();
26+
assert($builder instanceof Builder);
27+
28+
self::assertNotInstanceOf(expected: $className, actual: $builder->{$method}(...$parameters));
29+
}
30+
31+
public static function provideFunctionNames(): Generator
32+
{
33+
yield 'does not exist' => [
34+
'doesntExist',
35+
Builder::class,
36+
];
37+
38+
yield 'get bindings' => [
39+
'getBindings',
40+
Builder::class,
41+
];
42+
43+
yield 'get connection' => [
44+
'getConnection',
45+
Builder::class,
46+
];
47+
48+
yield 'get grammar' => [
49+
'getGrammar',
50+
Builder::class,
51+
];
52+
53+
yield 'insert get id' => [
54+
'insertGetId',
55+
Builder::class,
56+
[['user' => 'foo']],
57+
];
58+
59+
yield 'to Mql' => [
60+
'toMql',
61+
Builder::class,
62+
];
63+
}
64+
65+
#[Test]
66+
#[DataProvider('provideUnsupportedMethods')]
67+
public function callingUnsupportedMethodThrowsAnException(string $method, string $exceptionClass, string $exceptionMessage, $parameters = []): void
68+
{
69+
$builder = User::query()->newQuery();
70+
assert($builder instanceof Builder);
71+
72+
$this->expectException($exceptionClass);
73+
$this->expectExceptionMessage($exceptionMessage);
74+
75+
$builder->{$method}(...$parameters);
76+
}
77+
78+
public static function provideUnsupportedMethods(): Generator
79+
{
80+
yield 'insert or ignore' => [
81+
'insertOrIgnore',
82+
RuntimeException::class,
83+
'This database engine does not support inserting while ignoring errors',
84+
[['name' => 'Jane']],
85+
];
86+
87+
yield 'insert using' => [
88+
'insertUsing',
89+
BadMethodCallException::class,
90+
'This method is not supported by MongoDB. Try "toMql()" instead',
91+
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
92+
];
93+
94+
yield 'to sql' => [
95+
'toSql',
96+
BadMethodCallException::class,
97+
'This method is not supported by MongoDB. Try "toMql()" instead',
98+
[[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder],
99+
];
100+
}
101+
}

0 commit comments

Comments
 (0)