Completed
Push — master ( 36acf6...c2375b )
by
unknown
13:20
created

ForecastWidgetBusinessUnitSearchHandlerTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 1 Features 3
Metric Value
wmc 2
c 3
b 1
f 3
lcom 1
cbo 6
dl 0
loc 126
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
B testApplyBusinessUnitAcl() 0 98 1
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Tests\Unit\Autocomplete;
4
5
use OroCRM\Bundle\SalesBundle\Autocomplete\ForecastWidgetBusinessUnitSearchHandler;
6
7
class ForecastWidgetBusinessUnitSearchHandlerTest extends \PHPUnit_Framework_TestCase
8
{
9
    const TEST_ID_FIELD = 'id';
10
    const TEST_ENTITY_NAME = 'OroOrganizationBundle:BusinessUnit';
11
    const TEST_ENTITY_ALIAS = 'business_alias';
12
13
    /** @var \PHPUnit_Framework_MockObject_MockObject */
14
    protected $businessAclProvider;
15
16
    /** @var \PHPUnit_Framework_MockObject_MockObject */
17
    protected $handler;
18
19
    public function setUp()
20
    {
21
        $this->businessAclProvider = $this
22
            ->getMockBuilder('Oro\Bundle\OrganizationBundle\Provider\BusinessUnitAclProvider')
23
            ->disableOriginalConstructor()
24
            ->getMock();
25
26
        $this->handler = new ForecastWidgetBusinessUnitSearchHandler(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \OroCRM\Bundle\Sales...lesBundle:Opportunity') of type object<OroCRM\Bundle\Sal...inessUnitSearchHandler> is incompatible with the declared type object<PHPUnit_Framework_MockObject_MockObject> of property $handler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
            self::TEST_ENTITY_NAME,
28
            [],
29
            $this->businessAclProvider,
30
            'OroCRMSalesBundle:Opportunity'
31
        );
32
    }
33
34
    public function testApplyBusinessUnitAcl()
35
    {
36
        $actualElements = [
37
            new SearchElement(1),
38
            new SearchElement(2),
39
            new SearchElement(3)
40
        ];
41
        $expectedIds = [1, 2];
42
43
        $query = $this
44
            ->getMockBuilder('Oro\Bundle\SearchBundle\Query\Query')
45
            ->setMethods(['getElements'])
46
            ->disableOriginalConstructor()
47
            ->getMock();
48
49
        $query->expects($this->once())
50
            ->method('getElements')
51
            ->will($this->returnValue($actualElements));
52
53
        $this->businessAclProvider
54
            ->expects($this->exactly(1))
55
            ->method('getBusinessUnitIds')
56
            ->will($this->returnValue($expectedIds));
57
58
        $indexer = $this->getMockBuilder('Oro\Bundle\SearchBundle\Engine\Indexer')
59
            ->setMethods(['query', 'select', 'simpleSearch'])
60
            ->disableOriginalConstructor()->getMock();
61
62
        $indexer->expects($this->once())
63
            ->method('simpleSearch')
64
            ->will($this->returnValue($query));
65
66
        $expr = $this->getMockBuilder('Doctrine\ORM\Query\Expr')
67
            ->setMethods(['in'])
68
            ->disableOriginalConstructor()
69
            ->getMock();
70
71
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
72
            ->setMethods(['getQuery', 'getResult', 'where', 'expr'])
73
            ->disableOriginalConstructor()
74
            ->getMock();
75
76
        $queryBuilder
77
            ->expects($this->exactly(1))
78
            ->method('expr')
79
            ->will($this->returnValue($expr));
80
81
        $queryBuilder->expects($this->any())
82
            ->method('getQuery')
83
            ->will($this->returnValue($queryBuilder));
84
85
        $queryBuilder->expects($this->any())
86
            ->method('getResult')
87
            ->will($this->returnValue([]));
88
89
        $entityRepository = $this
90
            ->getMockBuilder('Doctrine\ORM\EntityRepository')
91
            ->setMethods(['createQueryBuilder'])
92
            ->disableOriginalConstructor()
93
            ->getMock();
94
95
        $entityRepository->expects($this->any())
96
            ->method('createQueryBuilder')
97
            ->will($this->returnValue($queryBuilder));
98
99
        $metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
100
            ->disableOriginalConstructor()->getMock();
101
        $metadata->expects($this->once())->method('getSingleIdentifierFieldName')
102
            ->will($this->returnValue(self::TEST_ID_FIELD));
103
104
        $metadataFactory = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataFactory')
105
            ->disableOriginalConstructor()->getMock();
106
        $metadataFactory->expects($this->once())
107
            ->method('getMetadataFor')->with(self::TEST_ENTITY_NAME)
108
            ->will($this->returnValue($metadata));
109
110
        $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
111
            ->disableOriginalConstructor()->getMock();
112
        $em->expects($this->once())->method('getRepository')
113
            ->will($this->returnValue($entityRepository));
114
        $em->expects($this->once())->method('getMetadataFactory')
115
            ->will($this->returnValue($metadataFactory));
116
117
        $managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
118
        $managerRegistry->expects($this->once())->method('getManagerForClass')->with(self::TEST_ENTITY_NAME)
119
            ->will($this->returnValue($em));
120
121
        $this->handler->initSearchIndexer($indexer, [self::TEST_ENTITY_NAME => ['alias' => self::TEST_ENTITY_ALIAS]]);
122
        $this->handler->initDoctrinePropertiesByManagerRegistry($managerRegistry);
123
124
        //the main filter check
125
        $expr
126
            ->expects($this->once())
127
            ->method('in')
128
            ->with('e.'.self::TEST_ID_FIELD, $expectedIds);
129
130
        $this->handler->search('query', 0, 10);
131
    }
132
}
133