Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

AdminBundle/Tests/unit/Entity/GroupTest.php (1 issue)

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