|
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
|
|
|
*/ |
|
102
|
|
|
public function testExecuteWithParameters(): void |
|
103
|
|
|
{ |
|
104
|
|
|
$queryBuilder = $this->dm->createQueryBuilder(SimpleAnnotationDocument::class); |
|
105
|
|
|
|
|
106
|
|
|
$proxyQuery = new ProxyQuery($queryBuilder); |
|
107
|
|
|
|
|
108
|
|
|
// NEXT_MAJOR: Uncomment this line |
|
109
|
|
|
//$this->expectException(\InvalidArgumentException::class); |
|
110
|
|
|
|
|
111
|
|
|
$proxyQuery->execute(['some' => 'parameter']); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testExecuteAllowsSorting(): void |
|
115
|
|
|
{ |
|
116
|
|
|
$documentA = new SimpleAnnotationDocument('A'); |
|
117
|
|
|
$documentB = new SimpleAnnotationDocument('B'); |
|
118
|
|
|
|
|
119
|
|
|
$this->dm->persist($documentA); |
|
120
|
|
|
$this->dm->persist($documentB); |
|
121
|
|
|
$this->dm->flush(); |
|
122
|
|
|
|
|
123
|
|
|
$queryBuilder = $this->dm->createQueryBuilder(SimpleAnnotationDocument::class); |
|
124
|
|
|
$queryBuilder->select('name')->hydrate(false); |
|
125
|
|
|
$proxyQuery = new ProxyQuery($queryBuilder); |
|
126
|
|
|
$proxyQuery->setSortBy([], ['fieldName' => 'name']); |
|
127
|
|
|
$proxyQuery->setSortOrder('DESC'); |
|
128
|
|
|
|
|
129
|
|
|
$result = $proxyQuery->execute(); |
|
130
|
|
|
|
|
131
|
|
|
$names = array_map(static function (array $result) { |
|
132
|
|
|
return $result['name']; |
|
133
|
|
|
}, $result->toArray()); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertSame(['B', 'A'], $names); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function createConfiguration(): Configuration |
|
139
|
|
|
{ |
|
140
|
|
|
$config = new Configuration(); |
|
141
|
|
|
|
|
142
|
|
|
$directory = sys_get_temp_dir().'/mongodb'; |
|
143
|
|
|
|
|
144
|
|
|
$config->setProxyDir($directory); |
|
145
|
|
|
$config->setProxyNamespace('Proxies'); |
|
146
|
|
|
$config->setHydratorDir($directory); |
|
147
|
|
|
$config->setHydratorNamespace('Hydrators'); |
|
148
|
|
|
$config->setPersistentCollectionDir($directory); |
|
149
|
|
|
$config->setPersistentCollectionNamespace('PersistentCollections'); |
|
150
|
|
|
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver()); |
|
151
|
|
|
|
|
152
|
|
|
return $config; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|