|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkSearchBundle\Tests\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkSearchBundle\Helper\Search; |
|
6
|
|
|
use SumoCoders\FrameworkSearchBundle\Entity\SearchResult; |
|
7
|
|
|
|
|
8
|
|
|
class SearchTest extends \PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public function testSuccesfullSearch() |
|
11
|
|
|
{ |
|
12
|
|
|
$sqlResult = array( |
|
13
|
|
|
'\Some\Class' => array(12 => 2), |
|
14
|
|
|
); |
|
15
|
|
|
$eventResult = array( |
|
16
|
|
|
new SearchResult('\Some\Class', 12, 'TestBundle', 'Test', 'test') |
|
17
|
|
|
); |
|
18
|
|
|
$repository = $this->getRepositoryMock($sqlResult); |
|
19
|
|
|
$eventDispatcher = $this->getEventDispatcherMock($eventResult); |
|
20
|
|
|
|
|
21
|
|
|
$search = new Search($repository, $eventDispatcher, 'test'); |
|
22
|
|
|
$results = $search->search(); |
|
23
|
|
|
|
|
24
|
|
|
// our item should still be here, because it's found in both sql and event |
|
25
|
|
|
$this->assertCount(1, $results); |
|
26
|
|
|
|
|
27
|
|
|
// the weight should be available in our resultset |
|
28
|
|
|
$this->assertEquals(2, $results[0]->getWeight()); |
|
29
|
|
|
|
|
30
|
|
|
// our item should still have the same id, class and title |
|
31
|
|
|
$this->assertEquals($eventResult[0]->getId(), $results[0]->getId()); |
|
32
|
|
|
$this->assertEquals($eventResult[0]->getClass(), $results[0]->getClass()); |
|
33
|
|
|
$this->assertEquals($eventResult[0]->getTitle(), $results[0]->getTitle()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testSearchNoEventResults() |
|
37
|
|
|
{ |
|
38
|
|
|
$sqlResult = array( |
|
39
|
|
|
'\Some\Class' => array(12 => 2), |
|
40
|
|
|
); |
|
41
|
|
|
$eventResult = array(); |
|
42
|
|
|
$repository = $this->getRepositoryMock($sqlResult); |
|
43
|
|
|
$eventDispatcher = $this->getEventDispatcherMock($eventResult); |
|
44
|
|
|
|
|
45
|
|
|
$search = new Search($repository, $eventDispatcher, 'test'); |
|
46
|
|
|
$results = $search->search(); |
|
47
|
|
|
|
|
48
|
|
|
// our item does not exist anymore |
|
49
|
|
|
$this->assertCount(0, $results); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testSearchWrongSorting() |
|
53
|
|
|
{ |
|
54
|
|
|
$sqlResult = array( |
|
55
|
|
|
'\Some\Class' => array(3 => 1, 12 => 2), |
|
56
|
|
|
); |
|
57
|
|
|
$eventResult = array( |
|
58
|
|
|
new SearchResult('\Some\Class', 3, 'TestBundle', 'Test', 'test'), |
|
59
|
|
|
new SearchResult('\Some\Class', 12, 'TestBundle', 'Test', 'test') |
|
60
|
|
|
); |
|
61
|
|
|
$repository = $this->getRepositoryMock($sqlResult); |
|
62
|
|
|
$eventDispatcher = $this->getEventDispatcherMock($eventResult); |
|
63
|
|
|
|
|
64
|
|
|
$search = new Search($repository, $eventDispatcher, 'test'); |
|
65
|
|
|
$results = $search->search(); |
|
66
|
|
|
|
|
67
|
|
|
// our items should still be here, because they are found in both sql and event |
|
68
|
|
|
$this->assertCount(2, $results); |
|
69
|
|
|
|
|
70
|
|
|
// the order should be from high weight to low weight |
|
71
|
|
|
$this->assertEquals(2, $results[0]->getWeight()); |
|
72
|
|
|
$this->assertEquals(1, $results[1]->getWeight()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testSearchCorrectSorting() |
|
76
|
|
|
{ |
|
77
|
|
|
$sqlResult = array( |
|
78
|
|
|
'\Some\Class' => array(3 => 1, 4 => 1), |
|
79
|
|
|
'\Other\Class' => array(12 => 2), |
|
80
|
|
|
); |
|
81
|
|
|
$eventResult = array( |
|
82
|
|
|
new SearchResult('\Some\Class', 3, 'TestBundle', 'Test', 'test'), |
|
83
|
|
|
new SearchResult('\Some\Class', 4, 'TestBundle', 'Test', 'test'), |
|
84
|
|
|
new SearchResult('\Other\Class', 12, 'TestBundle', 'Test', 'test') |
|
85
|
|
|
); |
|
86
|
|
|
$repository = $this->getRepositoryMock($sqlResult); |
|
87
|
|
|
$eventDispatcher = $this->getEventDispatcherMock($eventResult); |
|
88
|
|
|
|
|
89
|
|
|
$search = new Search($repository, $eventDispatcher, 'test'); |
|
90
|
|
|
$results = $search->search(); |
|
91
|
|
|
|
|
92
|
|
|
// our items should still be here, because they are found in both sql and event |
|
93
|
|
|
$this->assertCount(3, $results); |
|
94
|
|
|
|
|
95
|
|
|
// the order should be from high weight to low weight |
|
96
|
|
|
$this->assertEquals(2, $results[0]->getWeight()); |
|
97
|
|
|
$this->assertEquals(1, $results[1]->getWeight()); |
|
98
|
|
|
$this->assertEquals(1, $results[2]->getWeight()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function getRepositoryMock($sqlResult) |
|
102
|
|
|
{ |
|
103
|
|
|
$repository = $this->getMockBuilder('SumoCoders\FrameworkSearchBundle\Entity\IndexItemRepository') |
|
104
|
|
|
->disableOriginalConstructor() |
|
105
|
|
|
->getMock(); |
|
106
|
|
|
|
|
107
|
|
|
$repository->method('search')->willReturn($sqlResult); |
|
108
|
|
|
|
|
109
|
|
|
return $repository; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function getEventDispatcherMock($eventResult) |
|
113
|
|
|
{ |
|
114
|
|
|
$eventDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
|
115
|
|
|
->disableOriginalConstructor() |
|
116
|
|
|
->getMock(); |
|
117
|
|
|
|
|
118
|
|
|
$eventDispatcher->method('dispatch') |
|
119
|
|
|
->will($this->returnCallback( |
|
120
|
|
|
function ($eventName, $event) use ($eventResult) { |
|
121
|
|
|
foreach ($eventResult as $item) { |
|
122
|
|
|
$event->addResult($item); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
)); |
|
126
|
|
|
|
|
127
|
|
|
return $eventDispatcher; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|