CWIS Developer Documentation
SearchEngine--Test.php
Go to the documentation of this file.
1 <?PHP
2 
6 class SearchEngine_Test extends PHPUnit\Framework\TestCase
7 {
12  {
13  # grab an Engine
14  $Engine = new TestSearchEngine(
15  "Resources", "ResourceId", "SchemaId");
16 
17  # pull out a Reflector
18  $Reflector = new ReflectionObject($Engine);
19 
20  # get a ReflectionMethod for PSSFW and make it accessible
21  $Parse = $Reflector->getMethod(
22  'ParseSearchStringForWords');
23  $Parse->setAccessible(TRUE);
24 
25  # pull out class constants for easier use
26  $Present = SearchEngine::WORD_PRESENT;
27  $Excluded = SearchEngine::WORD_EXCLUDED;
28  $Required = SearchEngine::WORD_REQUIRED;
29 
30  # with AND logic, all terms are required
31  $this->assertEquals(
32  $Parse->invoke($Engine, "THIS IS A TEST", "AND"),
33  array(
34  "this" => $Present | $Required,
35  "is" => $Present | $Required,
36  "a" => $Present | $Required,
37  "test" => $Present | $Required,
38  ));
39 
40  # with OR logic, no terms are required
41  $this->assertEquals(
42  $Parse->invoke($Engine, "THIS IS A TEST", "OR"),
43  array(
44  "this" => $Present,
45  "is" => $Present,
46  "a" => $Present,
47  "test" => $Present,
48  ));
49 
50  # command characters (plus, minus, and tilde) override Logic
51  foreach (array("AND", "OR") as $Logic)
52  {
53  $this->assertEquals(
54  $Parse->invoke($Engine, "-WORD ~EXCLUSION +TEST", $Logic),
55  array(
56  "word" => $Present | $Excluded,
57  "exclusion" => $Present,
58  "test" => $Present | $Required,
59  ));
60  }
61 
62  # possessive plurals are stripped from search words
63  $this->assertEquals(
64  $Parse->invoke($Engine, "Children's toys", "AND"),
65  array(
66  "children" => $Present | $Required,
67  "toys" => $Present | $Required,
68  ));
69 
70  # single quotes are stripped from search string
71  $this->assertEquals(
72  $Parse->invoke($Engine, "Children's 'toys'", "AND"),
73  array(
74  "children" => $Present | $Required,
75  "toys" => $Present | $Required,
76  ));
77 
78  # phrases are stripped from search words
79  $this->assertEquals(
80  $Parse->invoke($Engine, "\"test kitten\"", "AND"),
81  array() );
82 
83  # groups are stripped from search words
84  $this->assertEquals(
85  $Parse->invoke($Engine, "(test kitten)", "AND"),
86  array() );
87 
88  # phrases treated as regular words when IgnoreGroups is TRUE
89  $this->assertEquals(
90  $Parse->invoke($Engine, "\"test kitten\"", "AND", TRUE),
91  array(
92  "test" => $Present | $Required,
93  "kitten" => $Present | $Required,
94  ));
95 
96  # groups treated as regular words when IgnoreGroups is TRUE
97  $this->assertEquals(
98  $Parse->invoke($Engine, "(test kitten)", "AND", TRUE),
99  array(
100  "test" => $Present | $Required,
101  "kitten" => $Present | $Required,
102  ));
103 
104  # punctuation is treated as a word separator
105  $this->assertEquals(
106  $Parse->invoke($Engine, "test...kitten", "AND"),
107  array(
108  "test" => $Present | $Required,
109  "kitten" => $Present | $Required,
110  ));
111 
112  # hyphenated words are considered as separate parts and also
113  # as one combined word
114  $this->assertEquals(
115  $Parse->invoke($Engine, "pre-process", "AND"),
116  array(
117  "pre" => $Present | $Required,
118  "process" => $Present | $Required,
119  "preprocess" => $Present | $Required,
120  ));
121 
122  # repeated runs of hyphens in the middle of a word are
123  # squeezed to a single hyphen and then processed normally
124  $this->assertEquals(
125  $Parse->invoke($Engine, "pre---process", "AND"),
126  array(
127  "pre" => $Present | $Required,
128  "process" => $Present | $Required,
129  "preprocess" => $Present | $Required,
130  ));
131 
132  # single non-hyphen command characters (plus and tilde) in the
133  # middle of words are treated as word separators
134  foreach (array("+", "~") as $Sep)
135  {
136  $this->assertEquals(
137  $Parse->invoke($Engine, "pre".$Sep."process", "AND"),
138  array(
139  "pre" => $Present | $Required,
140  "process" => $Present | $Required,
141  ));
142  }
143 
144  # runs of non-hyphen command characters (plus and tilde) in
145  # the middle of words are treated as word separators
146  foreach (array("+", "~") as $Sep)
147  {
148  $this->assertEquals(
149  $Parse->invoke($Engine, "pre".$Sep.$Sep.$Sep."process", "AND"),
150  array(
151  "pre" => $Present | $Required,
152  "process" => $Present | $Required,
153  ));
154  }
155 
156  # command characters (plus, minus, tilde) at the end of a word are
157  # ignored
158  foreach (array("+", "~", "-") as $Tail)
159  {
160  $this->assertEquals(
161  $Parse->invoke($Engine, "test".$Tail." kitten", "AND"),
162  array(
163  "test" => $Present | $Required,
164  "kitten" => $Present | $Required,
165  ));
166  }
167 
168  # runs of command characters (plus, minus, tilde) at the end
169  # of a word are ignored
170  foreach (array("+", "~", "-") as $Tail)
171  {
172  $this->assertEquals(
173  $Parse->invoke($Engine, "test".$Tail.$Tail.$Tail." kitten", "AND"),
174  array(
175  "test" => $Present | $Required,
176  "kitten" => $Present | $Required,
177  ));
178  }
179  }
180 }
181 
186 {
187  // purposely empty
188 }
189 
testParseSearchStringForWords()
Test ParseSearchStringForWords.
const WORD_EXCLUDED
Test cases for SearchEngine.
const WORD_PRESENT
Core metadata archive search engine class.
const WORD_REQUIRED
Create a concrete class extending SearchEngine (which is abstract) for testing.