Completed
Push — develop ( e2b887...c09155 )
by
unknown
12:13
created

AcceptInvitationHandlerTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 177
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 77 5
A testSetterAndGetter() 0 11 1
A testGetterThrowException() 0 12 3
A testReturnsErrorIndicatingOrganizationNotFound() 0 9 1
A testReturnsErrorIndicatinTokenIsInvalidOrExpired() 0 9 1
A testUserDraftsWillBeActivated() 0 9 1
A testAssigendUsersWillBeUnassigned() 0 16 1
A testUnAssigendUsersWillRemainUnassigned() 0 15 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2015 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace OrganizationsTest\Controller\Plugin;
12
13
use Auth\Entity\User;
14
use Core\Exception\MissingDependencyException;
15
use Organizations\Controller\Plugin\AcceptInvitationHandler;
16
use Organizations\Entity\Employee;
17
use Organizations\Entity\Organization;
18
19
/**
20
 * Tests for \Organizations\Controller\Plugin\AcceptInvitationHandler
21
 * 
22
 * @covers \Organizations\Controller\Plugin\AcceptInvitationHandler
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @group Organizations
25
 * @group Organizations.Controller
26
 * @group Organizations.Controller.Plugin
27
 */
28
class AcceptInvitationHandlerTest extends \PHPUnit_Framework_TestCase
29
{
30
31
    private $target;
32
    private $authMock;
33
    private $organizationRepositoryMock;
34
    private $userRepositoryMock;
35
    private $userMock;
36
    private $organizationMock;
37
38
    public function setUp()
39
    {
40
        $name = $this->getName(false);
41
42
        $this->target = new AcceptInvitationHandler();
43
44
        if (false !== strpos($name, 'Exception')) {
45
            return;
46
        }
47
48
        $this->authMock = $this->getMockBuilder('\Auth\AuthenticationService')->disableOriginalConstructor()->getMock();
49
        $this->organizationRepositoryMock = $this->getMockBuilder('Organizations\Repository\Organization')
50
                                                 ->disableOriginalConstructor()->getMock();
51
        $this->userRepositoryMock = $this->getMockBuilder('\Auth\Repository\User')->disableOriginalConstructor()->getMock();
52
53
        if (false !== strpos($this->getName(false), 'Setter')) {
54
            return;
55
        }
56
57
        $this->target->setAuthenticationService($this->authMock);
58
59
        $this->target->setOrganizationRepository($this->organizationRepositoryMock);
60
        if (false !== strpos($this->getName(false), 'OrganizationNotFound')) {
61
            return;
62
        }
63
        $this->organizationMock = $this->getMock('\Organizations\Entity\Organization');
64
65
        $this->organizationRepositoryMock->expects($this->once())->method('find')->with('testOrgId')->willReturn($this->organizationMock);
66
67
        $this->target->setUserRepository($this->userRepositoryMock);
68
69
        if (false !== strpos($this->getName(false), 'TokenIsInvalid')) {
70
            return;
71
        }
72
73
        $this->userMock = new User();
74
        $this->userMock->setId('testUserId');
75
        $employee = $this->getMock('\Organizations\Entity\Employee');
76
        $employee->expects($this->once())->method('setStatus')->with(Employee::STATUS_ASSIGNED);
77
78
        $this->organizationMock->expects($this->once())->method('getEmployee')->with($this->userMock->getId())
79
                               ->willReturn($employee);
80
81
        $this->organizationMock->expects($this->any())->method('getId')->willReturn('testOrgId');
82
        $this->userRepositoryMock->expects($this->once())->method('findByToken')->with('testToken')->willReturn($this->userMock);
83
84
        $sameOrganization = new Organization();
85
        $sameOrganization->setId('testOrgId');
86
87
        $assignedEmp = $this->getMock('\Organizations\Entity\Employee');
88
        $assignedEmp->expects($this->once())->method('isUnassigned')->with(true)->willReturn(false);
89
        $assignedEmp->expects($this->once())->method('setStatus')->with(Employee::STATUS_REJECTED);
90
91
        $assignedEmpOrganization = $this->getMock('\Organizations\Entity\Organization');
92
        $assignedEmpOrganization->expects($this->once())->method('getId')->willReturn('otherId');
93
        $assignedEmpOrganization->expects($this->once())->method('getEmployee')->with($this->userMock->getId())
94
                                ->willReturn($assignedEmp);
95
96
        $unassignedEmp = $this->getMock('\Organizations\Entity\Employee');
97
        $unassignedEmp->expects($this->once())->method('isUnassigned')->with(true)->willReturn(true);
98
        $unassignedEmp->expects($this->never())->method('setStatus');
99
100
        $unassignedEmpOrganization = $this->getMock('\Organizations\Entity\Organization');
101
        $unassignedEmpOrganization->expects($this->once())->method('getId')->willReturn('otherId');
102
        $unassignedEmpOrganization->expects($this->once())->method('getEmployee')->with($this->userMock->getId())
103
                                ->willReturn($unassignedEmp);
104
105
        $this->organizationRepositoryMock->expects($this->once())
106
                                         ->method('findPendingOrganizationsByEmployee')
107
                                         ->with($this->userMock->getId())
108
                                         ->willReturn(array($sameOrganization, $assignedEmpOrganization, $unassignedEmpOrganization));
109
110
        $storageMock = $this->getMockForAbstractClass('\Zend\Authentication\Storage\StorageInterface');
111
        $storageMock->expects($this->once())->method('write')->with($this->userMock->getId());
112
        $this->authMock->expects($this->once())->method('getStorage')->willReturn($storageMock);
113
114
    }
115
116
    public function testSetterAndGetter()
117
    {
118
        $this->assertSame($this->target, $this->target->setAuthenticationService($this->authMock), 'Fluent interface broken: setAuthenticationService()');
119
        $this->assertSame($this->target, $this->target->setOrganizationRepository($this->organizationRepositoryMock), 'Fluent interface broken: setOrganizationRepository()');
120
        $this->assertSame($this->target, $this->target->setUserRepository($this->userRepositoryMock), 'Fluent interface broken: setUserRepository()');
121
122
        $this->assertSame($this->organizationRepositoryMock, $this->target->getOrganizationRepository());
123
        $this->assertSame($this->userRepositoryMock, $this->target->getUserRepository());
124
        $this->assertSame($this->authMock, $this->target->getAuthenticationService());
125
126
    }
127
128
    public function testGetterThrowException()
129
    {
130
        foreach (array('getAuthenticationService', 'getOrganizationRepository', 'getUserRepository') as $method) {
131
            try {
132
                $this->target->$method();
133
            } catch (MissingDependencyException $e) {
134
                continue;
135
            }
136
137
            $this->fail('Expected exception was not thrown for "' . $method . '"');
138
        }
139
    }
140
141
142
    public function testReturnsErrorIndicatingOrganizationNotFound()
143
    {
144
        $orgid = 'testOrgId';
145
        $this->organizationRepositoryMock->expects($this->once())->method('find')->with($orgid)->willReturn(null);
146
147
        $result = $this->target->process('testToken', $orgid);
148
149
        $this->assertEquals(AcceptInvitationHandler::ERROR_ORGANIZATION_NOT_FOUND, $result);
150
    }
151
152
    public function testReturnsErrorIndicatinTokenIsInvalidOrExpired()
153
    {
154
        $token = 'testToken';
155
        $this->userRepositoryMock->expects($this->once())->method('findByToken')->with($token)->willReturn(null);
156
157
        $result = $this->target->process($token, 'testOrgId');
158
159
        $this->assertEquals(AcceptInvitationHandler::ERROR_TOKEN_INVALID, $result);
160
    }
161
162
    public function testUserDraftsWillBeActivated()
163
    {
164
        $this->userMock->setIsDraft(true);
165
166
        $result = $this->target->process('testToken', 'testOrgId');
167
168
        $this->assertEquals(AcceptInvitationHandler::OK_SET_PW, $result);
169
        $this->assertFalse($this->userMock->isDraft());
170
    }
171
172
    public function testAssigendUsersWillBeUnassigned()
173
    {
174
        $empMock = $this->getMock('\Organizations\Entity\Employee');
175
        $empMock->expects($this->once())->method('setStatus')->with(Employee::STATUS_UNASSIGNED);
176
177
        $orgRef = $this->getMockBuilder('\Organizations\Entity\OrganizationReference')->disableOriginalConstructor()->getMock();
178
        $orgRef->expects($this->once())->method('hasAssociation')->willReturn(true);
179
        $orgRef->expects($this->once())->method('getEmployee')->with($this->userMock->getId())
180
               ->willReturn($empMock);
181
182
        $this->userMock->setOrganization($orgRef);
183
184
        $result = $this->target->process('testToken', 'testOrgId');
185
186
        $this->assertEquals(AcceptInvitationHandler::OK, $result);
187
    }
188
189
    public function testUnAssigendUsersWillRemainUnassigned()
190
    {
191
        $empMock = $this->getMock('\Organizations\Entity\Employee');
192
        $empMock->expects($this->never())->method('setStatus');
193
194
        $orgRef = $this->getMockBuilder('\Organizations\Entity\OrganizationReference')->disableOriginalConstructor()->getMock();
195
        $orgRef->expects($this->once())->method('hasAssociation')->willReturn(false);
196
        $orgRef->expects($this->never())->method('getEmployee');
197
198
        $this->userMock->setOrganization($orgRef);
199
200
        $result = $this->target->process('testToken', 'testOrgId');
201
202
        $this->assertEquals(AcceptInvitationHandler::OK, $result);
203
    }
204
}