Completed
Push — develop ( eb5f8c...2537a4 )
by
unknown
13:09
created

EmployeeInvitationFactoryTest::testCreateService()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 67

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 96
rs 8.386
cc 1
eloc 67
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Mail;
12
13
use Auth\Entity\User;
14
use Core\Mail\HTMLTemplateMessage;
15
use Organizations\Entity\Organization;
16
use Organizations\Entity\OrganizationName;
17
use Organizations\Entity\OrganizationReference;
18
use Organizations\Mail\EmployeeInvitationFactory;
19
20
/**
21
 * Tests for \Organizations\Mail\EmployeeInvitationFactory
22
 * 
23
 * @covers \Organizations\Mail\EmployeeInvitationFactory
24
 * @author Mathias Gelhausen <[email protected]>
25
 * @group Organizations
26
 * @group Organizations.Mail
27
 */
28
class EmployeeInvitationFactoryTest extends \PHPUnit_Framework_TestCase
29
{
30
31
    /**
32
     * @testdox Implements \Zend\ServiceManager\FactoryInterface and \Zend\ServiceManager\MutableCreationOptionsInterface
33
     */
34
    public function testImplementsInterfaces()
35
    {
36
        $target = new EmployeeInvitationFactory();
37
38
        $this->assertInstanceOf('\Zend\ServiceManager\FactoryInterface', $target);
39
        $this->assertInstanceOf('\Zend\ServiceManager\MutableCreationOptionsInterface', $target);
40
    }
41
42
    /**
43
     * @testdox Allows setting of creation options for each invokation.
44
     * @expectedException \InvalidArgumentException
45
     * @expectedExceptionMessage An user interface is required
46
     */
47
    public function testSetCreationOptionsThrowsExceptionIfUserIsMissing()
48
    {
49
        $options = Array();
50
        $target = new EmployeeInvitationFactory();
51
52
        $target->setCreationOptions($options);
53
    }
54
55
    public function provideCreationOptionsTestData()
56
    {
57
        $user = $this->getMockForAbstractClass('\Auth\Entity\UserInterface');
58
59
        $makeArray = function($options) use ($user) {
60
            $options['user'] = $user;
61
            return array($options, array_merge(array('user' => $user, 'token' => false, 'template' => 'organizations/mail/invite-employee'), $options));
62
        };
63
64
        return array(
65
            $makeArray(array()),
66
            $makeArray(array('token' => 'testToken')),
67
            $makeArray(array('template' => 'testTemplate')),
68
            $makeArray(array('token' => 'tokenTest', 'template' => 'templateTest')),
69
            array(array('user' => $user, 'token' => true), array('user' => $user, 'token' => false, 'template' => 'organizations/mail/invite-employee')),
70
            array(array('user' => $user, 'template' => new \stdClass()), array('user' => $user, 'token' => false, 'template' => 'organizations/mail/invite-employee')),
71
        );
72
    }
73
74
    /**
75
     * @testdox Allows setting of creation options for each invokation.
76
     * @dataProvider provideCreationOptionsTestData
77
     */
78
    public function testSetCreationOptions($options, $expected)
79
    {
80
        $target = new EmployeeInvitationFactory();
81
82
        $target->setCreationOptions($options);
83
84
        $this->assertAttributeEquals($expected, 'options', $target);
85
    }
86
87
    /**
88
     * @testdox Creates a proper configured HTMLTemplate Mail.
89
     */
90
    public function testCreateService()
91
    {
92
93
        $user = new User();
94
        $user->setId('testUser');
95
        $user->setEmail('test@user');
96
97
98
        $options = array(
99
            'token' => 'testToken',
100
            'user' => $user,
101
            'template' => 'testTemplate',
102
        );
103
104
        $ownerOrg = new Organization();
105
        $ownerOrg->setId('ownerOrg');
106
        $ownerOrgName = new OrganizationName('TestOwnerOrg');
107
        $ownerOrg->setOrganizationName($ownerOrgName);
108
109
        $userOrg = new Organization();
110
        $userOrg->setId('userOrg');
111
        $userOrgName = new OrganizationName('TestUserOrg');
112
        $userOrg->setOrganizationName($userOrgName);
113
114
115
        $orgRep = $this->getMockBuilder('\Organizations\Repository\Organization')->disableOriginalConstructor()->getMock();
116
        $orgRep->expects($this->exactly(2))
117
               ->method('findByUser')
118
               ->withConsecutive(array('testOwner'), array('testUser'))
119
               ->will($this->onConsecutiveCalls($ownerOrg, $userOrg));
120
121
122
        $ownerOrgRef = new OrganizationReference('testOwner', $orgRep);
123
        $userOrgRef = new OrganizationReference('testUser', $orgRep);
124
125
        $user->setOrganization($userOrgRef);
126
127
        $owner = new User();
128
        $owner->getInfo()->setFirstName('Test')->setLastName('Owner');
129
        $owner->setOrganization($ownerOrgRef);
130
131
        $authService = $this->getMockBuilder('\Auth\AuthenticationService')->disableOriginalConstructor()->getMock();
132
        $authService->expects($this->once())->method('getUser')->willReturn($owner);
133
134
        $router = $this->getMockForAbstractClass('\Zend\Mvc\Router\RouteStackInterface');
135
        $router->expects($this->once())
136
               ->method('assemble')
137
               ->with(array('action' => 'accept'),
138
                      array('name' => 'lang/organizations/invite',
139
                            'query' => array('token' => $options['token'], 'organization' => $ownerOrg->getId()))
140
               )
141
               ->willReturn('testUrl');
142
143
        $services = $this->getMockBuilder('\Zend\ServiceManager\ServiceManager')->disableOriginalConstructor()->getMock();
144
145
        $services->expects($this->exactly(2))
146
                 ->method('get')
147
                 ->withConsecutive(
148
                        array('AuthenticationService'),
149
                        array('Router')
150
                 )->will($this->onConsecutiveCalls($authService, $router));
151
152
        $mailService = $this->getMockBuilder('\Core\Mail\MailService')->disableOriginalConstructor()->getMock();
153
154
        $mailService->expects($this->once())->method('getServiceLocator')->willReturn($services);
155
156
        $mailMock = new HTMLTemplateMessage();
157
        $translator = $this->getMockBuilder('\Zend\I18n\Translator\Translator')->disableOriginalConstructor()->getMock();
158
        $translator->expects($this->any())->method('translate')->will($this->returnArgument(0));
159
        $mailMock->setTranslator($translator);
160
        $mailService->expects($this->once())->method('get')->with('htmltemplate')
161
                    ->willReturn($mailMock);
162
163
164
        $target = new EmployeeInvitationFactory();
165
        $target->setCreationOptions($options);
166
        $mail = $target->createService($mailService);
167
168
169
        $vars = $mail->getVariables()->getArrayCopy();
170
171
        $expected = Array(
172
            'inviter' => 'Test Owner',
173
            'organization' => 'TestOwnerOrg',
174
            'token' => $options['token'],
175
            'user' => '',
176
            'hasAssociation' => true,
177
            'url' => 'testUrl',
178
            'isOwner' => true,
179
            'currentOrganization' => 'TestUserOrg',
180
            'template' => 'testTemplate',
181
        );
182
183
        $this->assertEquals($expected, $vars);
184
        $this->assertEquals($user->getEmail(), $mail->getTo()->current()->getEmail());
185
    }
186
}