Completed
Pull Request — 5.1 (#2266)
by
unknown
12:07
created

SearchServiceTest::testUnknownSearcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 0
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;
0 ignored issues
show
Bug introduced by
Accessing attributes on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
33
        $request->query = $parameterBag;
0 ignored issues
show
Bug introduced by
Accessing query on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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;
0 ignored issues
show
Bug introduced by
Accessing attributes on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
64
        $request->query = $parameterBag;
0 ignored issues
show
Bug introduced by
Accessing query on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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