|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zikula\SearchModule\Tests\Api; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
|
18
|
|
|
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface; |
|
19
|
|
|
use Zikula\SearchModule\Api\ApiInterface\SearchApiInterface; |
|
20
|
|
|
use Zikula\SearchModule\Api\SearchApi; |
|
21
|
|
|
use Zikula\SearchModule\Collector\SearchableModuleCollector; |
|
22
|
|
|
use Zikula\SearchModule\Entity\RepositoryInterface\SearchStatRepositoryInterface; |
|
23
|
|
|
use Zikula\SearchModule\Entity\SearchStatEntity; |
|
24
|
|
|
use Zikula\SearchModule\Tests\Api\Fixtures\MockSearchStatRepository; |
|
25
|
|
|
use Zikula\SearchModule\Tests\Api\Fixtures\SearchableBar; |
|
26
|
|
|
use Zikula\SearchModule\Tests\Api\Fixtures\SearchableFoo; |
|
27
|
|
|
use Zikula\SearchModule\Tests\Api\Fixtures\MockSearchResultRepository; |
|
28
|
|
|
|
|
29
|
|
|
class SearchApiTest extends \PHPUnit_Framework_TestCase |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var SearchStatRepositoryInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $searchStatRepo; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* SearchApiTest setUp. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setUp() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->searchStatRepo = new MockSearchStatRepository(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testInstance() |
|
45
|
|
|
{ |
|
46
|
|
|
$api = $this->getApi(); |
|
47
|
|
|
$this->assertInstanceOf(SearchApiInterface::class, $api); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testSearch() |
|
51
|
|
|
{ |
|
52
|
|
|
$api = $this->getApi(); |
|
53
|
|
|
$searchResult = $api->search('bar', true); |
|
54
|
|
|
$this->assertEquals(1, $searchResult['resultCount']); |
|
55
|
|
|
$this->assertCount(1, $searchResult['sqlResult']); |
|
56
|
|
|
$this->assertCount(0, $searchResult['errors']); |
|
57
|
|
|
$this->assertEquals('test', $searchResult['sqlResult'][0]->getSesid()); |
|
58
|
|
|
$this->assertEquals('ZikulaFooModule found using bar', $searchResult['sqlResult'][0]->getText()); |
|
59
|
|
|
$this->assertEquals('ZikulaFooModule result', $searchResult['sqlResult'][0]->getTitle()); |
|
60
|
|
|
|
|
61
|
|
|
$searchResult = $api->search('fee', true); |
|
62
|
|
|
$this->assertEquals(0, $searchResult['resultCount']); |
|
63
|
|
|
$this->assertCount(0, $searchResult['sqlResult']); |
|
64
|
|
|
$this->assertCount(0, $searchResult['errors']); |
|
65
|
|
|
|
|
66
|
|
|
$searchResult = $api->search('top bar fee', true); |
|
67
|
|
|
$this->assertEquals(2, $searchResult['resultCount']); |
|
68
|
|
|
$this->assertCount(2, $searchResult['sqlResult']); |
|
69
|
|
|
$this->assertCount(0, $searchResult['errors']); |
|
70
|
|
|
$this->assertEquals('test', $searchResult['sqlResult'][1]->getSesid()); |
|
71
|
|
|
$this->assertEquals('ZikulaFooModule found using top, bar, fee', $searchResult['sqlResult'][1]->getText()); |
|
72
|
|
|
$this->assertEquals('ZikulaFooModule result', $searchResult['sqlResult'][1]->getTitle()); |
|
73
|
|
|
$this->assertEquals('ZikulaBarModule found using top, bar, fee', $searchResult['sqlResult'][2]->getText()); |
|
74
|
|
|
$this->assertEquals('ZikulaBarModule result', $searchResult['sqlResult'][2]->getTitle()); |
|
75
|
|
|
|
|
76
|
|
|
$searchResult = $api->search('top bar fee', true, 'EXACT'); |
|
77
|
|
|
$this->assertEquals(0, $searchResult['resultCount']); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testLog() |
|
81
|
|
|
{ |
|
82
|
|
|
$api = $this->getApi(); |
|
83
|
|
|
$api->log('foo'); |
|
84
|
|
|
$this->assertEquals(1, $this->searchStatRepo->countStats()); |
|
85
|
|
|
$api->log('bar'); |
|
86
|
|
|
$this->assertEquals(2, $this->searchStatRepo->countStats()); |
|
87
|
|
|
$api->log('bar'); |
|
88
|
|
|
$this->assertEquals(2, $this->searchStatRepo->countStats()); |
|
89
|
|
|
$barLog = $this->searchStatRepo->findOneBy(['search' => 'bar']); |
|
90
|
|
|
$this->assertInstanceOf(SearchStatEntity::class, $barLog); |
|
91
|
|
|
$this->assertEquals(2, $barLog->getCount()); |
|
92
|
|
|
$fooLog = $this->searchStatRepo->findOneBy(['search' => 'foo']); |
|
93
|
|
|
$this->assertInstanceOf(SearchStatEntity::class, $fooLog); |
|
94
|
|
|
$this->assertEquals(1, $fooLog->getCount()); |
|
95
|
|
|
$api->log('foo'); |
|
96
|
|
|
$api->log('foo'); |
|
97
|
|
|
$fooLog = $this->searchStatRepo->findOneBy(['search' => 'foo']); |
|
98
|
|
|
$this->assertEquals(3, $fooLog->getCount()); |
|
99
|
|
|
$this->assertEquals(2, $this->searchStatRepo->countStats()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function getApi() |
|
103
|
|
|
{ |
|
104
|
|
|
$variableApi = $this->getMockBuilder(VariableApiInterface::class)->getMock(); |
|
105
|
|
|
$variableApi->method('get')->willReturnArgument(2); |
|
106
|
|
|
$searchResultRepo = new MockSearchResultRepository(); |
|
107
|
|
|
$searchableModuleCollector = new SearchableModuleCollector(); |
|
108
|
|
|
$searchableModuleCollector->add('ZikulaFooModule', new SearchableFoo()); |
|
109
|
|
|
$searchableModuleCollector->add('ZikulaBarModule', new SearchableBar()); |
|
110
|
|
|
$storage = new MockArraySessionStorage(); |
|
111
|
|
|
$session = new Session($storage, new AttributeBag(), new FlashBag()); |
|
112
|
|
|
$session->setId('test'); |
|
113
|
|
|
$session->start(); |
|
114
|
|
|
|
|
115
|
|
|
return new SearchApi($variableApi, $searchResultRepo, $this->searchStatRepo, $session, $searchableModuleCollector); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|