22
22
use ApiPlatform \Core \Tests \ProphecyTrait ;
23
23
use Doctrine \ORM \EntityManager ;
24
24
use Doctrine \ORM \Mapping \ClassMetadata ;
25
+ use Doctrine \ORM \Query \Expr \OrderBy ;
25
26
use Doctrine \ORM \QueryBuilder ;
26
27
use PHPUnit \Framework \TestCase ;
27
28
@@ -36,6 +37,7 @@ public function testApplyToCollectionWithValidOrder()
36
37
{
37
38
$ queryBuilderProphecy = $ this ->prophesize (QueryBuilder::class);
38
39
40
+ $ queryBuilderProphecy ->getDQLPart ('orderBy ' )->shouldBeCalled ()->willReturn ([]);
39
41
$ queryBuilderProphecy ->addOrderBy ('o.name ' , 'asc ' )->shouldBeCalled ();
40
42
41
43
$ classMetadataProphecy = $ this ->prophesize (ClassMetadata::class);
@@ -56,6 +58,7 @@ public function testApplyToCollectionWithWrongOrder()
56
58
{
57
59
$ queryBuilderProphecy = $ this ->prophesize (QueryBuilder::class);
58
60
61
+ $ queryBuilderProphecy ->getDQLPart ('orderBy ' )->shouldBeCalled ()->willReturn ([]);
59
62
$ queryBuilderProphecy ->addOrderBy ('o.name ' , 'asc ' )->shouldNotBeCalled ();
60
63
61
64
$ classMetadataProphecy = $ this ->prophesize (ClassMetadata::class);
@@ -77,6 +80,7 @@ public function testApplyToCollectionWithOrderOverridden()
77
80
$ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
78
81
$ queryBuilderProphecy = $ this ->prophesize (QueryBuilder::class);
79
82
83
+ $ queryBuilderProphecy ->getDQLPart ('orderBy ' )->shouldBeCalled ()->willReturn ([]);
80
84
$ queryBuilderProphecy ->addOrderBy ('o.foo ' , 'DESC ' )->shouldBeCalled ();
81
85
82
86
$ classMetadataProphecy = $ this ->prophesize (ClassMetadata::class);
@@ -100,6 +104,7 @@ public function testApplyToCollectionWithOrderOverriddenWithNoDirection()
100
104
$ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
101
105
$ queryBuilderProphecy = $ this ->prophesize (QueryBuilder::class);
102
106
107
+ $ queryBuilderProphecy ->getDQLPart ('orderBy ' )->shouldBeCalled ()->willReturn ([]);
103
108
$ queryBuilderProphecy ->addOrderBy ('o.foo ' , 'ASC ' )->shouldBeCalled ();
104
109
$ queryBuilderProphecy ->addOrderBy ('o.bar ' , 'DESC ' )->shouldBeCalled ();
105
110
@@ -124,6 +129,7 @@ public function testApplyToCollectionWithOrderOverriddenWithAssociation()
124
129
$ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
125
130
$ queryBuilderProphecy = $ this ->prophesize (QueryBuilder::class);
126
131
132
+ $ queryBuilderProphecy ->getDQLPart ('orderBy ' )->shouldBeCalled ()->willReturn ([]);
127
133
$ queryBuilderProphecy ->getDQLPart ('join ' )->willReturn (['o ' => []])->shouldBeCalled ();
128
134
$ queryBuilderProphecy ->innerJoin ('o.author ' , 'author_a1 ' , null , null )->shouldBeCalled ();
129
135
$ queryBuilderProphecy ->addOrderBy ('author_a1.name ' , 'ASC ' )->shouldBeCalled ();
@@ -148,6 +154,7 @@ public function testApplyToCollectionWithOrderOverriddenWithEmbeddedAssociation(
148
154
{
149
155
$ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
150
156
$ queryBuilderProphecy = $ this ->prophesize (QueryBuilder::class);
157
+ $ queryBuilderProphecy ->getDQLPart ('orderBy ' )->shouldBeCalled ()->willReturn ([]);
151
158
$ queryBuilderProphecy ->getRootAliases ()->willReturn (['o ' ]);
152
159
$ queryBuilderProphecy ->addOrderBy ('o.embeddedDummy.dummyName ' , 'DESC ' )->shouldBeCalled ();
153
160
@@ -166,4 +173,17 @@ public function testApplyToCollectionWithOrderOverriddenWithEmbeddedAssociation(
166
173
$ orderExtensionTest = new OrderExtension ('asc ' , $ resourceMetadataFactoryProphecy ->reveal ());
167
174
$ orderExtensionTest ->applyToCollection ($ queryBuilder , new QueryNameGenerator (), EmbeddedDummy::class);
168
175
}
176
+
177
+ public function testApplyToCollectionWithExistingOrderByDql ()
178
+ {
179
+ $ queryBuilderProphecy = $ this ->prophesize (QueryBuilder::class);
180
+
181
+ $ queryBuilderProphecy ->getDQLPart ('orderBy ' )->shouldBeCalled ()->willReturn ([new OrderBy ('o.name ' )]);
182
+ $ queryBuilderProphecy ->getEntityManager ()->shouldNotBeCalled ();
183
+ $ queryBuilderProphecy ->getRootAliases ()->shouldNotBeCalled ();
184
+
185
+ $ queryBuilder = $ queryBuilderProphecy ->reveal ();
186
+ $ orderExtensionTest = new OrderExtension ();
187
+ $ orderExtensionTest ->applyToCollection ($ queryBuilder , new QueryNameGenerator (), Dummy::class);
188
+ }
169
189
}
0 commit comments