Completed
Push — 1.9 ( d8eb28...5c3e2e )
by
unknown
61:52 queued 29s
created

MarketingListProviderTest::assertGetQueryBuilder()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 19
nc 2
nop 4
1
<?php
2
3
namespace OroCRM\Bundle\MarketingListBundle\Tests\Unit\Provider;
4
5
use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration;
6
use Oro\Bundle\DataGridBundle\Extension\Pager\PagerInterface;
7
use Oro\Bundle\TagBundle\Grid\TagsExtension;
8
use OroCRM\Bundle\MarketingListBundle\Entity\MarketingList;
9
use OroCRM\Bundle\MarketingListBundle\Entity\MarketingListType;
10
use OroCRM\Bundle\MarketingListBundle\Datagrid\ConfigurationProvider;
11
use OroCRM\Bundle\MarketingListBundle\Provider\MarketingListProvider;
12
13
class MarketingListProviderTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var \PHPUnit_Framework_MockObject_MockObject
17
     */
18
    protected $dataGridManager;
19
20
    /**
21
     * @var MarketingListProvider
22
     */
23
    protected $provider;
24
25
    /** @var \PHPUnit_Framework_MockObject_MockObject */
26
    protected $em;
27
28
    protected function setUp()
29
    {
30
        $this->dataGridManager = $this->getMockBuilder('Oro\Bundle\DataGridBundle\Datagrid\Manager')
31
            ->disableOriginalConstructor()
32
            ->getMock();
33
34
        $this->provider = new MarketingListProvider($this->dataGridManager);
35
36
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
37
            ->disableOriginalConstructor()
38
            ->getMock();
39
    }
40
41
    protected function tearDown()
42
    {
43
        unset($this->provider, $this->dataGridManager, $this->em);
44
    }
45
46
    /**
47
     * Gets mock object for query builder
48
     *
49
     * @return \PHPUnit_Framework_MockObject_MockObject
50
     */
51
    protected function getQueryBuilder()
52
    {
53
        return $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
54
            ->setConstructorArgs([$this->em])
55
            ->getMock();
56
    }
57
58
    /**
59
     * @dataProvider queryBuilderDataProvider
60
     * @param string $type
61
     */
62
    public function testGetMarketingListQueryBuilder($type)
63
    {
64
        $marketingList = $this->getMarketingList($type);
65
        $queryBuilder = $this->getQueryBuilder();
66
        $dataGrid = $this->getMockBuilder('Oro\Bundle\DataGridBundle\Datagrid\DatagridInterface')
67
            ->getMockForAbstractClass();
68
        $this->assertGetQueryBuilder($marketingList, $queryBuilder, $dataGrid);
69
70
        $this->assertEquals($queryBuilder, $this->provider->getMarketingListQueryBuilder($marketingList));
71
    }
72
73
    public function queryBuilderDataProvider()
74
    {
75
        return [
76
            [MarketingListType::TYPE_MANUAL],
77
            [MarketingListType::TYPE_DYNAMIC],
78
            [MarketingListType::TYPE_STATIC],
79
        ];
80
    }
81
82
    /**
83
     * @dataProvider queryBuilderDataProvider
84
     * @param string $type
85
     */
86
    public function testGetMarketingListResultIterator($type)
87
    {
88
        if ($type === MarketingListType::TYPE_MANUAL) {
89
            $mixin = MarketingListProvider::MANUAL_RESULT_ITEMS_MIXIN;
90
        } else {
91
            $mixin = MarketingListProvider::RESULT_ITEMS_MIXIN;
92
        }
93
94
        $marketingList = $this->getMarketingList($type);
95
        $queryBuilder = $this->getQueryBuilder();
96
        $dataGrid = $this->getMockBuilder('Oro\Bundle\DataGridBundle\Datagrid\DatagridInterface')
97
            ->getMockForAbstractClass();
98
        $config = $this->getMockBuilder('Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration')
99
            ->disableOriginalConstructor()
100
            ->getMock();
101
        $config->expects($this->once())
102
            ->method('offsetGetByPath')
103
            ->with(DatagridConfiguration::DATASOURCE_SKIP_COUNT_WALKER_PATH)
104
            ->will($this->returnValue(true));
105
        $dataGrid->expects($this->once())
106
            ->method('getConfig')
107
            ->will($this->returnValue($config));
108
109
        $this->assertGetQueryBuilder(
110
            $marketingList,
111
            $queryBuilder,
112
            $dataGrid,
113
            $mixin
114
        );
115
116
        $this->assertInstanceOf('\Iterator', $this->provider->getMarketingListResultIterator($marketingList));
117
    }
118
119
    /**
120
     * @dataProvider queryBuilderDataProvider
121
     * @param string $type
122
     */
