Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Kunstmaan/AdminBundle/Tests/Entity/GroupTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Tests\Entity;
4
5
use Kunstmaan\AdminBundle\Entity\Group;
6
use Kunstmaan\AdminBundle\Entity\Role;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\Validator\Validation;
9
10
class GroupTest extends TestCase
11
{
12
    /**
13
     * @var Group
14
     */
15
    protected $object;
16
17
    /**
18
     * Sets up the fixture, for example, opens a network connection.
19
     * This method is called before a test is executed.
20
     */
21
    protected function setUp()
22
    {
23
        $this->object = new Group('group');
24
    }
25
26
    public function testGetId()
27
    {
28
        $this->assertEquals(null, $this->object->getId());
29
    }
30
31
    public function test__toString()
32
    {
33
        $this->assertEquals('group', $this->object->__toString());
34
    }
35
36
    public function testGetRoles()
37
    {
38
        /* @var $role Role */
39
        $role = $this->getRole();
40
        $this->object->addRole($role);
41
42
        $this->assertEquals(['role1'], $this->object->getRoles());
43
    }
44
45
    public function testGetRolesCollection()
46
    {
47
        /* @var $role Role */
48
        $role = $this->getRole();
49
        $this->object->addRole($role);
50
51
        $collection = new \Doctrine\Common\Collections\ArrayCollection();
52
        $collection->add($role);
53
54
        $this->assertEquals($collection, $this->object->getRolesCollection());
55
    }
56
57
    public function testGetRole()
58
    {
59
        /* @var $role Role */
60
        $role = $this->getRole();
61
        $this->object->addRole($role);
62
63
        $result = $this->object->getRole('role1');
64
        $this->assertEquals($role, $result);
65
66
        $result = $this->object->getRole('role2');
67
        $this->assertEquals(null, $result);
68
    }
69
70
    public function testHasRole()
71
    {
72
        /* @var $role Role */
73
        $role = $this->getRole();
74
        $this->object->addRole($role);
75
76
        $this->assertTrue($this->object->hasRole('role1'));
77
        $this->assertFalse($this->object->hasRole('role2'));
78
    }
79
80 View Code Duplication
    public function testRemoveRole()
81
    {
82
        /* @var $role Role */
83
        $role = $this->getRole();
84
        $this->object->addRole($role);
85
        $this->assertTrue($this->object->hasRole('role1'));
86
87
        $this->object->removeRole('role1');
88
        $this->assertFalse($this->object->hasRole('role1'));
89
    }
90
91
    /**
92
     * @expectedException \InvalidArgumentException
93
     */
94
    public function testAddRoleWithInvalidParameter()
95
    {
96
        /* @var $role Role */
97
        $role = new \StdClass();
98
        $this->object->addRole($role);
99
    }
100
101
    public function testSetRoles()
102
    {
103
        $role1 = $this->getRole('role1');
104
        $role2 = $this->getRole('role2');
105
        $role3 = $this->getRole('role3');
106
        $roles = [$role1, $role2, $role3];
107
        $this->object->setRoles($roles);
108
109
        $this->assertEquals(3, \count($this->object->getRoles()));
110
    }
111
112
    public function testSetRolesCollection()
113
    {
114
        $role1 = $this->getRole('role1');
115
        $role2 = $this->getRole('role2');
116
        $role3 = $this->getRole('role3');
117
118
        $roles = new \Doctrine\Common\Collections\ArrayCollection();
119
        $roles->add($role1);
120
        $roles->add($role2);
121
        $roles->add($role3);
122
123
        $this->object->setRolesCollection($roles);
124
        $this->assertEquals(3, $this->object->getRolesCollection()->count());
125
    }
126
127
    public function testConstructorAndGetSetName()
128
    {
129
        $object = new Group('testgroup');
130
        $this->assertEquals('testgroup', $object->getName());
131
132
        $object->setName('group2');
133
        $this->assertEquals('group2', $object->getName());
134
    }
135
136 View Code Duplication
    public function testValidateGroupWithoutRole()
0 ignored issues
show
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...
137
    {
138
        $group = new Group('test');
139
140
        $validator = Validation::createValidatorBuilder()
141
            ->enableAnnotationMapping()
142
            ->getValidator();
143
144
        $violations = $validator->validate($group);
145
146
        $this->assertCount(1, $violations);
147
    }
148
149 View Code Duplication
    public function testValidateGroupWithRole()
0 ignored issues
show
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
        $group = new Group('test');
152
        $group->addRole(new Role('role'));
153
154
        $validator = Validation::createValidatorBuilder()
155
            ->enableAnnotationMapping()
156
            ->getValidator();
157
158
        $violations = $validator->validate($group);
159
160
        $this->assertCount(0, $violations);
161
    }
162
163
    /**
164
     * @param string $name
165
     *
166
     * @return \Kunstmaan\AdminBundle\Entity\Role
167
     */
168
    protected function getRole($name = 'role1')
169
    {
170
        $role = $this->getMockBuilder('Kunstmaan\AdminBundle\Entity\Role')
171
            ->disableOriginalConstructor()
172
            ->getMock();
173
        $role->expects($this->any())
174
            ->method('getRole')
175
            ->will($this->returnValue($name));
176
177
        return $role;
178
    }
179
}
180