_query = new Solarium_Query_Select; $this->_helper = new Solarium_Query_Helper($this->_query); } public function testRangeQueryInclusive() { $this->assertEquals( 'field:[1 TO 2]', $this->_helper->rangeQuery('field',1,2) ); $this->assertEquals( 'store:[45,-94 TO 46,-93]', $this->_helper->rangeQuery('store', '45,-94', '46,-93') ); } public function testRangeQueryExclusive() { $this->assertEquals( 'field:{1 TO 2}', $this->_helper->rangeQuery('field',1,2, false) ); $this->assertEquals( 'store:{45,-94 TO 46,-93}', $this->_helper->rangeQuery('store', '45,-94', '46,-93', false) ); } public function testGeofilt() { $this->assertEquals( '{!geofilt pt=45.15,-93.85 sfield=store d=5}', $this->_helper->geofilt(45.15, -93.85, 'store', 5) ); } public function testBbox() { $this->assertEquals( '{!bbox pt=45.15,-93.85 sfield=store d=5}', $this->_helper->bbox(45.15, -93.85, 'store', 5) ); } public function testGeodist() { $this->assertEquals( 'geodist(45.15,-93.85,store)', $this->_helper->geodist(45.15, -93.85, 'store') ); } public function testQparserNoParams() { $this->assertEquals( '{!parser}', $this->_helper->qparser('parser') ); } public function testQparser() { $this->assertEquals( '{!parser a=1 b=test}', $this->_helper->qparser('parser', array('a' => 1, 'b' => 'test')) ); } public function testQparserDereferencedNoQuery() { $helper = new Solarium_Query_Helper(); $this->setExpectedException('Solarium_Exception'); $helper->qparser('join', array('from' => 'manu_id', 'to' => 'id'), true); } public function testQparserDereferenced() { $this->assertEquals( '{!join from=$deref_1 to=$deref_2}', $this->_helper->qparser('join', array('from' => 'manu_id', 'to' => 'id'), true) ); $this->assertEquals( array('deref_1' => 'manu_id', 'deref_2' => 'id'), $this->_query->getParams() ); // second call, params should have updated counts $this->assertEquals( '{!join from=$deref_3 to=$deref_4}', $this->_helper->qparser('join', array('from' => 'cat_id', 'to' => 'prod_id'), true) ); // previous params should also still be there $this->assertEquals( array('deref_1' => 'manu_id', 'deref_2' => 'id', 'deref_3' => 'cat_id', 'deref_4' => 'prod_id'), $this->_query->getParams() ); } public function testFunctionCallNoParams() { $this->assertEquals( 'sum()', $this->_helper->functionCall('sum') ); } public function testFunctionCall() { $this->assertEquals( 'sum(1,2)', $this->_helper->functionCall('sum', array(1,2)) ); } public function testEscapeTerm() { $this->assertEquals( 'a\\+b', $this->_helper->escapeTerm('a+b') ); } public function testEscapeTermNoEscape() { $this->assertEquals( 'abc', $this->_helper->escapeTerm('abc') ); } public function testEscapePhrase() { $this->assertEquals( '"a+\\"b"', $this->_helper->escapePhrase('a+"b') ); } public function testEscapePhraseNoEscape() { $this->assertEquals( '"a+b"', $this->_helper->escapePhrase('a+b') ); } public function testFormatDateInputTimestamp() { $this->assertFalse( $this->_helper->formatDate(strtotime('2011---')), 'Expects invalid strtotime/timestamp input (false) not to be accepted' ); //allow negative dates. $this->assertNotEquals( false, $this->_helper->formatDate(strtotime('2011-10-01')), 'Expects negative timestamp input to be accepted' ); //@todo find out if we need to any test for php versions / platforms which do not support negative timestamp $this->assertFalse( $this->_helper->formatDate(strtotime('2010-31-02')), 'Expects invalid timestamp input (not in calendar) not to be accepted' ); $this->assertEquals( $this->_mockFormatDateOutput(strtotime('2011-10-01')), $this->_helper->formatDate(strtotime('2011-10-01')), 'Expects formatDate with Timstamp input to output ISO8601 with stripped timezone' ); } public function testFormatDateInputString() { $this->assertFalse( $this->_helper->formatDate('2011-13-31'), 'Expects an invalid date string input not to be accepted' ); $this->assertEquals( $this->_mockFormatDateOutput(strtotime('2011-10-01')), $this->_helper->formatDate('2011-10-01'), 'Expects formatDate with String input to output ISO8601 with stripped timezone' ); } public function testFormatDateInputDateTime() { date_default_timezone_set("UTC"); // prevent timezone differences $this->assertFalse( $this->_helper->formatDate(new stdClass()), 'Expect any other object not to be accepted' ); $this->assertEquals( $this->_mockFormatDateOutput(strtotime('2011-10-01')), $this->_helper->formatDate(new DateTime('2011-10-01')), 'Expects formatDate with DateTime input to output ISO8601 with stripped timezone' ); } public function testFormatDate() { //check if timezone is stripped $expected = strtoupper('Z'); $actual = substr($this->_helper->formatDate(time()), 19, 20); $this->assertEquals($expected, $actual, 'Expects last charachter to be uppercased Z'); $this->assertEquals( $this->_mockFormatDateOutput(time()), $this->_helper->formatDate(time()) ); } protected function _mockFormatDateOutput($timestamp) { $date = new DateTime('@'.$timestamp); return strstr($date->format(DateTime::ISO8601), '+', true) . 'Z'; } public function testAssemble() { // test single basic placeholder $this->assertEquals( 'id:456 AND cat:2', $this->_helper->assemble('id:%1% AND cat:2',array(456)) ); // test multiple basic placeholders and placeholder repeat $this->assertEquals( '(id:456 AND cat:2) OR (id:456 AND cat:1)', $this->_helper->assemble('(id:%1% AND cat:%2%) OR (id:%1% AND cat:%3%)',array(456, 2, 1)) ); // test literal placeholder (same as basic) $this->assertEquals( 'id:456 AND cat:2', $this->_helper->assemble('id:%L1% AND cat:2',array(456)) ); // test term placeholder $this->assertEquals( 'cat:2 AND content:a\\+b', $this->_helper->assemble('cat:2 AND content:%T1%',array('a+b')) ); // test term placeholder case-insensitive $this->assertEquals( 'cat:2 AND content:a\\+b', $this->_helper->assemble('cat:2 AND content:%t1%',array('a+b')) ); // test phrase placeholder $this->assertEquals( 'cat:2 AND content:"a+\\"b"', $this->_helper->assemble('cat:2 AND content:%P1%',array('a+"b')) ); } public function testAssembleInvalidPartNumber() { $this->setExpectedException('Solarium_Exception'); $this->_helper->assemble('cat:%1% AND content:%2%',array('value1')); } public function testJoin() { $this->assertEquals( '{!join from=manu_id to=id}', $this->_helper->join('manu_id', 'id') ); } public function testJoinDereferenced() { $this->assertEquals( '{!join from=$deref_1 to=$deref_2}', $this->_helper->join('manu_id', 'id', true) ); $this->assertEquals( array('deref_1' => 'manu_id', 'deref_2' => 'id'), $this->_query->getParams() ); } }