|
9 | 9 | use Closure; |
10 | 10 | use DateTimeImmutable; |
11 | 11 | use Illuminate\Database\Eloquent\Collection; |
| 12 | +use Illuminate\Support\Arr; |
12 | 13 | use Illuminate\Tests\Database\DatabaseQueryBuilderTest; |
13 | 14 | use InvalidArgumentException; |
14 | 15 | use LogicException; |
@@ -57,6 +58,24 @@ public function testMql(array $expected, Closure $build, ?string $requiredMethod |
57 | 58 | $this->assertEquals($expected, $mql, var_export($mql, true)); |
58 | 59 | } |
59 | 60 |
|
| 61 | + #[DataProvider('provideConnectionOptions')] |
| 62 | + public function testConnectionOptionsAreInheritedByMql(array $expected, Closure $build, array $config): void |
| 63 | + { |
| 64 | + $builder = $build($this->getBuilder(true, $config)); |
| 65 | + $this->assertInstanceOf(Builder::class, $builder); |
| 66 | + $mql = $builder->toMql(); |
| 67 | + |
| 68 | + if (isset($expected['find'][1])) { |
| 69 | + $expected['find'][1]['typeMap'] = ['root' => 'object', 'document' => 'array']; |
| 70 | + } |
| 71 | + |
| 72 | + if (isset($expected['aggregate'][1])) { |
| 73 | + $expected['aggregate'][1]['typeMap'] = ['root' => 'object', 'document' => 'array']; |
| 74 | + } |
| 75 | + |
| 76 | + $this->assertEquals($expected, $mql, var_export($mql, true)); |
| 77 | + } |
| 78 | + |
60 | 79 | public static function provideQueryBuilderToMql(): iterable |
61 | 80 | { |
62 | 81 | /** |
@@ -1769,10 +1788,57 @@ public function prepareFieldsForQuery(array $values, bool $root = true): array |
1769 | 1788 | ); |
1770 | 1789 | } |
1771 | 1790 |
|
1772 | | - private function getBuilder(bool $renameEmbeddedIdField = true): Builder |
| 1791 | + public static function provideConnectionOptions(): iterable |
| 1792 | + { |
| 1793 | + yield 'find inherits maxTimeMS from config' => [ |
| 1794 | + ['find' => [[], ['maxTimeMS' => 2000]]], |
| 1795 | + fn (Builder $builder) => $builder, |
| 1796 | + ['options' => ['maxTimeMS' => 2000]], |
| 1797 | + ]; |
| 1798 | + |
| 1799 | + yield 'aggregate inherits maxTimeMS from config' => [ |
| 1800 | + [ |
| 1801 | + 'aggregate' => [ |
| 1802 | + [['$group' => ['_id' => ['foo' => '$foo'], 'foo' => ['$last' => '$foo']]]], |
| 1803 | + ['maxTimeMS' => 2000], |
| 1804 | + ], |
| 1805 | + ], |
| 1806 | + fn (Builder $builder) => $builder->groupBy('foo'), |
| 1807 | + ['options' => ['maxTimeMS' => 2000]], |
| 1808 | + ]; |
| 1809 | + |
| 1810 | + yield 'distinct inherits maxTimeMS from config' => [ |
| 1811 | + ['distinct' => ['foo', [], ['maxTimeMS' => 2000]]], |
| 1812 | + fn (Builder $builder) => $builder->distinct('foo'), |
| 1813 | + ['options' => ['maxTimeMS' => 2000]], |
| 1814 | + ]; |
| 1815 | + |
| 1816 | + yield 'options maxTimeMS overrides config maxTimeMS' => [ |
| 1817 | + ['find' => [[], ['maxTimeMS' => 5000]]], |
| 1818 | + fn (Builder $builder) => $builder->options(['maxTimeMS' => 5000]), |
| 1819 | + ['options' => ['maxTimeMS' => 2000]], |
| 1820 | + ]; |
| 1821 | + |
| 1822 | + yield 'timeout overrides config maxTimeMS' => [ |
| 1823 | + ['find' => [[], ['maxTimeMS' => 1000]]], |
| 1824 | + fn (Builder $builder) => $builder->timeout(1), |
| 1825 | + ['options' => ['maxTimeMS' => 2000]], |
| 1826 | + ]; |
| 1827 | + |
| 1828 | + yield 'empty config does not inject maxTimeMS' => [ |
| 1829 | + ['find' => [[], []]], |
| 1830 | + fn (Builder $builder) => $builder, |
| 1831 | + [], |
| 1832 | + ]; |
| 1833 | + } |
| 1834 | + |
| 1835 | + private function getBuilder(bool $renameEmbeddedIdField = true, array $config = []): Builder |
1773 | 1836 | { |
1774 | 1837 | $connection = $this->createStub(Connection::class); |
1775 | 1838 | $connection->method('getRenameEmbeddedIdField')->willReturn($renameEmbeddedIdField); |
| 1839 | + $connection->method('getConfig')->willReturnCallback( |
| 1840 | + static fn (?string $key = null): mixed => Arr::get($config, $key), |
| 1841 | + ); |
1776 | 1842 | $processor = $this->createStub(Processor::class); |
1777 | 1843 | $connection->method('getSession')->willReturn(null); |
1778 | 1844 | $connection->method('getQueryGrammar')->willReturn(new Grammar($connection)); |
|
0 commit comments