/**
  * Tests the returned result of {@link Solr::query()}.
  */
 function testQuery()
 {
     $response = new HTTP_Request2_Response('HTTP/1.0 200 OK');
     $solrResponse = '{"responseHeader":{"status":0,"QTime":0,"params":{"q":"php","qt":"standard","wt":"json"}},"response":{"numFound":1,"start":0,"docs":[{"id":42,"title":"PHP 5"}]}}';
     $response->appendBody($solrResponse);
     $this->httpClientMock->expects($this->once())->method('sendRequest')->with($this->equalTo('/select?qt=standard&wt=json&q=php'))->will($this->returnValue($response));
     $this->assertEquals(SolrSearchResult::parseResponse($solrResponse), $this->solr->query('php'));
 }
Example #2
0
 /**
  * Sends a query to the Solr server.
  * 
  * Although it is recommended to provide the query as a {@link SolrQuery}
  * object, it is also possible to pass it as a string. So, the following
  * two lines of code produce exactly the same result:
  * 
  * <code>
  * $result = $solr->query('hello world');
  * $result = $solr->query(new SolrQuery('hello world'));
  * </code>
  * 
  * Please refer to the {@link SolrQuery} class for more details on query
  * parameters and the query syntax.
  * 
  * @see SolrQuery
  * @uses json_decode()
  * 
  * @param SolrQuery|string $query The query.
  * 
  * @return SolrSearchResult
  */
 public function query($query)
 {
     Solr::autoload('SolrSearchResult');
     if (is_string($query)) {
         Solr::autoload('SolrQuery');
         $query = new SolrQuery($query);
     } elseif (!$query instanceof SolrQuery) {
         throw new InvalidArgumentException('Invalid query.');
     }
     $response = $this->httpClient->sendRequest('/select' . $query->getQueryString());
     return SolrSearchResult::parseResponse($response->getBody());
 }
 /**
  * Tests faceting.
  */
 function testFaceting()
 {
     $solrResponse = '{"responseHeader":{"status":0,"QTime":0,"params":{"facet":"true","q":"php","facet.limit":"3","facet.field":"author","qt":"standard","wt":"json","rows":"0"}},"response":{"numFound":241,"start":0,"docs":[]},"facet_counts":{"facet_queries":{"jahr:[* TO 2004]":42,"jahr:[2005 TO *]":24},"facet_fields":{"author":["foo",25,"bar",10,"foobar",5]}}}';
     $searchResult = SolrSearchResult::parseResponse($solrResponse);
     $actualFacets = $searchResult->facets;
     $this->assertType('SolrFacetCounts', $actualFacets);
     $expectedFacetFields = array('author' => array('foo' => 25, 'bar' => 10, 'foobar' => 5));
     $this->assertEquals($expectedFacetFields, $actualFacets->fields);
     $expectedFacetQueries = array('jahr:[* TO 2004]' => 42, 'jahr:[2005 TO *]' => 24);
     $this->assertEquals($expectedFacetQueries, $actualFacets->queries);
 }