[7588] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
| 4 | * |
---|
| 5 | * Redistribution and use in source and binary forms, with or without |
---|
| 6 | * modification, are permitted provided that the following conditions are met: |
---|
| 7 | * |
---|
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
| 9 | * this list of conditions and the following disclaimer. |
---|
| 10 | * |
---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 12 | * this listof conditions and the following disclaimer in the documentation |
---|
| 13 | * and/or other materials provided with the distribution. |
---|
| 14 | * |
---|
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 26 | * |
---|
| 27 | * The views and conclusions contained in the software and documentation are |
---|
| 28 | * those of the authors and should not be interpreted as representing official |
---|
| 29 | * policies, either expressed or implied, of the copyright holder. |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase |
---|
| 33 | { |
---|
| 34 | /** |
---|
| 35 | * @var Solarium_Query_Helper |
---|
| 36 | */ |
---|
| 37 | protected $_helper; |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * @var Solarium_Query_Select |
---|
| 41 | */ |
---|
| 42 | protected $_query; |
---|
| 43 | |
---|
| 44 | public function setUp() |
---|
| 45 | { |
---|
| 46 | $this->_query = new Solarium_Query_Select; |
---|
| 47 | $this->_helper = new Solarium_Query_Helper($this->_query); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | public function testRangeQueryInclusive() |
---|
| 51 | { |
---|
| 52 | $this->assertEquals( |
---|
| 53 | 'field:[1 TO 2]', |
---|
| 54 | $this->_helper->rangeQuery('field',1,2) |
---|
| 55 | ); |
---|
| 56 | |
---|
| 57 | $this->assertEquals( |
---|
| 58 | 'store:[45,-94 TO 46,-93]', |
---|
| 59 | $this->_helper->rangeQuery('store', '45,-94', '46,-93') |
---|
| 60 | ); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | public function testRangeQueryExclusive() |
---|
| 64 | { |
---|
| 65 | $this->assertEquals( |
---|
| 66 | 'field:{1 TO 2}', |
---|
| 67 | $this->_helper->rangeQuery('field',1,2, false) |
---|
| 68 | ); |
---|
| 69 | |
---|
| 70 | $this->assertEquals( |
---|
| 71 | 'store:{45,-94 TO 46,-93}', |
---|
| 72 | $this->_helper->rangeQuery('store', '45,-94', '46,-93', false) |
---|
| 73 | ); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public function testGeofilt() |
---|
| 77 | { |
---|
| 78 | $this->assertEquals( |
---|
| 79 | '{!geofilt pt=45.15,-93.85 sfield=store d=5}', |
---|
| 80 | $this->_helper->geofilt(45.15, -93.85, 'store', 5) |
---|
| 81 | ); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public function testBbox() |
---|
| 85 | { |
---|
| 86 | $this->assertEquals( |
---|
| 87 | '{!bbox pt=45.15,-93.85 sfield=store d=5}', |
---|
| 88 | $this->_helper->bbox(45.15, -93.85, 'store', 5) |
---|
| 89 | ); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | public function testGeodist() |
---|
| 93 | { |
---|
| 94 | $this->assertEquals( |
---|
| 95 | 'geodist(45.15,-93.85,store)', |
---|
| 96 | $this->_helper->geodist(45.15, -93.85, 'store') |
---|
| 97 | ); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | public function testQparserNoParams() |
---|
| 101 | { |
---|
| 102 | $this->assertEquals( |
---|
| 103 | '{!parser}', |
---|
| 104 | $this->_helper->qparser('parser') |
---|
| 105 | ); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | public function testQparser() |
---|
| 109 | { |
---|
| 110 | $this->assertEquals( |
---|
| 111 | '{!parser a=1 b=test}', |
---|
| 112 | $this->_helper->qparser('parser', array('a' => 1, 'b' => 'test')) |
---|
| 113 | ); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | public function testQparserDereferencedNoQuery() |
---|
| 117 | { |
---|
| 118 | $helper = new Solarium_Query_Helper(); |
---|
| 119 | $this->setExpectedException('Solarium_Exception'); |
---|
| 120 | $helper->qparser('join', array('from' => 'manu_id', 'to' => 'id'), true); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | public function testQparserDereferenced() |
---|
| 124 | { |
---|
| 125 | $this->assertEquals( |
---|
| 126 | '{!join from=$deref_1 to=$deref_2}', |
---|
| 127 | $this->_helper->qparser('join', array('from' => 'manu_id', 'to' => 'id'), true) |
---|
| 128 | ); |
---|
| 129 | |
---|
| 130 | $this->assertEquals( |
---|
| 131 | array('deref_1' => 'manu_id', 'deref_2' => 'id'), |
---|
| 132 | $this->_query->getParams() |
---|
| 133 | ); |
---|
| 134 | |
---|
| 135 | // second call, params should have updated counts |
---|
| 136 | $this->assertEquals( |
---|
| 137 | '{!join from=$deref_3 to=$deref_4}', |
---|
| 138 | $this->_helper->qparser('join', array('from' => 'cat_id', 'to' => 'prod_id'), true) |
---|
| 139 | ); |
---|
| 140 | |
---|
| 141 | // previous params should also still be there |
---|
| 142 | $this->assertEquals( |
---|
| 143 | array('deref_1' => 'manu_id', 'deref_2' => 'id', 'deref_3' => 'cat_id', 'deref_4' => 'prod_id'), |
---|
| 144 | $this->_query->getParams() |
---|
| 145 | ); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | public function testFunctionCallNoParams() |
---|
| 149 | { |
---|
| 150 | $this->assertEquals( |
---|
| 151 | 'sum()', |
---|
| 152 | $this->_helper->functionCall('sum') |
---|
| 153 | ); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | public function testFunctionCall() |
---|
| 157 | { |
---|
| 158 | $this->assertEquals( |
---|
| 159 | 'sum(1,2)', |
---|
| 160 | $this->_helper->functionCall('sum', array(1,2)) |
---|
| 161 | ); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | public function testEscapeTerm() |
---|
| 165 | { |
---|
| 166 | $this->assertEquals( |
---|
| 167 | 'a\\+b', |
---|
| 168 | $this->_helper->escapeTerm('a+b') |
---|
| 169 | ); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | public function testEscapeTermNoEscape() |
---|
| 173 | { |
---|
| 174 | $this->assertEquals( |
---|
| 175 | 'abc', |
---|
| 176 | $this->_helper->escapeTerm('abc') |
---|
| 177 | ); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | public function testEscapePhrase() |
---|
| 181 | { |
---|
| 182 | $this->assertEquals( |
---|
| 183 | '"a+\\"b"', |
---|
| 184 | $this->_helper->escapePhrase('a+"b') |
---|
| 185 | ); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | public function testEscapePhraseNoEscape() |
---|
| 189 | { |
---|
| 190 | $this->assertEquals( |
---|
| 191 | '"a+b"', |
---|
| 192 | $this->_helper->escapePhrase('a+b') |
---|
| 193 | ); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | public function testFormatDateInputTimestamp() |
---|
| 197 | { |
---|
| 198 | $this->assertFalse( |
---|
| 199 | $this->_helper->formatDate(strtotime('2011---')), |
---|
| 200 | 'Expects invalid strtotime/timestamp input (false) not to be accepted' |
---|
| 201 | ); |
---|
| 202 | |
---|
| 203 | //allow negative dates. |
---|
| 204 | $this->assertNotEquals( |
---|
| 205 | false, |
---|
| 206 | $this->_helper->formatDate(strtotime('2011-10-01')), |
---|
| 207 | 'Expects negative timestamp input to be accepted' |
---|
| 208 | ); |
---|
| 209 | |
---|
| 210 | //@todo find out if we need to any test for php versions / platforms which do not support negative timestamp |
---|
| 211 | |
---|
| 212 | $this->assertFalse( |
---|
| 213 | $this->_helper->formatDate(strtotime('2010-31-02')), |
---|
| 214 | 'Expects invalid timestamp input (not in calendar) not to be accepted' |
---|
| 215 | ); |
---|
| 216 | |
---|
| 217 | $this->assertEquals( |
---|
| 218 | $this->_mockFormatDateOutput(strtotime('2011-10-01')), |
---|
| 219 | $this->_helper->formatDate(strtotime('2011-10-01')), |
---|
| 220 | 'Expects formatDate with Timstamp input to output ISO8601 with stripped timezone' |
---|
| 221 | ); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | public function testFormatDateInputString() |
---|
| 225 | { |
---|
| 226 | $this->assertFalse( |
---|
| 227 | $this->_helper->formatDate('2011-13-31'), |
---|
| 228 | 'Expects an invalid date string input not to be accepted' |
---|
| 229 | ); |
---|
| 230 | |
---|
| 231 | $this->assertEquals( |
---|
| 232 | $this->_mockFormatDateOutput(strtotime('2011-10-01')), |
---|
| 233 | $this->_helper->formatDate('2011-10-01'), |
---|
| 234 | 'Expects formatDate with String input to output ISO8601 with stripped timezone' |
---|
| 235 | ); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | public function testFormatDateInputDateTime() |
---|
| 239 | { |
---|
| 240 | date_default_timezone_set("UTC"); // prevent timezone differences |
---|
| 241 | |
---|
| 242 | $this->assertFalse( |
---|
| 243 | $this->_helper->formatDate(new stdClass()), |
---|
| 244 | 'Expect any other object not to be accepted' |
---|
| 245 | ); |
---|
| 246 | |
---|
| 247 | $this->assertEquals( |
---|
| 248 | $this->_mockFormatDateOutput(strtotime('2011-10-01')), |
---|
| 249 | $this->_helper->formatDate(new DateTime('2011-10-01')), |
---|
| 250 | 'Expects formatDate with DateTime input to output ISO8601 with stripped timezone' |
---|
| 251 | ); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | public function testFormatDate() |
---|
| 255 | { |
---|
| 256 | //check if timezone is stripped |
---|
| 257 | $expected = strtoupper('Z'); |
---|
| 258 | $actual = substr($this->_helper->formatDate(time()), 19, 20); |
---|
| 259 | $this->assertEquals($expected, $actual, 'Expects last charachter to be uppercased Z'); |
---|
| 260 | |
---|
| 261 | $this->assertEquals( |
---|
| 262 | $this->_mockFormatDateOutput(time()), |
---|
| 263 | $this->_helper->formatDate(time()) |
---|
| 264 | ); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | protected function _mockFormatDateOutput($timestamp) |
---|
| 268 | { |
---|
| 269 | $date = new DateTime('@'.$timestamp); |
---|
| 270 | return strstr($date->format(DateTime::ISO8601), '+', true) . 'Z'; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | public function testAssemble() |
---|
| 274 | { |
---|
| 275 | // test single basic placeholder |
---|
| 276 | $this->assertEquals( |
---|
| 277 | 'id:456 AND cat:2', |
---|
| 278 | $this->_helper->assemble('id:%1% AND cat:2',array(456)) |
---|
| 279 | ); |
---|
| 280 | |
---|
| 281 | // test multiple basic placeholders and placeholder repeat |
---|
| 282 | $this->assertEquals( |
---|
| 283 | '(id:456 AND cat:2) OR (id:456 AND cat:1)', |
---|
| 284 | $this->_helper->assemble('(id:%1% AND cat:%2%) OR (id:%1% AND cat:%3%)',array(456, 2, 1)) |
---|
| 285 | ); |
---|
| 286 | |
---|
| 287 | // test literal placeholder (same as basic) |
---|
| 288 | $this->assertEquals( |
---|
| 289 | 'id:456 AND cat:2', |
---|
| 290 | $this->_helper->assemble('id:%L1% AND cat:2',array(456)) |
---|
| 291 | ); |
---|
| 292 | |
---|
| 293 | // test term placeholder |
---|
| 294 | $this->assertEquals( |
---|
| 295 | 'cat:2 AND content:a\\+b', |
---|
| 296 | $this->_helper->assemble('cat:2 AND content:%T1%',array('a+b')) |
---|
| 297 | ); |
---|
| 298 | |
---|
| 299 | // test term placeholder case-insensitive |
---|
| 300 | $this->assertEquals( |
---|
| 301 | 'cat:2 AND content:a\\+b', |
---|
| 302 | $this->_helper->assemble('cat:2 AND content:%t1%',array('a+b')) |
---|
| 303 | ); |
---|
| 304 | |
---|
| 305 | // test phrase placeholder |
---|
| 306 | $this->assertEquals( |
---|
| 307 | 'cat:2 AND content:"a+\\"b"', |
---|
| 308 | $this->_helper->assemble('cat:2 AND content:%P1%',array('a+"b')) |
---|
| 309 | ); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | public function testAssembleInvalidPartNumber() |
---|
| 313 | { |
---|
| 314 | $this->setExpectedException('Solarium_Exception'); |
---|
| 315 | $this->_helper->assemble('cat:%1% AND content:%2%',array('value1')); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | public function testJoin() |
---|
| 319 | { |
---|
| 320 | $this->assertEquals( |
---|
| 321 | '{!join from=manu_id to=id}', |
---|
| 322 | $this->_helper->join('manu_id', 'id') |
---|
| 323 | ); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | public function testJoinDereferenced() |
---|
| 327 | { |
---|
| 328 | $this->assertEquals( |
---|
| 329 | '{!join from=$deref_1 to=$deref_2}', |
---|
| 330 | $this->_helper->join('manu_id', 'id', true) |
---|
| 331 | ); |
---|
| 332 | |
---|
| 333 | $this->assertEquals( |
---|
| 334 | array('deref_1' => 'manu_id', 'deref_2' => 'id'), |
---|
| 335 | $this->_query->getParams() |
---|
| 336 | ); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | } |
---|