Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

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