Completion service add the nb of doc + test
authorymh <ymh.work@gmail.com>
Thu, 23 Feb 2012 23:02:07 +0100
changeset 75 ca2a145e67f3
parent 74 901463f9b11c
child 77 021131fbe2c5
Completion service add the nb of doc + test
Entity/TagRepository.php
Services/SearchService.php
Tests/Services/SearchServiceTest.php
phpunit.xml.dist
--- a/Entity/TagRepository.php	Tue Feb 21 15:15:22 2012 +0100
+++ b/Entity/TagRepository.php	Thu Feb 23 23:02:07 2012 +0100
@@ -44,8 +44,10 @@
     public function getCompletion($seed)
     {
         $qb = $this->getEntityManager()->createQueryBuilder();
-        $qb->select('DISTINCT t.label');
+        $qb->select('t.label', 'COUNT(dt.id) AS nb_docs');
         $qb->from('WikiTagBundle:Tag','t');
+        $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
+        $qb->addGroupBy('t.label');
         $qb->where($qb->expr()->orx(
             $qb->expr()->like('t.label',$qb->expr()->literal(addcslashes(mysql_real_escape_string($seed),"%_")."%")),
             $qb->expr()->like('t.label',$qb->expr()->literal("%".addcslashes(mysql_real_escape_string($seed),"%_"))),
--- a/Services/SearchService.php	Tue Feb 21 15:15:22 2012 +0100
+++ b/Services/SearchService.php	Thu Feb 23 23:02:07 2012 +0100
@@ -49,12 +49,12 @@
     
     
     /**
+     * Search.
      *
-     * Enter description here ...
      * @param mixed $value : either a string or the list of fields to search into ; format : ['<field_name>' => ['weight'=><relative weight of the field>, 'value' => value]]
      * @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operator is IN) or array("operator"=>"=","!=","<".">","<=".">=","like","ilike","in">, "value"=>value)
      * @param array $fields : The field of field names to export, one of the list specified in the configuration file
-     * @return array []
+     * @return array [{'host_doc_id'=>,'wikitag_doc_id'=>, 'wikitag_doc'=>, '_score'=>,'field1'=>, 'field2'=>, }]
      */
     public function search($value, array $conditions = null, array $fields = null)
     {
@@ -145,16 +145,17 @@
     /**
      * List the tag label containing the seed given as an argument.
      * The seed is either at the beggining, the end of the label or at the beggining of a word.
-     * @param unknown_type $seed
-     * @return : an array containing the possible tag labels
+     * @param string $seed
+     * @param boolean $doc_nb
+     * @return : an array containing the possible tag labels with the number of documents
      */
-    public function completion($seed)
+    public function completion($seed, $doc_nb=False)
     {
         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
         
         $res = array();
         foreach ($rep->getCompletion($seed) as $value) {
-            $res[] = $value['label'];
+            $res[] = array('label' => $value['label'], 'nb_docs' => $value['nb_docs']);
         }
         
         return $res;
--- a/Tests/Services/SearchServiceTest.php	Tue Feb 21 15:15:22 2012 +0100
+++ b/Tests/Services/SearchServiceTest.php	Thu Feb 23 23:02:07 2012 +0100
@@ -97,12 +97,20 @@
         $search_service = $this->get("wiki_tag.search");
     
         $result = $search_service->completion("tag");
+        $completion_result = array(
+        	'Tag1' => 2,
+        	'Tag2' => 4,
+        	'Tag3' => 5,
+            'Tag4' => 6
+        );
     
         $this->assertNotNull($result, "completion should not be null");
         $this->assertEquals(4, count($result));
         
         foreach ($result as $tagname) {
-            $this->assertEquals(0,strpos($tagname,"tag"));
+            $this->assertEquals(0,strpos($tagname['label'],"Tag"));
+            $this->assertArrayHasKey($tagname['label'], $completion_result);
+            $this->assertEquals($completion_result[$tagname['label']], $tagname['nb_docs']);
         }
     }
     
--- a/phpunit.xml.dist	Tue Feb 21 15:15:22 2012 +0100
+++ b/phpunit.xml.dist	Thu Feb 23 23:02:07 2012 +0100
@@ -1,3 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
 <!--
   This file is part of the WikiTagBundle package.
 
@@ -6,7 +7,7 @@
   For the full copyright and license information, please view the LICENSE
   file that was distributed with this source code.
 -->
-<?xml version="1.0" encoding="UTF-8"?>
+
 
 <!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
 <!-- command line in the host application : php bin/phpunit.php  -d memory_limit=512M -c vendor/bundles/IRI/Bundle/WikiTagBundle vendor/bundles/IRI/Bundle/WikiTagBundle/Tests/Services/SearchServiceTest.php -->