123 View Code Duplication
    public function testGetMarketingListEntitiesQueryBuilder($type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $marketingList = $this->getMarketingList($type);
126
127
        $from = $this->getMockBuilder('Doctrine\ORM\Query\Expr\From')
128
            ->disableOriginalConstructor()
129
            ->getMock();
130
        $from->expects($this->once())
131
            ->method('getAlias')
132
            ->will($this->returnValue('alias'));
133
        $queryBuilder = $this->assertEntitiesQueryBuilder($marketingList, 'alias');
134
        $queryBuilder->expects($this->once())
135
            ->method('getDQLPart')
136
            ->with('from')
137
            ->will($this->returnValue([$from]));
138
139
        $this->assertInstanceOf(
140
            'Doctrine\ORM\QueryBuilder',
141
            $this->provider->getMarketingListEntitiesQueryBuilder($marketingList)
142
        );
143
    }
144
145
    /**
146
     * @dataProvider queryBuilderDataProvider
147
     * @param string $type
148
     */
149 View Code Duplication
    public function testGetMarketingListEntitiesIterator($type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $marketingList = $this->getMarketingList($type);
152
153
        $from = $this->getMockBuilder('Doctrine\ORM\Query\Expr\From')
154
            ->disableOriginalConstructor()
155
            ->getMock();
156
        $from->expects($this->once())
157
            ->method('getAlias')
158
            ->will($this->returnValue('alias'));
159
        $queryBuilder = $this->assertEntitiesQueryBuilder($marketingList, 'alias');
160
        $queryBuilder->expects($this->once())
161
            ->method('getDQLPart')
162
            ->with('from')
163
            ->will($this->returnValue([$from]));
164
165
        $this->assertInstanceOf('\Iterator', $this->provider->getMarketingListEntitiesIterator($marketingList));
166
    }
167
168
    /**
169
     * @param MarketingList $marketingList
170
     * @param string $alias
171
     * @return \PHPUnit_Framework_MockObject_MockObject
172
     */
173
    protected function assertEntitiesQueryBuilder(MarketingList $marketingList, $alias)
174
    {
175
        if ($marketingList->isManual()) {
176
            $mixin = MarketingListProvider::MANUAL_RESULT_ENTITIES_MIXIN;
177
        } else {
178
            $mixin = MarketingListProvider::RESULT_ENTITIES_MIXIN;
179
        }
180
181
        $queryBuilder = $this->getQueryBuilder();
182
        $dataGrid = $this->getMockBuilder('Oro\Bundle\DataGridBundle\Datagrid\DatagridInterface')
183
            ->getMockForAbstractClass();
184
185
        $queryBuilder->expects($this->exactly(2))
186
            ->method('resetDQLPart')
187
            ->will($this->returnSelf());
188
        $queryBuilder->expects($this->once())
189
            ->method('select')
190
            ->with($alias)
191
            ->will($this->returnSelf());
192
        $queryBuilder->expects($this->once())
193
            ->method('orderBy')
194
            ->with($alias . '.id')
195
            ->will($this->returnSelf());
196
197
        $this->assertGetQueryBuilder(
198
            $marketingList,
199
            $queryBuilder,
200
            $dataGrid,
201
            $mixin
202
        );
203
204
        return $queryBuilder;
205
    }
206
207
    protected function assertGetQueryBuilder(MarketingList $marketingList, $queryBuilder, $dataGrid, $mixin = null)
208
    {
209
        $dataSource = $this->getMockBuilder('Oro\Bundle\DataGridBundle\Datasource\Orm\OrmDatasource')
210
            ->disableOriginalConstructor()
211
            ->getMock();
212
        $dataSource->expects($this->once())
213
            ->method('getQueryBuilder')
214
            ->will($this->returnValue($queryBuilder));
215
        $dataGrid->expects($this->once())
216
            ->method('getAcceptedDatasource')
217
            ->will($this->returnValue($dataSource));
218
219
        $parameters = [
220
            PagerInterface::PAGER_ROOT_PARAM => [PagerInterface::DISABLED_PARAM => true],
221
            TagsExtension::TAGS_ROOT_PARAM => [PagerInterface::DISABLED_PARAM => true],
222
        ];
223
        if ($mixin) {
224
            $parameters['grid-mixin'] = $mixin;
225
        }
226
        $this->dataGridManager->expects($this->atLeastOnce())
227
            ->method('getDatagrid')
228
            ->with(ConfigurationProvider::GRID_PREFIX . $marketingList->getId(), $parameters)
229
            ->will($this->returnValue($dataGrid));
230
    }
231
232
    protected function getMarketingList($typeName)
233
    {
234
        $type = $this->getMockBuilder('OroCRM\Bundle\MarketingListBundle\Entity\MarketingListType')
235
            ->disableOriginalConstructor()
236
            ->getMock();
237
        $type->expects($this->any())
238
            ->method('getName')
239
            ->will($this->returnValue($typeName));
240
241
        $marketingList = $this->getMockBuilder('OroCRM\Bundle\MarketingListBundle\Entity\MarketingList')
242
            ->disableOriginalConstructor()
243
            ->getMock();
244
        $marketingList->expects($this->any())
245
            ->method('getType')
246
            ->will($this->returnValue($type));
247
        $marketingList->expects($this->any())
248
            ->method('getId')
249
            ->will($this->returnValue(1));
250
        $marketingList->expects($this->any())
251
            ->method('isManual')
252
            ->will($this->returnValue($typeName === MarketingListType::TYPE_MANUAL));
253
254
        return $marketingList;
255
    }
256
}
257