1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Datagrid; |
15
|
|
|
|
16
|
|
|
use Doctrine\ODM\MongoDB\Configuration; |
17
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
18
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
19
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery; |
22
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document\SimpleAnnotationDocument; |
23
|
|
|
|
24
|
|
|
final class ProxyQueryTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Builder&MockObject |
28
|
|
|
*/ |
29
|
|
|
private $queryBuilder; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var DocumentManager |
33
|
|
|
*/ |
34
|
|
|
private $dm; |
35
|
|
|
|
36
|
|
|
protected function setUp(): void |
37
|
|
|
{ |
38
|
|
|
$this->dm = DocumentManager::create(null, $this->createConfiguration()); |
39
|
|
|
|
40
|
|
|
$this->queryBuilder = $this->createMock(Builder::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function tearDown(): void |
44
|
|
|
{ |
45
|
|
|
$this->dm->createQueryBuilder(SimpleAnnotationDocument::class) |
46
|
|
|
->remove() |
47
|
|
|
->getQuery() |
48
|
|
|
->execute(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testSetLimitToZeroWhenResettingMaxResults(): void |
52
|
|
|
{ |
53
|
|
|
$proxyQuery = new ProxyQuery($this->queryBuilder); |
54
|
|
|
|
55
|
|
|
$this->queryBuilder |
56
|
|
|
->expects($this->once()) |
57
|
|
|
->method('limit') |
58
|
|
|
->with(0); |
59
|
|
|
|
60
|
|
|
$proxyQuery->setMaxResults(null); |
61
|
|
|
|
62
|
|
|
$this->assertNull($proxyQuery->getMaxResults()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testSetSkipToZeroWhenResettingFirstResult(): void |
66
|
|
|
{ |
67
|
|
|
$proxyQuery = new ProxyQuery($this->queryBuilder); |
68
|
|
|
|
69
|
|
|
$this->queryBuilder |
70
|
|
|
->expects($this->once()) |
71
|
|
|
->method('skip') |
72
|
|
|
->with(0); |
73
|
|
|
|
74
|
|
|
$proxyQuery->setFirstResult(null); |
75
|
|
|
|
76
|
|
|
$this->assertNull($proxyQuery->getFirstResult()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testSorting(): void |
80
|
|
|
{ |
81
|
|
|
$proxyQuery = new ProxyQuery($this->queryBuilder); |
82
|
|
|
$proxyQuery->setSortBy([], ['fieldName' => 'name']); |
83
|
|
|
$proxyQuery->setSortOrder('ASC'); |
84
|
|
|
|
85
|
|
|
$this->assertSame( |
86
|
|
|
'name', |
87
|
|
|
$proxyQuery->getSortBy() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->assertSame( |
91
|
|
|
'ASC', |
92
|
|
|
$proxyQuery->getSortOrder() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* NEXT_MAJOR: Remove the legacy group and the "doesNotPerformAssertions". |
98
|
|
|
* |
99
|
|
|
* @group legacy |
100
|
|
|
* @doesNotPerformAssertions |
101
|
|
|
* @dataProvider getDeprecatedParameters |
102
|
|
|
*/ |
103
|
|
|
public function testExecuteWithParameters(array $parameters, ?int $hydrationMode): void |
104
|
|
|
{ |
105
|
|
|
$queryBuilder = $this->dm->createQueryBuilder(SimpleAnnotationDocument::class); |
106
|
|
|
|
107
|
|
|
$proxyQuery = new ProxyQuery($queryBuilder); |
108
|
|
|
|
109
|
|
|
// NEXT_MAJOR: Uncomment this line |
110
|
|
|
//$this->expectException(\InvalidArgumentException::class); |
111
|
|
|
|
112
|
|
|
$proxyQuery->execute($parameters, $hydrationMode); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getDeprecatedParameters(): array |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
[['some' => 'parameter'], null], |
119
|
|
|
[[], 3], |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testExecuteAllowsSorting(): void |
124
|
|
|
{ |
125
|
|
|
$documentA = new SimpleAnnotationDocument('A'); |
126
|
|
|
$documentB = new SimpleAnnotationDocument('B'); |
127
|
|
|
|
128
|
|
|
$this->dm->persist($documentA); |
129
|
|
|
$this->dm->persist($documentB); |
130
|
|
|
$this->dm->flush(); |
131
|
|
|
|
132
|
|
|
$queryBuilder = $this->dm->createQueryBuilder(SimpleAnnotationDocument::class); |
133
|
|
|
$queryBuilder->select('name')->hydrate(false); |
134
|
|
|
$proxyQuery = new ProxyQuery($queryBuilder); |
135
|
|
|
$proxyQuery->setSortBy([], ['fieldName' => 'name']); |
136
|
|
|
$proxyQuery->setSortOrder('DESC'); |
137
|
|
|
|
138
|
|
|
$result = $proxyQuery->execute(); |
139
|
|
|
|
140
|
|
|
$names = array_map(static function (array $result) { |
141
|
|
|
return $result['name']; |
142
|
|
|
}, $result->toArray()); |
143
|
|
|
|
144
|
|
|
$this->assertSame(['B', 'A'], array_values($names)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function createConfiguration(): Configuration |
148
|
|
|
{ |
149
|
|
|
$config = new Configuration(); |
150
|
|
|
|
151
|
|
|
$directory = sys_get_temp_dir().'/mongodb'; |
152
|
|
|
|
153
|
|
|
$config->setProxyDir($directory); |
154
|
|
|
$config->setProxyNamespace('Proxies'); |
155
|
|
|
$config->setHydratorDir($directory); |
156
|
|
|
$config->setHydratorNamespace('Hydrators'); |
157
|
|
|
$config->setPersistentCollectionDir($directory); |
158
|
|
|
$config->setPersistentCollectionNamespace('PersistentCollections'); |
159
|
|
|
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver()); |
160
|
|
|
|
161
|
|
|
return $config; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|