Completed
Push — master ( 1db3cd...632e40 )
by Jeroen
24:52 queued 11:31
created

Configurator/AbstractDoctrineORMAdminListConfiguratorTest.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 6
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 0
dl 6
loc 6
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\Tests\AdminList\Configurator;
4
5
use Doctrine\ORM\AbstractQuery;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\ORM\QueryBuilder;
10
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
11
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
12
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineORMAdminListConfigurator;
13
use Kunstmaan\AdminListBundle\AdminList\Filter;
14
use Kunstmaan\AdminListBundle\AdminList\FilterBuilder;
15
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\AbstractORMFilterType;
16
use Pagerfanta\Pagerfanta;
17
use PHPUnit\Framework\TestCase;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Session\Session;
20
21
class AbstractDoctrineORMAdminListConfiguratorTest extends TestCase
22
{
23
    /** @var EntityManagerInterface */
24
    private $emMock;
25
26
    /** @var AclHelper */
27
    private $aclHelperMock;
28
29
    public function setUp()
30
    {
31
        $queryMock = $this->createMock(AbstractQuery::class);
32
        $queryMock
33
            ->expects($this->any())
34
            ->method('iterate')
35
            ->willReturn($this->createMock(\Iterator::class))
36
        ;
37
38
        $this->emMock = $this->createMock(EntityManagerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...anagerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManagerInterface> of property $emMock.

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...
39
        $this->aclHelperMock = $this->createMock(AclHelper::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Kunst...y\Acl\AclHelper::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Kunstmaan\AdminBu...Security\Acl\AclHelper> of property $aclHelperMock.

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...
40
        $this->aclHelperMock
41
            ->expects($this->any())
42
            ->method('apply')
43
            ->willReturn($queryMock)
44
        ;
45
46
        $queryBuilderMock = $this->createMock(QueryBuilder::class);
47
        $queryBuilderMock
48
            ->expects($this->any())
49
            ->method('getQuery')
50
            ->willReturn($queryMock)
51
        ;
52
53
        $entityRepositoryMock = $this->createMock(EntityRepository::class);
54
        $entityRepositoryMock
55
            ->expects($this->any())
56
            ->method('createQueryBuilder')
57
            ->willReturn($queryBuilderMock)
58
        ;
59
60
        $this->emMock
61
            ->expects($this->any())
62
            ->method('getRepository')
63
            ->with('Bundle:MyEntity')
64
            ->willReturn($entityRepositoryMock)
65
        ;
66
        $this->emMock
67
            ->expects($this->any())
68
            ->method('getClassMetadata')
69
            ->willReturn($this->createMock(ClassMetadata::class))
70
        ;
71
    }
72
73 View Code Duplication
    public function testGetEditUrl()
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...
74
    {
75
        $abstractMock = $this->setUpAbstractMock();
76
77
        $item = new class() {
78
            public function getId()
79
            {
80
                return 747;
81
            }
82
        };
83
84
        $editUrl = $abstractMock->getEditUrlFor($item);
85
        $this->assertIsArray($editUrl);
86
        $this->assertArrayHasKey('path', $editUrl);
87
        $this->assertArrayHasKey('params', $editUrl);
88
        $this->assertEquals('bundle_admin_myentity_'.AbstractDoctrineORMAdminListConfigurator::SUFFIX_EDIT, $editUrl['path']);
89
        $this->assertContains(747, $editUrl['params']);
90
    }
91
92 View Code Duplication
    public function testGetDeleteUrl()
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...
93
    {
94
        $abstractMock = $this->setUpAbstractMock();
95
96
        $item = new class() {
97
            public function getId()
98
            {
99
                return 747;
100
            }
101
        };
102
103
        $editUrl = $abstractMock->getDeleteUrlFor($item);
104
        $this->assertIsArray($editUrl);
105
        $this->assertArrayHasKey('path', $editUrl);
106
        $this->assertArrayHasKey('params', $editUrl);
107
        $this->assertEquals('bundle_admin_myentity_'.AbstractDoctrineORMAdminListConfigurator::SUFFIX_DELETE, $editUrl['path']);
108
        $this->assertContains(747, $editUrl['params']);
109
    }
110
111
    public function testGetPagerFanta()
112
    {
113
        $abstractMock = $this->setUpAbstractMock();
114
115
        $this->assertInstanceOf(Pagerfanta::class, $abstractMock->getPagerfanta());
116
    }
117
118
    public function testGetQueryDefault()
119
    {
120
        $abstractMock = $this->setUpAbstractMock();
121
122
        // default
123
        $this->assertInstanceOf(AbstractQuery::class, $abstractMock->getQuery());
124
125
        // no longer null, direct return
126
        $this->assertInstanceOf(AbstractQuery::class, $abstractMock->getQuery());
127
128
        // check the iterator in one go
129
        $this->assertInstanceOf(\Iterator::class, $abstractMock->getIterator());
130
    }
131
132
    public function testGetQueryWithFilter()
133
    {
134
        $abstractORMFilterTypeMock = $this->createMock(AbstractORMFilterType::class);
135
136
        $filterBuilderMock = $this->createMock(FilterBuilder::class);
137
        $filterBuilderMock
138
            ->expects($this->any())
139
            ->method('getCurrentFilters')
140
            ->willReturn([
141
                new Filter('foo', ['type' => $abstractORMFilterTypeMock, 'options' => []], 'uid'),
142
            ])
143
        ;
144
145
        /** @var AbstractDoctrineORMAdminListConfigurator $abstractMock */
146
        $abstractMock = $this->setUpAbstractMock(['getFilterBuilder']);
147
        $abstractMock
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Kunstmaan\AdminLi...MAdminListConfigurator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
            ->expects($this->any())
149
            ->method('getFilterBuilder')
150
            ->willReturn($filterBuilderMock)
151
        ;
152
153
        $abstractMock->addFilter('foo');
154
        $this->assertInstanceOf(AbstractQuery::class, $abstractMock->getQuery());
155
    }
156
157
    public function testGetQueryWithOrderBy()
158
    {
159
        /** @var AbstractDoctrineORMAdminListConfigurator $abstractMock */
160
        $abstractMock = $this->setUpAbstractMock(['getFilterBuilder']);
161
162
        $filterBuilderMock = $this->createMock(FilterBuilder::class);
163
        $filterBuilderMock
164
            ->expects($this->once())
165
            ->method('bindRequest')
166
        ;
167
        $filterBuilderMock
168
            ->expects($this->any())
169
            ->method('getCurrentFilters')
170
            ->willReturn([])
171
        ;
172
173
        $abstractMock
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Kunstmaan\AdminLi...MAdminListConfigurator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
174
            ->expects($this->exactly(2))
175
            ->method('getFilterBuilder')
176
            ->willReturn($filterBuilderMock)
177
        ;
178
179
        $requestMock = $this->getMockBuilder(Request::class)
180
            ->setConstructorArgs([['page' => 1], [], ['_route' => 'testroute']])
181
            ->setMethods(['getSession'])
182
            ->getMock()
183
        ;
184
185
        $sessionMock = $this->createMock(Session::class);
186
        $sessionMock
187
            ->expects($this->once())
188
            ->method('has')
189
            ->willReturn(true)
190
        ;
191
        $sessionMock
192
            ->expects($this->once())
193
            ->method('get')
194
            ->with('listconfig_testroute')
195
            ->willReturn(['page' => 1, 'orderBy' => 'foo', 'orderDirection' => 'up'])
196
        ;
197
198
        $requestMock
199
            ->expects($this->exactly(2))
200
            ->method('getSession')
201
            ->willReturn($sessionMock)
202
        ;
203
204
        $abstractMock->bindRequest($requestMock);
0 ignored issues
show
Documentation introduced by
$requestMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component\HttpFoundation\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
205
        $this->assertInstanceOf(AbstractQuery::class, $abstractMock->getQuery());
206
    }
207
208 View Code Duplication
    public function testGetQueryWithPermissionDefAndAcl()
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...
209
    {
210
        $abstractMock = $this->setUpAbstractMock();
211
212
        /** @var PermissionDefinition $permissionDefinitionMock */
213
        $permissionDefinitionMock = $this->createMock(PermissionDefinition::class);
214
        $this->assertInstanceOf(AbstractDoctrineORMAdminListConfigurator::class, $abstractMock->setPermissionDefinition($permissionDefinitionMock));
215
        $this->assertInstanceOf(AbstractQuery::class, $abstractMock->getQuery());
216
    }
217
218 View Code Duplication
    public function testSetGetPermissionDefinition()
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...
219
    {
220
        $abstractMock = $this->setUpAbstractMock();
221
222
        /** @var PermissionDefinition $permissionDefinitionMock */
223
        $permissionDefinitionMock = $this->createMock(PermissionDefinition::class);
224
        $this->assertInstanceOf(AbstractDoctrineORMAdminListConfigurator::class, $abstractMock->setPermissionDefinition($permissionDefinitionMock));
225
        $this->assertInstanceOf(PermissionDefinition::class, $abstractMock->getPermissionDefinition());
226
    }
227
228 View Code Duplication
    public function testSetGetEntityManager()
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...
229
    {
230
        $abstractMock = $this->setUpAbstractMock();
231
232
        /** @var EntityManagerInterface $emMock */
233
        $emMock = $this->createMock(EntityManagerInterface::class);
234
        $this->assertInstanceOf(AbstractDoctrineORMAdminListConfigurator::class, $abstractMock->setEntityManager($emMock));
235
        $this->assertInstanceOf(EntityManagerInterface::class, $abstractMock->getEntityManager());
236
    }
237
238 View Code Duplication
    public function setUpAbstractMock(array $methods = [])
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...
239
    {
240
        /** @var AbstractDoctrineORMAdminListConfigurator $abstractMock */
241
        $abstractMock = $this->getMockForAbstractClass(AbstractDoctrineORMAdminListConfigurator::class, [$this->emMock], '', true, true, true, $methods);
242
        $abstractMock
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Kunstmaan\AdminLi...MAdminListConfigurator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
243
            ->expects($this->any())
244
            ->method('getBundleName')
245
            ->willReturn('Bundle')
246
        ;
247
        $abstractMock
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Kunstmaan\AdminLi...MAdminListConfigurator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
248
            ->expects($this->any())
249
            ->method('getEntityName')
250
            ->willReturn('MyEntity')
251
        ;
252
253
        return $abstractMock;
254
    }
255
}
256