Completed
Push — 1.9 ( 5c3e2e...1529fd )
by
unknown
60:20
created

testGetMarketingListResultIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 9.0856
cc 2
eloc 16
nc 2
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\MarketingListBundle\Tests\Unit\Provider;
4
5
use Doctrine\ORM\Query\Expr\Select;
6
7
use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration;
8
use Oro\Bundle\DataGridBundle\Datagrid\DatagridInterface;
9
use Oro\Bundle\DataGridBundle\Extension\Pager\PagerInterface;
10
use Oro\Bundle\TagBundle\Grid\TagsExtension;
11
use Oro\Bundle\DataGridBundle\Datagrid\Manager;
12
13
use OroCRM\Bundle\MarketingListBundle\Entity\MarketingList;
14
use OroCRM\Bundle\MarketingListBundle\Entity\MarketingListType;
15
use OroCRM\Bundle\MarketingListBundle\Datagrid\ConfigurationProvider;
16
use OroCRM\Bundle\MarketingListBundle\Provider\MarketingListProvider;
17
18
class MarketingListProviderTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var \PHPUnit_Framework_MockObject_MockObject|Manager
22
     */
23
    protected $dataGridManager;
24
25
    /**
26
     * @var MarketingListProvider
27
     */
28
    protected $provider;
29
30
    protected function setUp()
31
    {
32
        $this->dataGridManager = $this->getMockBuilder('Oro\Bundle\DataGridBundle\Datagrid\Manager')
33
            ->disableOriginalConstructor()
34
            ->getMock();
35
36
        $this->provider = new MarketingListProvider($this->dataGridManager);
37
    }
38
39
    protected function tearDown()
40
    {
41
        unset($this->provider, $this->dataGridManager);
42
    }
43
44
    /**
45
     * Gets mock object for query builder
46
     *
47
     * @param array $dqlParts
48
     * @return \PHPUnit_Framework_MockObject_MockObject
49
     */
50
    protected function getQueryBuilder(array $dqlParts = [])
51
    {
52
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
53
            ->disableOriginalConstructor()
54
            ->disableOriginalClone()
55
            ->getMock();
56
57
        $select = new Select();
58
        $select->add('t0.test as c1');
59
60
        $dqlParts[] = ['select', [$select]];
61
62
        $qb->expects($this->any())
63
            ->method('getDQLPart')
64
            ->will($this->returnValueMap($dqlParts));
65
66
        return $qb;
67
    }
68
69
    /**
70
     * @dataProvider queryBuilderDataProvider
71
     * @param string $type
72
     */
73
    public function testGetMarketingListQueryBuilder($type)
