Tests/Services/SearchServiceTest.php
changeset 68 e7384fb35f7a
parent 63 774ba82dca59
child 75 ca2a145e67f3
--- a/Tests/Services/SearchServiceTest.php	Mon Dec 26 22:53:50 2011 +0100
+++ b/Tests/Services/SearchServiceTest.php	Mon Jan 23 00:48:55 2012 +0100
@@ -16,14 +16,36 @@
 {
 
     protected $_container;
+    protected $_application;
+    protected $_kernel;
+    
     
     public function __construct()
     {
-        $kernel = new \AppKernel("test", true);
-        $kernel->boot();
-        $this->_container = $kernel->getContainer();
+        $this->_kernel = new \AppKernel("test", true);
+        $this->_kernel->boot();
+        $this->_container = $this->_kernel->getContainer();
         parent::__construct();
     }
+
+    protected function runConsole($command, Array $options = array())
+    {
+    
+        $options["-e"] = "test";
+        $options["-q"] = null;
+        $options = array_merge($options, array('command' => $command));
+        return $this->_application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
+    }
+    
+    public function setUp()
+    {
+        $this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->_kernel);
+        $this->_application->setAutoExit(false);
+        $this->runConsole("doctrine:schema:drop", array("--force" => true));
+        $this->runConsole("wikitag:schema:create");
+        $this->runConsole("cache:warmup");
+        $this->runConsole("doctrine:fixtures:load"/*, array("--fixtures" => __DIR__ . "/../../../../../../../src/Company/BaseBundle/DataFixtures")*/);
+    }
     
     protected function get($service)
     {
@@ -33,27 +55,122 @@
     
     public function testTagCloud()
     {
-        
         $search_service = $this->get("wiki_tag.search");
         
-        $result = $search_service->getTagCloud(30);
+        $result = $search_service->getTagCloud(4);
         
         $this->assertNotNull($result, "tag cloud should not be null");
-        $this->assertLessThanOrEqual(30, count($result));
+        $this->assertLessThanOrEqual(4, count($result));
+        $this->assertEquals(4, count($result));
+        
+        // 0->tag4,6 ; 0->tag3,5 ; 0->tag2,4 ; 0->tag1,2
+        $expected = array('Tag1'=>2,'Tag2'=>4, 'Tag3'=>5, 'Tag4'=>6);
+        for ($i = 0; $i < 4; $i++) {
+            $tagname = "Tag".(4-$i);
+            $this->assertEquals($tagname,$result[$i]['label']);
+            $this->assertEquals($expected[$tagname],$result[$i]['nb_docs']);
+        }
+        
     }
 
+
+    public function testTagCloudLimit()
+    {
+        $search_service = $this->get("wiki_tag.search");
+    
+        $result = $search_service->getTagCloud(2);
+    
+        $this->assertNotNull($result, "tag cloud should not be null");
+        $this->assertEquals(2, count($result));
+    
+        $expected = array('Tag3'=>5, 'Tag4'=>6);
+        for ($i = 0; $i < 2; $i++) {
+            $tagname = "Tag".(4-$i);
+            $this->assertEquals($tagname,$result[$i]['label']);
+            $this->assertEquals($expected[$tagname],$result[$i]['nb_docs']);
+        }
+    
+    }
     
     public function testCompletion()
     {
-    
         $search_service = $this->get("wiki_tag.search");
     
         $result = $search_service->completion("tag");
     
-        $this->assertNotNull($result, "tag cloud should not be null");
+        $this->assertNotNull($result, "completion should not be null");
         $this->assertEquals(4, count($result));
+        
+        foreach ($result as $tagname) {
+            $this->assertEquals(0,strpos($tagname,"tag"));
+        }
     }
     
+    public function testSearch() {
+        
+        $search_service = $this->get("wiki_tag.search");
+        $result = $search_service->search("Suspendisse");
+        
+        $this->assertNotNull($result, "search result should not be null");
+        $this->assertEquals(1, count($result));
+        foreach (array("_score","host_doc_id", "wikitag_doc_id", "wikitag_doc", "title", "description", "categories") as $key) {
+            $this->assertArrayHasKey($key,$result[0]);
+            $this->assertNotNull($result[0][$key]);
+        }
+        $this->assertTrue($result[0]['_score']>0);
+        $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['title']);
+        $this->assertTrue(is_a($result[0]['wikitag_doc'], "\IRI\Bundle\WikiTagBundle\Model\DocumentInterface"));
+        $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['wikitag_doc']->getTitle());
+        
+    }
+
+    public function testSearchCondition() {
+    
+        $search_service = $this->get("wiki_tag.search");
+        $result = $search_service->search("ullamcorper");
+    
+        $this->assertNotNull($result, "search result should not be null");
+        $this->assertEquals(2, count($result));
+        
+        $result = $search_service->search("ullamcorper", array("tagsStr"=>array("operator"=>"like","value"=>"barfoo")));
+        
+        foreach (array("_score","host_doc_id", "wikitag_doc_id", "wikitag_doc", "title", "description", "categories") as $key) {
+            $this->assertArrayHasKey($key,$result[0]);
+            $this->assertNotNull($result[0][$key]);
+        }
+        $this->assertTrue($result[0]['_score']>0);
+        $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['title']);
+        $this->assertTrue(is_a($result[0]['wikitag_doc'], "\IRI\Bundle\WikiTagBundle\Model\DocumentInterface"));
+        $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['wikitag_doc']->getTitle());
+        $this->assertEquals("Lorem ipsum ullamcorper", $result[0]['description']);
+    
+    }
+    
+    public function testSearchWeight() {
+        $search_service = $this->get("wiki_tag.search");
+        
+        $result = $search_service->search(array('title'=>array('weight'=>0.9, 'value'=>'caveat'), 'description'=>array('weight'=>0.1, 'value'=>'caveat')));
+        
+        $this->assertNotNull($result, "search result should not be null");
+        $this->assertEquals(2, count($result));
+        $this->assertTrue($result[0]['_score']>$result[1]['_score']);
+        
+        $this->assertEquals('caveat', $result[0]['title']);
+        $this->assertEquals('emptor', $result[0]['description']);
+
+        $result = $search_service->search(array('title'=>array('weight'=>0.1, 'value'=>'caveat'), 'description'=>array('weight'=>0.9, 'value'=>'caveat')));
+        
+        $this->assertNotNull($result, "search result should not be null");
+        $this->assertEquals(2, count($result));
+        $this->assertTrue($result[0]['_score']>$result[1]['_score']);
+        
+        $this->assertEquals('caveat', $result[0]['description']);
+        $this->assertEquals('emptor', $result[0]['title']);
+        
+        
+    }
+    
+    
     
 }