Completed
Push — master ( 1af7e7...796d56 )
by Jeroen
16s
created

AbstractArticlePageAdminListConfiguratorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 165
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetQueryBuilder() 0 28 1
A testEntityClassName() 0 25 1
A testGetOverViewPages() 0 18 1
A setUp() 0 20 1
A testGetters() 0 7 1
A testBuildFields() 0 7 1
A testGetUrls() 0 28 1
1
<?php
2
3
namespace Kunstmaan\ArticleBundle\Tests\AdminList;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\QueryBuilder;
8
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
9
use Kunstmaan\ArticleBundle\AdminList\AbstractArticlePageAdminListConfigurator;
10
use Kunstmaan\NodeBundle\Entity\Node;
11
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
12
use Kunstmaan\NodeBundle\Helper\NodeMenu;
13
use Kunstmaan\NodeBundle\Helper\NodeMenuItem;
14
use PHPUnit_Framework_TestCase;
15
use ReflectionClass;
16
17
class Configurator extends AbstractArticlePageAdminListConfigurator
18
{
19
    /** @var EntityRepository */
20
    private $repo;
21
22
    public function __construct(EntityManager $em, AclHelper $aclHelper, $locale, $permission, $repo)
23
    {
24
        parent::__construct($em, $aclHelper, $locale, $permission);
25
        $this->repo = $repo;
26
    }
27
28
    /**
29
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be EntityRepository?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
30
     */
31
    public function getOverviewPageRepository()
32
    {
33
        return $this->repo;
34
    }
35
}
36
37
/**
38
 * Class AbstractArticlePageAdminListConfiguratorTest
39
 */
40
class AbstractArticlePageAdminListConfiguratorTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
41
{
42
    /**
43
     * @var AbstractArticlePageAdminListConfigurator
44
     */
45
    protected $object;
46
47
    /**
48
     * @var EntityManager
49
     */
50
    protected $em;
51
52
    /**
53
     * Sets up the fixture, for example, opens a network connection.
54
     * This method is called before a test is executed.
55
     */
56
    protected function setUp()
57
    {
58
        $em = $this->createMock(EntityManager::class);
59
        $em->expects($this->any())
60
            ->method($this->anything())
61
            ->willReturn($em);
62
63
        $acl = $this->createMock(AclHelper::class);
64
65
        $repo = $this->createMock(EntityRepository::class);
66
        $repo->expects($this->any())
67
            ->method($this->anything())
68
            ->willReturn([['fake' => 'array']]);
69
70
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
It seems like $em of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManager> of property $em.

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...
71
72
        /* @var EntityManager $em */
73
        /* @var AclHelper $acl */
74
        $this->object = new Configurator($em, $acl, 'nl', 'admin', $repo);
75
    }
76
77
    public function testGetters()
78
    {
79
        $this->assertEquals('KunstmaanArticleBundle', $this->object->getBundleName());
80
        $this->assertEquals('AbstractArticlePage', $this->object->getEntityName());
81
        $this->assertEquals('KunstmaanArticleBundle:AbstractArticlePageAdminList:list.html.twig', $this->object->getListTemplate());
82
        $this->assertEquals('KunstmaanArticleBundle:AbstractArticlePage', $this->object->getRepositoryName());
83
    }
84
85
    public function testBuildFields()
86
    {
87
        $this->object->buildFields();
88
        $this->object->buildFilters();
89
        $fields = $this->object->getFields();
90
        $this->assertCount(4, $fields);
91
    }
92
93
    public function testGetUrls()
94
    {
95
        $node = new Node();
96
        $node->setId(1314);
97
        $nodeTranslation = new NodeTranslation();
98
        /** @var NodeMenu $menu */
99
        $menu = $this->createMock(NodeMenu::class);
100
        $item = new NodeMenuItem($node, $nodeTranslation, false, $menu);
101
102
        $url = $this->object->getEditUrlFor($item);
103
104
        $this->assertCount(2, $url);
105
        $this->assertArrayHasKey('path', $url);
106
        $this->assertArrayHasKey('params', $url);
107
        $this->assertEquals('KunstmaanNodeBundle_nodes_edit', $url['path']);
108
        $this->assertCount(1, $url['params']);
109
        $this->assertEquals(1314, $url['params']['id']);
110
        $url = $this->object->getEditUrlFor($item);
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
112
        $url = $this->object->getDeleteUrlFor($item);
113
114
        $this->assertCount(2, $url);
115
        $this->assertArrayHasKey('path', $url);
116
        $this->assertArrayHasKey('params', $url);
117
        $this->assertEquals('KunstmaanNodeBundle_nodes_delete', $url['path']);
118
        $this->assertCount(1, $url['params']);
119
        $this->assertEquals(1314, $url['params']['id']);
120
    }
121
122
    /**
123
     * @throws \ReflectionException
124
     */
125
    public function testGetQueryBuilder()
126
    {
127
        $em = $this->createMock(EntityManager::class);
128
        $qb = $this->createMock(QueryBuilder::class);
129
        $em->expects($this->any())
130
            ->method('createQueryBuilder')
131
            ->willReturn($qb);
132
133
        $em->expects($this->any())
134
            ->method('getRepository')
135
            ->willReturn($em);
136
137
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
It seems like $em of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManager> of property $em.

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...
138
139
        $mirror = new ReflectionClass(Configurator::class);
140
        $method = $mirror->getMethod('getQueryBuilder');
141
        $method->setAccessible(true);
142
143
        $mirror = new ReflectionClass(Configurator::class);
144
        $prop = $mirror->getProperty('em');
145
        $prop->setAccessible(true);
146
        $prop->setValue($this->object, $em);
147
148
        /* @var QueryBuilder $qb */
149
        $this->object->adaptQueryBuilder($qb);
150
        $qb = $method->invoke($this->object);
151
        $this->assertInstanceOf(QueryBuilder::class, $qb);
152
    }
153
154
    /**
155
     * @throws \ReflectionException
156
     */
157
    public function testEntityClassName()
158
    {
159
        $em = $this->createMock(EntityManager::class);
160
        $repo = $this->createMock(EntityRepository::class);
161
162
        $em->expects($this->any())
163
            ->method('getRepository')
164
            ->willReturn($repo);
165
166
        $repo->expects($this->any())
167
            ->method('getClassName')
168
            ->willReturn(Configurator::class);
169
170
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
It seems like $em of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManager> of property $em.

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...
171
172
        $mirror = new ReflectionClass(Configurator::class);
173
        $method = $mirror->getMethod('getQueryBuilder');
174
        $method->setAccessible(true);
175
176
        $mirror = new ReflectionClass(Configurator::class);
177
        $prop = $mirror->getProperty('em');
178
        $prop->setAccessible(true);
179
        $prop->setValue($this->object, $em);
180
        $this->assertEquals(Configurator::class, $this->object->getEntityClassName());
181
    }
182
183
    /**
184
     * @throws \ReflectionException
185
     */
186
    public function testGetOverViewPages()
187
    {
188
        $this->assertCount(1, $this->object->getOverviewPages());
189
        $this->assertCount(1, $this->object->getOverviewPage());
190
191
        $mirror = new ReflectionClass(Configurator::class);
192
        $prop = $mirror->getProperty('repo');
193
        $prop->setAccessible(true);
194
195
        $repo = $this->createMock(EntityRepository::class);
196
        $repo->expects($this->any())
197
            ->method($this->anything())
198
            ->willReturn([]);
199
200
        $prop->setValue($this->object, $repo);
201
202
        $this->assertNull($this->object->getOverviewPage());
203
    }
204
}
205