1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeSearchBundle\Tests\unit\Services; |
4
|
|
|
|
5
|
|
|
use Elastica\Query; |
6
|
|
|
use Kunstmaan\NodeSearchBundle\Entity\AbstractSearchPage; |
7
|
|
|
use Kunstmaan\NodeSearchBundle\Search\AbstractElasticaSearcher; |
8
|
|
|
use Kunstmaan\NodeSearchBundle\Services\SearchService; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
11
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
14
|
|
|
|
15
|
|
|
class SearchServiceTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @group legacy |
19
|
|
|
* @expectedDeprecation Getting the node searcher "%s" from the container is deprecated in KunstmaanNodeSearchBundle 5.2 and will be removed in KunstmaanNodeSearchBundle 6.0. Tag your searcher service with the "kunstmaan_node_search.node_searcher" tag to add a searcher. |
20
|
|
|
*/ |
21
|
|
|
public function testLegacyContainerSearcher() |
22
|
|
|
{ |
23
|
|
|
$entity = new AbstractSearchPage(); |
24
|
|
|
$parameterBag = new ParameterBag(); |
25
|
|
|
$parameterBag->add([ |
26
|
|
|
'_entity' => $entity, |
27
|
|
|
'query' => '', |
28
|
|
|
'type' => 'html/text', |
29
|
|
|
]); |
30
|
|
|
|
31
|
|
|
$request = $this->createMock(Request::class); |
32
|
|
|
$request->attributes = $parameterBag; |
|
|
|
|
33
|
|
|
$request->query = $parameterBag; |
|
|
|
|
34
|
|
|
|
35
|
|
|
$elasticSearcher = $this->createMock(AbstractElasticaSearcher::class); |
36
|
|
|
$elasticSearcher->method('setData')->willReturnSelf(); |
37
|
|
|
$elasticSearcher->method('setContentType')->willReturnSelf(); |
38
|
|
|
$elasticSearcher->method('getQuery')->willReturn(new Query()); |
39
|
|
|
|
40
|
|
|
$container = $this->createMock(ContainerInterface::class); |
41
|
|
|
$container->method('get')->with('kunstmaan_node_search.search.node')->willReturn($elasticSearcher); |
42
|
|
|
|
43
|
|
|
$requestStack = $this->createMock(RequestStack::class); |
44
|
|
|
$requestStack->method('getCurrentRequest')->willReturn($request); |
45
|
|
|
|
46
|
|
|
$searcher = new SearchService($container, $requestStack, 10, []); |
47
|
|
|
|
48
|
|
|
$searcher->search(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException |
53
|
|
|
*/ |
54
|
|
|
public function testUnknownSearcher() |
55
|
|
|
{ |
56
|
|
|
$entity = $this->createMock(AbstractSearchPage::class); |
57
|
|
|
$entity->method('getSearcher')->willReturn('unknown_searcher'); |
58
|
|
|
|
59
|
|
|
$parameterBag = new ParameterBag(); |
60
|
|
|
$parameterBag->set('_entity', $entity); |
61
|
|
|
|
62
|
|
|
$request = $this->createMock(Request::class); |
63
|
|
|
$request->attributes = $parameterBag; |
|
|
|
|
64
|
|
|
$request->query = $parameterBag; |
|
|
|
|
65
|
|
|
|
66
|
|
|
$container = $this->createMock(ContainerInterface::class); |
67
|
|
|
$container->method('get')->with('unknown_searcher')->willThrowException(new ServiceNotFoundException('unknown_searcher')); |
68
|
|
|
|
69
|
|
|
$requestStack = $this->createMock(RequestStack::class); |
70
|
|
|
$requestStack->method('getCurrentRequest')->willReturn($request); |
71
|
|
|
|
72
|
|
|
$searcher = new SearchService($container, $requestStack, 10, []); |
73
|
|
|
|
74
|
|
|
$searcher->search(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: