|
| 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' => [ |
| 38 | + 'doesntExist', |
| 39 | + Builder::class, |
| 40 | + ]; |
| 41 | + |
| 42 | + yield 'get bindings' => [ |
| 43 | + 'getBindings', |
| 44 | + Builder::class, |
| 45 | + ]; |
| 46 | + |
| 47 | + yield 'get connection' => [ |
| 48 | + 'getConnection', |
| 49 | + Builder::class, |
| 50 | + ]; |
| 51 | + |
| 52 | + yield 'get grammar' => [ |
| 53 | + 'getGrammar', |
| 54 | + Builder::class, |
| 55 | + ]; |
| 56 | + |
| 57 | + yield 'insert get id' => [ |
| 58 | + 'insertGetId', |
| 59 | + Builder::class, |
| 60 | + [['user' => 'foo']], |
| 61 | + ]; |
| 62 | + |
| 63 | + yield 'to Mql' => [ |
| 64 | + 'toMql', |
| 65 | + Builder::class, |
| 66 | + ]; |
| 67 | + |
| 68 | + yield 'average' => [ |
| 69 | + 'average', |
| 70 | + Builder::class, |
| 71 | + ['name'], |
| 72 | + ]; |
| 73 | + |
| 74 | + yield 'avg' => [ |
| 75 | + 'avg', |
| 76 | + Builder::class, |
| 77 | + ['name'], |
| 78 | + ]; |
| 79 | + |
| 80 | + yield 'count' => [ |
| 81 | + 'count', |
| 82 | + Builder::class, |
| 83 | + ['name'], |
| 84 | + ]; |
| 85 | + |
| 86 | + yield 'exists' => [ |
| 87 | + 'exists', |
| 88 | + Builder::class, |
| 89 | + ]; |
| 90 | + |
| 91 | + yield 'insert' => [ |
| 92 | + 'insert', |
| 93 | + Builder::class, |
| 94 | + [['name']], |
| 95 | + ]; |
| 96 | + |
| 97 | + yield 'max' => [ |
| 98 | + 'max', |
| 99 | + Builder::class, |
| 100 | + ['name'], |
| 101 | + ]; |
| 102 | + |
| 103 | + yield 'min' => [ |
| 104 | + 'min', |
| 105 | + Builder::class, |
| 106 | + ['name'], |
| 107 | + ]; |
| 108 | + |
| 109 | + yield 'pluck' => [ |
| 110 | + 'pluck', |
| 111 | + Builder::class, |
| 112 | + ['name'], |
| 113 | + ]; |
| 114 | + |
| 115 | + yield 'pull' => [ |
| 116 | + 'pull', |
| 117 | + Builder::class, |
| 118 | + ['name'], |
| 119 | + ]; |
| 120 | + |
| 121 | + yield 'push' => [ |
| 122 | + 'push', |
| 123 | + Builder::class, |
| 124 | + ['name'], |
| 125 | + ]; |
| 126 | + |
| 127 | + yield 'raw' => [ |
| 128 | + 'raw', |
| 129 | + Builder::class, |
| 130 | + ]; |
| 131 | + |
| 132 | + yield 'sum' => [ |
| 133 | + 'sum', |
| 134 | + Builder::class, |
| 135 | + ['name'], |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + #[Test] |
| 140 | + #[DataProvider('provideUnsupportedMethods')] |
| 141 | + public function callingUnsupportedMethodThrowsAnException(string $method, string $exceptionClass, string $exceptionMessage, $parameters = []): void |
| 142 | + { |
| 143 | + $builder = User::query()->newQuery(); |
| 144 | + assert($builder instanceof Builder); |
| 145 | + |
| 146 | + $this->expectException($exceptionClass); |
| 147 | + $this->expectExceptionMessage($exceptionMessage); |
| 148 | + |
| 149 | + $builder->{$method}(...$parameters); |
| 150 | + } |
| 151 | + |
| 152 | + public static function provideUnsupportedMethods(): Generator |
| 153 | + { |
| 154 | + yield 'insert or ignore' => [ |
| 155 | + 'insertOrIgnore', |
| 156 | + RuntimeException::class, |
| 157 | + 'This database engine does not support inserting while ignoring errors', |
| 158 | + [['name' => 'Jane']], |
| 159 | + ]; |
| 160 | + |
| 161 | + yield 'insert using' => [ |
| 162 | + 'insertUsing', |
| 163 | + BadMethodCallException::class, |
| 164 | + 'This method is not supported by MongoDB. Try "toMql()" instead', |
| 165 | + [[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder], |
| 166 | + ]; |
| 167 | + |
| 168 | + yield 'to sql' => [ |
| 169 | + 'toSql', |
| 170 | + BadMethodCallException::class, |
| 171 | + 'This method is not supported by MongoDB. Try "toMql()" instead', |
| 172 | + [[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder], |
| 173 | + ]; |
| 174 | + |
| 175 | + yield 'dd' => [ |
| 176 | + 'dd', |
| 177 | + BadMethodCallException::class, |
| 178 | + 'This method is not supported by MongoDB. Try "toMql()" instead', |
| 179 | + [[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder], |
| 180 | + ]; |
| 181 | + |
| 182 | + yield 'dump' => [ |
| 183 | + 'dump', |
| 184 | + BadMethodCallException::class, |
| 185 | + 'This method is not supported by MongoDB. Try "toMql()" instead', |
| 186 | + [[['name' => 'Jane']], fn (QueryBuilder $builder) => $builder], |
| 187 | + ]; |
| 188 | + } |
| 189 | +} |
0 commit comments