1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: FlorianMeyer |
5
|
|
|
* Date: 06.03.2019 |
6
|
|
|
* Time: 09:48 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace StingerSoft\EntitySearchBundle\Tests\Controller; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Persistence\AbstractManagerRegistry; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
use StingerSoft\EntitySearchBundle\Controller\SearchController; |
14
|
|
|
use StingerSoft\EntitySearchBundle\Form\FacetType; |
15
|
|
|
use StingerSoft\EntitySearchBundle\Form\QueryType; |
16
|
|
|
use StingerSoft\EntitySearchBundle\Services\Mapping\DocumentToEntityMapper; |
17
|
|
|
use StingerSoft\EntitySearchBundle\Services\SearchService; |
18
|
|
|
use StingerSoft\EntitySearchBundle\Tests\AbstractORMTestCase; |
19
|
|
|
use StingerSoft\EntitySearchBundle\Tests\Fixtures\ORM\Beer; |
20
|
|
|
use Symfony\Component\Form\Extension\HttpFoundation\Type\FormTypeHttpFoundationExtension; |
21
|
|
|
use Symfony\Component\Form\Forms; |
22
|
|
|
use Symfony\Component\Form\ResolvedFormTypeFactory; |
23
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
26
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
27
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
28
|
|
|
|
29
|
|
|
class SearchControllerTest extends AbstractORMTestCase { |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @throws \Doctrine\ORM\ORMException |
33
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
34
|
|
|
*/ |
35
|
|
|
public function testAutocomplete(): void { |
36
|
|
|
$request = new Request(['term' => 'He']); |
37
|
|
|
$searchService = $this->getDummySearchService(); |
38
|
|
|
|
39
|
|
|
$controller = $this->getSearchController($searchService); |
40
|
|
|
$response = $controller->autocompleteAction($request); |
41
|
|
|
$objResponse = \json_decode($response->getContent(), true); |
42
|
|
|
$this->assertEmpty($objResponse); |
43
|
|
|
|
44
|
|
|
$this->indexBeer($searchService); |
45
|
|
|
$response = $controller->autocompleteAction($request); |
46
|
|
|
$objResponse = \json_decode($response->getContent(), true); |
47
|
|
|
$this->assertCount(1, $objResponse); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testNewSearch(): void { |
51
|
|
|
$request = new Request(['term' => 'He']); |
52
|
|
|
$session = new Session(new MockArraySessionStorage()); |
53
|
|
|
$request->setSession($session); |
54
|
|
|
|
55
|
|
|
$controller = $this->getSearchController(); |
56
|
|
|
|
57
|
|
|
$response = $controller->searchAction($request, $this->getDocumentToEntityMapper()); |
58
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
59
|
|
|
$this->assertEquals('He', $session->get('stinger_soft_entity_search_term')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function render(): Response { |
63
|
|
|
return new Response(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testSearch(): void { |
67
|
|
|
$request = new Request(); |
68
|
|
|
$session = new Session(new MockArraySessionStorage()); |
69
|
|
|
$session->set('stinger_soft_entity_search_term', 'He'); |
70
|
|
|
$request->setSession($session); |
71
|
|
|
|
72
|
|
|
$controller = $this->getSearchController(); |
73
|
|
|
|
74
|
|
|
$response = $controller->searchAction($request, $this->getDocumentToEntityMapper()); |
75
|
|
|
$this->assertInstanceOf(Response::class, $response); |
76
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function getSearchController(?SearchService $searchService = null): SearchController { |
81
|
|
|
$searchService = $searchService ?? $this->getDummySearchService(); |
82
|
|
|
|
83
|
|
|
$controller = $this->getMockBuilder(SearchController::class)->setMethods(array( |
84
|
|
|
'redirectToRoute', |
85
|
|
|
'getParameter' |
86
|
|
|
))->disableOriginalConstructor()->getMock(); |
87
|
|
|
|
88
|
|
|
$controller->method('redirectToRoute')->willReturn(new RedirectResponse('https://www.stinger-soft.net')); |
89
|
|
|
$controller->method('getParameter')->willReturn([]); |
90
|
|
|
|
91
|
|
|
$that = $this; |
92
|
|
|
|
93
|
|
|
$container = $this->getMockBuilder(ContainerInterface::class)->setMethods([ |
94
|
|
|
'get', 'has' |
95
|
|
|
])->disableOriginalConstructor()->getMockForAbstractClass(); |
96
|
|
|
$container->method('has')->willReturnCallback(function($id) { |
97
|
|
|
return \in_array($id, ['twig', 'form.factory'], true); |
98
|
|
|
}); |
99
|
|
|
$container->method('get')->willReturnCallback(function($id) use ($that) { |
100
|
|
|
if($id === 'form.factory') { |
101
|
|
|
$resolvedFormTypeFactory = new ResolvedFormTypeFactory(); |
102
|
|
|
$formTypeFactory = Forms::createFormFactoryBuilder() |
103
|
|
|
->addType(new FacetType()) |
104
|
|
|
->addType(new QueryType()) |
105
|
|
|
->addTypeExtension(new FormTypeHttpFoundationExtension()) |
106
|
|
|
->setResolvedTypeFactory($resolvedFormTypeFactory) |
107
|
|
|
->getFormFactory(); |
108
|
|
|
return $formTypeFactory; |
109
|
|
|
} |
110
|
|
|
if($id === 'twig') { |
111
|
|
|
return $that; |
112
|
|
|
} |
113
|
|
|
}); |
114
|
|
|
|
115
|
|
|
$controller->setSearchService($searchService); |
116
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
117
|
|
|
$controller->setContainer($container); |
118
|
|
|
|
119
|
|
|
return $controller; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param SearchService $service |
124
|
|
|
* @param string $title |
125
|
|
|
* @throws \Doctrine\ORM\ORMException |
126
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
127
|
|
|
*/ |
128
|
|
|
protected function indexBeer(SearchService $service, $title = 'Hemelinger'): void { |
129
|
|
|
$beer = new Beer(); |
130
|
|
|
$beer->setTitle($title); |
131
|
|
|
$this->em->persist($beer); |
132
|
|
|
$this->em->flush(); |
133
|
|
|
|
134
|
|
|
$document = $service->createEmptyDocumentFromEntity($beer); |
|
|
|
|
135
|
|
|
$beer->indexEntity($document); |
136
|
|
|
$service->saveDocument($document); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* |
141
|
|
|
* @return \StingerSoft\EntitySearchBundle\Services\Mapping\DocumentToEntityMapper |
142
|
|
|
*/ |
143
|
|
|
protected function getDocumentToEntityMapper(): DocumentToEntityMapper { |
144
|
|
|
$managerMock = $this->getMockBuilder(AbstractManagerRegistry::class)->setMethods(array( |
145
|
|
|
'getManagerForClass' |
146
|
|
|
))->disableOriginalConstructor()->getMockForAbstractClass(); |
147
|
|
|
$managerMock->method('getManagerForClass')->willReturn($this->em); |
148
|
|
|
return new DocumentToEntityMapper($managerMock); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* |
153
|
|
|
* {@inheritDoc} |
154
|
|
|
* |
155
|
|
|
* @see \StingerSoft\EntitySearchBundle\Tests\AbstractTestCase::getUsedEntityFixtures() |
156
|
|
|
*/ |
157
|
|
|
protected function getUsedEntityFixtures(): array { |
158
|
|
|
return array( |
159
|
|
|
Beer::class, |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: