Tests/Services/SearchServiceTest.php
changeset 68 e7384fb35f7a
parent 63 774ba82dca59
child 75 ca2a145e67f3
equal deleted inserted replaced
67:989d9e117586 68:e7384fb35f7a
    14 
    14 
    15 class SearchServiceTest extends \PHPUnit_Framework_TestCase
    15 class SearchServiceTest extends \PHPUnit_Framework_TestCase
    16 {
    16 {
    17 
    17 
    18     protected $_container;
    18     protected $_container;
       
    19     protected $_application;
       
    20     protected $_kernel;
       
    21     
    19     
    22     
    20     public function __construct()
    23     public function __construct()
    21     {
    24     {
    22         $kernel = new \AppKernel("test", true);
    25         $this->_kernel = new \AppKernel("test", true);
    23         $kernel->boot();
    26         $this->_kernel->boot();
    24         $this->_container = $kernel->getContainer();
    27         $this->_container = $this->_kernel->getContainer();
    25         parent::__construct();
    28         parent::__construct();
       
    29     }
       
    30 
       
    31     protected function runConsole($command, Array $options = array())
       
    32     {
       
    33     
       
    34         $options["-e"] = "test";
       
    35         $options["-q"] = null;
       
    36         $options = array_merge($options, array('command' => $command));
       
    37         return $this->_application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
       
    38     }
       
    39     
       
    40     public function setUp()
       
    41     {
       
    42         $this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->_kernel);
       
    43         $this->_application->setAutoExit(false);
       
    44         $this->runConsole("doctrine:schema:drop", array("--force" => true));
       
    45         $this->runConsole("wikitag:schema:create");
       
    46         $this->runConsole("cache:warmup");
       
    47         $this->runConsole("doctrine:fixtures:load"/*, array("--fixtures" => __DIR__ . "/../../../../../../../src/Company/BaseBundle/DataFixtures")*/);
    26     }
    48     }
    27     
    49     
    28     protected function get($service)
    50     protected function get($service)
    29     {
    51     {
    30         return $this->_container->get($service);
    52         return $this->_container->get($service);
    31     }
    53     }
    32     
    54     
    33     
    55     
    34     public function testTagCloud()
    56     public function testTagCloud()
    35     {
    57     {
    36         
       
    37         $search_service = $this->get("wiki_tag.search");
    58         $search_service = $this->get("wiki_tag.search");
    38         
    59         
    39         $result = $search_service->getTagCloud(30);
    60         $result = $search_service->getTagCloud(4);
    40         
    61         
    41         $this->assertNotNull($result, "tag cloud should not be null");
    62         $this->assertNotNull($result, "tag cloud should not be null");
    42         $this->assertLessThanOrEqual(30, count($result));
    63         $this->assertLessThanOrEqual(4, count($result));
       
    64         $this->assertEquals(4, count($result));
       
    65         
       
    66         // 0->tag4,6 ; 0->tag3,5 ; 0->tag2,4 ; 0->tag1,2
       
    67         $expected = array('Tag1'=>2,'Tag2'=>4, 'Tag3'=>5, 'Tag4'=>6);
       
    68         for ($i = 0; $i < 4; $i++) {
       
    69             $tagname = "Tag".(4-$i);
       
    70             $this->assertEquals($tagname,$result[$i]['label']);
       
    71             $this->assertEquals($expected[$tagname],$result[$i]['nb_docs']);
       
    72         }
       
    73         
    43     }
    74     }
    44 
    75 
       
    76 
       
    77     public function testTagCloudLimit()
       
    78     {
       
    79         $search_service = $this->get("wiki_tag.search");
       
    80     
       
    81         $result = $search_service->getTagCloud(2);
       
    82     
       
    83         $this->assertNotNull($result, "tag cloud should not be null");
       
    84         $this->assertEquals(2, count($result));
       
    85     
       
    86         $expected = array('Tag3'=>5, 'Tag4'=>6);
       
    87         for ($i = 0; $i < 2; $i++) {
       
    88             $tagname = "Tag".(4-$i);
       
    89             $this->assertEquals($tagname,$result[$i]['label']);
       
    90             $this->assertEquals($expected[$tagname],$result[$i]['nb_docs']);
       
    91         }
       
    92     
       
    93     }
    45     
    94     
    46     public function testCompletion()
    95     public function testCompletion()
    47     {
    96     {
    48     
       
    49         $search_service = $this->get("wiki_tag.search");
    97         $search_service = $this->get("wiki_tag.search");
    50     
    98     
    51         $result = $search_service->completion("tag");
    99         $result = $search_service->completion("tag");
    52     
   100     
    53         $this->assertNotNull($result, "tag cloud should not be null");
   101         $this->assertNotNull($result, "completion should not be null");
    54         $this->assertEquals(4, count($result));
   102         $this->assertEquals(4, count($result));
       
   103         
       
   104         foreach ($result as $tagname) {
       
   105             $this->assertEquals(0,strpos($tagname,"tag"));
       
   106         }
    55     }
   107     }
       
   108     
       
   109     public function testSearch() {
       
   110         
       
   111         $search_service = $this->get("wiki_tag.search");
       
   112         $result = $search_service->search("Suspendisse");
       
   113         
       
   114         $this->assertNotNull($result, "search result should not be null");
       
   115         $this->assertEquals(1, count($result));
       
   116         foreach (array("_score","host_doc_id", "wikitag_doc_id", "wikitag_doc", "title", "description", "categories") as $key) {
       
   117             $this->assertArrayHasKey($key,$result[0]);
       
   118             $this->assertNotNull($result[0][$key]);
       
   119         }
       
   120         $this->assertTrue($result[0]['_score']>0);
       
   121         $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['title']);
       
   122         $this->assertTrue(is_a($result[0]['wikitag_doc'], "\IRI\Bundle\WikiTagBundle\Model\DocumentInterface"));
       
   123         $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['wikitag_doc']->getTitle());
       
   124         
       
   125     }
       
   126 
       
   127     public function testSearchCondition() {
       
   128     
       
   129         $search_service = $this->get("wiki_tag.search");
       
   130         $result = $search_service->search("ullamcorper");
       
   131     
       
   132         $this->assertNotNull($result, "search result should not be null");
       
   133         $this->assertEquals(2, count($result));
       
   134         
       
   135         $result = $search_service->search("ullamcorper", array("tagsStr"=>array("operator"=>"like","value"=>"barfoo")));
       
   136         
       
   137         foreach (array("_score","host_doc_id", "wikitag_doc_id", "wikitag_doc", "title", "description", "categories") as $key) {
       
   138             $this->assertArrayHasKey($key,$result[0]);
       
   139             $this->assertNotNull($result[0][$key]);
       
   140         }
       
   141         $this->assertTrue($result[0]['_score']>0);
       
   142         $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['title']);
       
   143         $this->assertTrue(is_a($result[0]['wikitag_doc'], "\IRI\Bundle\WikiTagBundle\Model\DocumentInterface"));
       
   144         $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['wikitag_doc']->getTitle());
       
   145         $this->assertEquals("Lorem ipsum ullamcorper", $result[0]['description']);
       
   146     
       
   147     }
       
   148     
       
   149     public function testSearchWeight() {
       
   150         $search_service = $this->get("wiki_tag.search");
       
   151         
       
   152         $result = $search_service->search(array('title'=>array('weight'=>0.9, 'value'=>'caveat'), 'description'=>array('weight'=>0.1, 'value'=>'caveat')));
       
   153         
       
   154         $this->assertNotNull($result, "search result should not be null");
       
   155         $this->assertEquals(2, count($result));
       
   156         $this->assertTrue($result[0]['_score']>$result[1]['_score']);
       
   157         
       
   158         $this->assertEquals('caveat', $result[0]['title']);
       
   159         $this->assertEquals('emptor', $result[0]['description']);
       
   160 
       
   161         $result = $search_service->search(array('title'=>array('weight'=>0.1, 'value'=>'caveat'), 'description'=>array('weight'=>0.9, 'value'=>'caveat')));
       
   162         
       
   163         $this->assertNotNull($result, "search result should not be null");
       
   164         $this->assertEquals(2, count($result));
       
   165         $this->assertTrue($result[0]['_score']>$result[1]['_score']);
       
   166         
       
   167         $this->assertEquals('caveat', $result[0]['description']);
       
   168         $this->assertEquals('emptor', $result[0]['title']);
       
   169         
       
   170         
       
   171     }
       
   172     
    56     
   173     
    57     
   174     
    58 }
   175 }
    59 
   176