74
    {
75
        $marketingList = $this->getMarketingList($type);
76
        $queryBuilder = $this->getQueryBuilder();
77
        $dataGrid = $this->getDataGrid();
78
        $this->assertGetQueryBuilder($marketingList, $queryBuilder, $dataGrid);
79
80
        $this->assertEquals($queryBuilder, $this->provider->getMarketingListQueryBuilder($marketingList));
0 ignored issues
show
Bug introduced by
It seems like $marketingList defined by $this->getMarketingList($type) on line 75 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, OroCRM\Bundle\MarketingL...etingListQueryBuilder() does only seem to accept object<OroCRM\Bundle\Mar...e\Entity\MarketingList>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81
        $expectedColumnInformation = ['testField' => 't0.test'];
82
        $this->assertEquals($expectedColumnInformation, $this->provider->getColumnInformation($marketingList));
0 ignored issues
show
Bug introduced by
It seems like $marketingList defined by $this->getMarketingList($type) on line 75 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, OroCRM\Bundle\MarketingL...:getColumnInformation() does only seem to accept object<OroCRM\Bundle\Mar...e\Entity\MarketingList>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function queryBuilderDataProvider()
89
    {
90
        return [
91
            [MarketingListType::TYPE_MANUAL],
92
            [MarketingListType::TYPE_DYNAMIC],
93
            [MarketingListType::TYPE_STATIC],
94
        ];
95
    }
96
97
    /**
98
     * @dataProvider queryBuilderDataProvider
99
     * @param string $type
100
     */
101
    public function testGetMarketingListResultIterator($type)
102
    {
103
        if ($type === MarketingListType::TYPE_MANUAL) {
104
            $mixin = MarketingListProvider::MANUAL_RESULT_ITEMS_MIXIN;
105
        } else {
106
            $mixin = MarketingListProvider::RESULT_ITEMS_MIXIN;
107
        }
108
109
        $marketingList = $this->getMarketingList($type);
110
        $queryBuilder = $this->getQueryBuilder();
111
        $dataGrid = $this->getDataGrid();
112
        $config = $dataGrid->getConfig();
113
        $config->offsetSetByPath(DatagridConfiguration::DATASOURCE_SKIP_COUNT_WALKER_PATH, true);
114
115
        $this->assertGetQueryBuilder(
116
            $marketingList,
117
            $queryBuilder,
118
            $dataGrid,
119
            $mixin
120
        );
121
122
        $this->assertInstanceOf('\Iterator', $this->provider->getMarketingListResultIterator($marketingList));
0 ignored issues
show
Bug introduced by
It seems like $marketingList defined by $this->getMarketingList($type) on line 109 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, OroCRM\Bundle\MarketingL...ingListResultIterator() does only seem to accept object<OroCRM\Bundle\Mar...e\Entity\MarketingList>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123
    }
124
125
    /**
126
     * @dataProvider queryBuilderDataProvider
127
     * @param string $type
128
     */
129 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...
130
    {
131
        $marketingList = $this->getMarketingList($type);
132
133
        $from = $this->getMockBuilder('Doctrine\ORM\Query\Expr\From')
134
            ->disableOriginalConstructor()
135
            ->getMock();
136
        $from->expects($this->once())
137
            ->method('getAlias')
138
            ->will($this->returnValue('alias'));
139
        $queryBuilder = $this->getQueryBuilder([['from', [$from]]]);
140
        $this->assertEntitiesQueryBuilder($queryBuilder, $marketingList, 'alias');
141
142
        $this->assertInstanceOf(
143
            'Doctrine\ORM\QueryBuilder',
144
            $this->provider->getMarketingListEntitiesQueryBuilder($marketingList)
0 ignored issues
show
Bug introduced by
It seems like $marketingList defined by $this->getMarketingList($type) on line 131 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, OroCRM\Bundle\MarketingL...tEntitiesQueryBuilder() does only seem to accept object<OroCRM\Bundle\Mar...e\Entity\MarketingList>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
145
        );
146
    }
147
148
    /**
149
     * @dataProvider queryBuilderDataProvider
150
     * @param string $type
151
     */
152 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...
153
    {
154
        $marketingList = $this->getMarketingList($type);
155
156
        $from = $this->getMockBuilder('Doctrine\ORM\Query\Expr\From')
157
            ->disableOriginalConstructor()
158
            ->getMock();
159
        $from->expects($this->once())
160
            ->method('getAlias')
161
            ->will($this->returnValue('alias'));
162
        $queryBuilder = $this->getQueryBuilder([['from', [$from]]]);
163
        $this->assertEntitiesQueryBuilder($queryBuilder, $marketingList, 'alias');
164
165
        $this->assertInstanceOf('\Iterator', $this->provider->getMarketingListEntitiesIterator($marketingList));
0 ignored issues
show
Bug introduced by
It seems like $marketingList defined by $this->getMarketingList($type) on line 154 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, OroCRM\Bundle\MarketingL...gListEntitiesIterator() does only seem to accept object<OroCRM\Bundle\Mar...e\Entity\MarketingList>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
166
    }
167
168
    /**
169
     * @param \PHPUnit_Framework_MockObject_MockObject $queryBuilder
170
     * @param MarketingList $marketingList
171
     * @param string $alias
172
     */
173
    protected function assertEntitiesQueryBuilder($queryBuilder, 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
        $dataGrid = $this->getDataGrid();
182
183
        $queryBuilder->expects($this->exactly(2))
184
            ->method('resetDQLPart')
185
            ->will($this->returnSelf());
186
        $queryBuilder->expects($this->once())
187
            ->method('select')
188
            ->with($alias)
189
            ->will($this->returnSelf());
190
        $queryBuilder->expects($this->once())
191
            ->method('orderBy')
192
            ->with($alias . '.id')
193
            ->will($this->returnSelf());
194
195
        $this->assertGetQueryBuilder(
196
            $marketingList,
197
            $queryBuilder,
198
            $dataGrid,
199
            $mixin
200
        );
201
    }
202
203
    /**
204
     * @param MarketingList $marketingList
205
     * @param \PHPUnit_Framework_MockObject_MockObject $queryBuilder
206
     * @param \PHPUnit_Framework_MockObject_MockObject $dataGrid
207
     * @param null|string $mixin
208
     */
209
    protected function assertGetQueryBuilder(MarketingList $marketingList, $queryBuilder, $dataGrid, $mixin = null)
210
    {
211
        $dataSource = $this->getMockBuilder('Oro\Bundle\DataGridBundle\Datasource\Orm\OrmDatasource')
212
            ->disableOriginalConstructor()
213
            ->getMock();
214
        $dataSource->expects($this->once())
215
            ->method('getQueryBuilder')
216
            ->will($this->returnValue($queryBuilder));
217
        $dataGrid->expects($this->once())
218
            ->method('getAcceptedDatasource')
219
            ->will($this->returnValue($dataSource));
220
221
        $parameters = [
222
            PagerInterface::PAGER_ROOT_PARAM => [PagerInterface::DISABLED_PARAM => true],
223
            TagsExtension::TAGS_ROOT_PARAM => [PagerInterface::DISABLED_PARAM => true],
224
        ];
225
        if ($mixin) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mixin of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
226
            $parameters['grid-mixin'] = $mixin;
227
        }
228
        $this->dataGridManager->expects($this->atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Oro\Bundle\DataGridBundle\Datagrid\Manager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
229
            ->method('getDatagrid')
230
            ->with(ConfigurationProvider::GRID_PREFIX . $marketingList->getId(), $parameters)
231
            ->will($this->returnValue($dataGrid));
232
    }
233
234
    /**
235
     * @param string $typeName
236
     * @return \PHPUnit_Framework_MockObject_MockObject|MarketingList
237
     */
238
    protected function getMarketingList($typeName)
239
    {
240
        $type = $this->getMockBuilder('OroCRM\Bundle\MarketingListBundle\Entity\MarketingListType')
241
            ->disableOriginalConstructor()
242
            ->getMock();
243
        $type->expects($this->any())
244
            ->method('getName')
245
            ->will($this->returnValue($typeName));
246
247
        $marketingList = $this->getMockBuilder('OroCRM\Bundle\MarketingListBundle\Entity\MarketingList')
248
            ->disableOriginalConstructor()
249
            ->getMock();
250
        $marketingList->expects($this->any())
251
            ->method('getType')
252
            ->will($this->returnValue($type));
253
        $marketingList->expects($this->any())
254
            ->method('getId')
255
            ->will($this->returnValue(1));
256
        $marketingList->expects($this->any())
257
            ->method('isManual')
258
            ->will($this->returnValue($typeName === MarketingListType::TYPE_MANUAL));
259
260
        return $marketingList;
261
    }
262
263
    /**
264
     * @return \PHPUnit_Framework_MockObject_MockObject|DatagridInterface
265
     */
266
    protected function getDataGrid()
267
    {
268
        $dataGrid = $this->getMockBuilder('Oro\Bundle\DataGridBundle\Datagrid\DatagridInterface')
269
            ->getMockForAbstractClass();
270
271
        $columnAliases = ['testField' => 'c1'];
272
        $config = DatagridConfiguration::createNamed('test', []);
273
        $config->offsetSetByPath(MarketingListProvider::DATAGRID_COLUMN_ALIASES_PATH, $columnAliases);
274
275
        $dataGrid->expects($this->any())
276
            ->method('getConfig')
277
            ->will($this->returnValue($config));
278
279
        return $dataGrid;
280
    }
281
}
282