8
8
use Firesphere \SolrSearch \Factories \QueryComponentFactory ;
9
9
use Firesphere \SolrSearch \Indexes \BaseIndex ;
10
10
use Firesphere \SolrSearch \Queries \BaseQuery ;
11
+ use Minimalcode \Search \Criteria ;
11
12
use Page ;
13
+ use ReflectionMethod ;
12
14
use SilverStripe \Core \Config \Config ;
13
15
use SilverStripe \Core \Injector \Injector ;
14
16
use SilverStripe \Dev \SapphireTest ;
15
17
use Solarium \Core \Query \Helper ;
18
+ use Solarium \QueryType \Select \Query \FilterQuery ;
19
+ use Solarium \QueryType \Select \Query \Query ;
16
20
17
21
class QueryComponentFactoryTest extends SapphireTest
18
22
{
19
23
protected static $ fixture_file = '../fixtures/DataResolver.yml ' ;
24
+
20
25
protected static $ extra_dataobjects = [
21
26
TestObject::class,
22
27
TestPage::class,
@@ -28,6 +33,17 @@ class QueryComponentFactoryTest extends SapphireTest
28
33
*/
29
34
protected $ factory ;
30
35
36
+ protected function setUp ()
37
+ {
38
+ parent ::setUp ();
39
+ Injector::inst ()->get (Page::class)->requireDefaultRecords ();
40
+ foreach (self ::$ extra_dataobjects as $ className ) {
41
+ Config::modify ()->merge ($ className , 'extensions ' , [DataObjectExtension::class]);
42
+ }
43
+ $ this ->factory = new QueryComponentFactory ();
44
+ $ this ->factory ->setIndex (Injector::inst ()->get (CircleCITestIndex::class));
45
+ }
46
+
31
47
public function testBuildQuery ()
32
48
{
33
49
$ index = new CircleCITestIndex ();
@@ -61,7 +77,6 @@ public function testBuildQuery()
61
77
$ this ->assertInternalType ('array ' , $ this ->factory ->getBoostTerms ());
62
78
}
63
79
64
-
65
80
public function testEscapeTerms ()
66
81
{
67
82
$ term = '"test me" help ' ;
@@ -72,19 +87,104 @@ public function testEscapeTerms()
72
87
$ this ->assertEquals ('"test me" help ' , $ escaped );
73
88
74
89
$ term = 'help me ' ;
75
-
76
90
$ this ->assertEquals ('help me ' , $ this ->factory ->escapeSearch ($ term ));
77
91
}
78
92
93
+ public function testBuildCriteriaFilter ()
94
+ {
95
+ $ reflectionMethod = new ReflectionMethod (QueryComponentFactory::class, 'buildCriteriaFilter ' );
96
+ $ reflectionMethod ->setAccessible (true );
97
+
98
+ /** @var Criteria $criteria */
99
+ $ criteria = $ reflectionMethod ->invoke (new QueryComponentFactory (), 'TestField ' , 'TestValue ' );
100
+ $ this ->assertInstanceOf (Criteria::class, $ criteria );
101
+ $ this ->assertEquals ('TestField:TestValue ' , $ criteria ->getQuery ());
102
+
103
+ $ criteria = $ reflectionMethod ->invoke (new QueryComponentFactory (), 'TestField ' , ['Arr1 ' , 'Arr2 ' ]);
104
+ $ this ->assertInstanceOf (Criteria::class, $ criteria );
105
+ $ this ->assertEquals ('TestField:(Arr1 Arr2) ' , $ criteria ->getQuery ());
106
+
107
+ $ criteriaValue = Criteria::where ('TestField ' )->is ('TestValue ' );
108
+ $ criteria = $ reflectionMethod ->invoke (new QueryComponentFactory (), 'TestField ' , $ criteriaValue );
109
+ $ this ->assertInstanceOf (Criteria::class, $ criteria );
110
+ $ this ->assertSame ($ criteriaValue , $ criteria );
111
+ }
79
112
80
- protected function setUp ()
113
+ public function testFilterAndExcludeStrings ()
81
114
{
82
- parent ::setUp ();
83
- Injector::inst ()->get (Page::class)->requireDefaultRecords ();
84
- foreach (self ::$ extra_dataobjects as $ className ) {
85
- Config::modify ()->merge ($ className , 'extensions ' , [DataObjectExtension::class]);
86
- }
87
- $ this ->factory = new QueryComponentFactory ();
88
- $ this ->factory ->setIndex (Injector::inst ()->get (CircleCITestIndex::class));
115
+ $ mockFilterQuery = $ this ->createMock (FilterQuery::class);
116
+ $ mockFilterQuery ->expects ($ this ->once ())
117
+ ->method ('setQuery ' )
118
+ ->with ($ this ->equalTo ('TestFilterField:TestFilterValue ' ));
119
+ $ mockExcludeQuery = $ this ->createMock (FilterQuery::class);
120
+ $ mockExcludeQuery ->expects ($ this ->once ())
121
+ ->method ('setQuery ' )
122
+ ->with ($ this ->equalTo ('-TestExcludeField:TestExcludeValue ' ));
123
+
124
+ $ mockClientQuery = new class extends Query {
125
+ public $ mockFilterQuery ;
126
+ public $ mockExcludeQuery ;
127
+ public function createFilterQuery ($ options = null ): FilterQuery
128
+ {
129
+ if ($ options === 'filter-TestFilterField ' ) {
130
+ return $ this ->mockFilterQuery ;
131
+ } elseif ($ options === 'exclude-TestExcludeField ' ) {
132
+ return $ this ->mockExcludeQuery ;
133
+ }
134
+
135
+ return parent ::createFilterQuery ($ options );
136
+ }
137
+ };
138
+ $ mockClientQuery ->mockFilterQuery = $ mockFilterQuery ;
139
+ $ mockClientQuery ->mockExcludeQuery = $ mockExcludeQuery ;
140
+
141
+ $ baseQuery = new BaseQuery ();
142
+ $ baseQuery ->addFilter ('TestFilterField ' , 'TestFilterValue ' );
143
+ $ baseQuery ->addExclude ('TestExcludeField ' , 'TestExcludeValue ' );
144
+
145
+ $ factory = new QueryComponentFactory ();
146
+ $ factory ->setIndex (Injector::inst ()->get (CircleCITestIndex::class));
147
+ $ factory ->setQuery ($ baseQuery );
148
+ $ factory ->setClientQuery ($ mockClientQuery );
149
+ $ factory ->buildQuery ();
150
+ }
151
+
152
+ public function testFilterAndExcludeCriteria ()
153
+ {
154
+ $ mockFilterQuery = $ this ->createMock (FilterQuery::class);
155
+ $ mockFilterQuery ->expects ($ this ->once ())
156
+ ->method ('setQuery ' )
157
+ ->with ($ this ->equalTo ('TestFilterField:TestFilterValue ' ));
158
+ $ mockExcludeQuery = $ this ->createMock (FilterQuery::class);
159
+ $ mockExcludeQuery ->expects ($ this ->once ())
160
+ ->method ('setQuery ' )
161
+ ->with ($ this ->equalTo ('-TestExcludeField:TestExcludeValue ' ));
162
+
163
+ $ mockClientQuery = new class extends Query {
164
+ public $ mockFilterQuery ;
165
+ public $ mockExcludeQuery ;
166
+ public function createFilterQuery ($ options = null ): FilterQuery
167
+ {
168
+ if ($ options === 'filter-TestFilterField ' ) {
169
+ return $ this ->mockFilterQuery ;
170
+ } elseif ($ options === 'exclude-TestExcludeField ' ) {
171
+ return $ this ->mockExcludeQuery ;
172
+ }
173
+
174
+ return parent ::createFilterQuery ($ options );
175
+ }
176
+ };
177
+ $ mockClientQuery ->mockFilterQuery = $ mockFilterQuery ;
178
+ $ mockClientQuery ->mockExcludeQuery = $ mockExcludeQuery ;
179
+
180
+ $ baseQuery = new BaseQuery ();
181
+ $ baseQuery ->addFilter ('TestFilterField ' , Criteria::where ('TestFilterField ' )->is ('TestFilterValue ' ));
182
+ $ baseQuery ->addExclude ('TestExcludeField ' , Criteria::where ('TestExcludeField ' )->is ('TestExcludeValue ' ));
183
+
184
+ $ factory = new QueryComponentFactory ();
185
+ $ factory ->setIndex (Injector::inst ()->get (CircleCITestIndex::class));
186
+ $ factory ->setQuery ($ baseQuery );
187
+ $ factory ->setClientQuery ($ mockClientQuery );
188
+ $ factory ->buildQuery ();
89
189
}
90
190
}
0 commit comments