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

InvitationHandlerTest::testCreatesNewUser()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 34
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
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 Organizations\Controller\Plugin\InvitationHandler;
15
16
/**
17
 * Tests for \Organizations\Controller\Plugin\InvitationHandler
18
 * 
19
 * @covers \Organizations\Controller\Plugin\InvitationHandler
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @group Organizations
22
 * @group Organizations.Controller
23
 * @group Organizations.Controller.Plugin
24
 */
25
class InvitationHandlerTest extends \PHPUnit_Framework_TestCase
26
{
27
28
    private $target;
29
    private $emailValidatorMock;
30
    private $translatorMock;
31
    private $userRepositoryMock;
32
    private $userTokenGeneratorMock;
33
    private $mailerPluginMock;
34
35
    public function setUp()
36
    {
37
        $name = $this->getName(false);
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
        $this->target = new InvitationHandler();
39
    }
40
41
    private function setupMocks($mocks = array(), $inject = false)
42
    {
43
        if (is_bool($mocks) || (is_array($mocks) && empty($mocks))) {
44
            $inject = $mocks;
45
            $mocks = array('emailValidator', 'translator', 'userRepository', 'userTokenGenerator', 'mailerPlugin');
46
        }
47
48
        if (!is_array($mocks)) {
49
            $mocks = array($mocks);
50
        }
51
52
        if (in_array('emailValidator', $mocks)) {
53
            $this->emailValidatorMock = new \Zend\Validator\EmailAddress();
54
        }
55
        if (in_array('translator', $mocks)) {
56
            $this->translatorMock     = $this->getMock('\Zend\I18n\Translator\Translator');
57
        }
58
        if (in_array('userRepository', $mocks)) {
59
            $this->userRepositoryMock = $this->getMockBuilder('\Auth\Repository\User')->disableOriginalConstructor()->getMock();
60
        }
61
        if (in_array('userTokenGenerator', $mocks)) {
62
            $this->userTokenGeneratorMock = $this->getMockBuilder('\Auth\Service\UserUniqueTokenGenerator')
63
                                             ->disableOriginalConstructor()->getMock();
64
        }
65
        if (in_array('mailerPlugin', $mocks)) {
66
            $this->mailerPluginMock   = $this->getMockBuilder('\Core\Controller\Plugin\Mailer')
67
                                         ->disableOriginalConstructor()->getMock();
68
        }
69
70
        if ($inject) {
71
            foreach ($mocks as $key) {
72
                $setter = 'set' . $key;
73
                $this->target->$setter($this->{$key . 'Mock'});
74
            }
75
        }
76
    }
77
78
79
    /**
80
     * @testdox Extends \Zend\Mvc\Controller\Plugin\AbstractPlugin
81
     */
82
    public function testExtendsAbstractPlugin()
83
    {
84
        $this->assertInstanceOf('\Zend\Mvc\Controller\Plugin\AbstractPlugin', $this->target);
85
    }
86
87
    public function provideSetterAndGetterTestData()
88
    {
89
        return array(
90
            array('emailValidator'),
91
            array('translator'),
92
            array('userRepository'),
93
            array('userTokenGenerator'),
94
            array('mailerPlugin')
95
        );
96
    }
97
98
    /**
99
     * Allows setting and getting dependencies.
100
     * @dataProvider provideSetterAndGetterTestData()
101
     *
102
     * @param $mockKey
103
     */
104
    public function testSetterAndGetter($mockKey)
105
    {
106
        $this->setupMocks($mockKey);
107
108
        $setter = 'set' . ucfirst($mockKey);
109
        $getter = 'get' . ucfirst($mockKey);
110
        $value  = $this->{$mockKey . 'Mock'};
111
112
        $this->assertSame($this->target, $this->target->$setter($value), 'Fluent interface broken!');
113
        $this->assertSame($value, $this->target->$getter());
114
    }
115
116
    /**
117
     * @testdox Throws exception if dependencies are missing.
118
     * @dataProvider provideSetterAndGetterTestData()
119
     *
120
     * @expectedException \Core\Exception\MissingDependencyException
121
     */
122
    public function testGetterThrowExceptionIfDependencyMissing($getterName)
123
    {
124
        $getter = "get" . $getterName;
125
126
        $this->target->$getter();
127
    }
128
129
    public function testReturnsErrorResultIfEmailAddressIsInvalidOrEmpty()
130
    {
131
        $this->setupMocks(array('emailValidator', 'translator'), /* inject */ true);
132
        $email = 'invalidEmailAddress';
133
        $message = 'Email address is invalid.';
134
        $this->translatorMock->expects($this->exactly(5))
135
                             ->method('translate')
136
                             ->with($message)
137
                             ->will($this->returnArgument(0));
138
139
        $expected = array(
140
            'ok' => false,
141
            'message' => $message,
142
        );
143
144
        foreach (array($email, null, '', 0, array()) as $testEmail)  {
145
            $result = $this->target->process($testEmail);
146
147
            $this->assertEquals($expected, $result);
148
        }
149
    }
150
151
    public function testFindsExistentUsers()
152
    {
153
        $email = '[email protected]';
154
        $this->setupMocks(true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
156
        $user = new User();
157
        $user->setId('testUserId');
158
        $user->setEmail($email);
159
        $user->getInfo()->setEmail($email);
160
161
        $this->userRepositoryMock->expects($this->once())->method('findByEmail')->with($email, null)
162
                                 ->willReturn($user);
163
        $this->userRepositoryMock->expects($this->never())->method('create');
164
        $this->userTokenGeneratorMock->expects($this->once())->method('generate')->with($user, 7)->willReturn('testToken');
165
166
        $this->mailerPluginMock->expects($this->once())->method('__invoke');
167
168
        $expected = array(
169
            'ok' => true,
170
            'result' => array(
171
                'userId' => $user->getId(),
172
                'userName' => $user->getInfo()->getDisplayName(),
173
                'userEmail' => $email
174
            )
175
        );
176
177
        $result = $this->target->process($email);
178
179
        $this->assertEquals($expected, $result);
180
    }
181
182
    public function testCreatesNewUser()
183
    {
184
        $this->setupMocks(true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
186
        $email = '[email protected]';
187
188
        $user = new User();
189
        $user->setId('testUserId');
190
        $user->setEmail($email);
191
        $user->getInfo()->setEmail($email);
192
193
        $this->userRepositoryMock->expects($this->once())->method('findByEmail')->with($email, null)
194
                                 ->willReturn(null);
195
        $this->userRepositoryMock->expects($this->once())->method('create')->willReturn($user);
196
        $this->userTokenGeneratorMock->expects($this->once())->method('generate')->with($user, 7)->willReturn('testToken');
197
198
        $this->mailerPluginMock->expects($this->once())->method('__invoke');
199
200
        $expected = array(
201
            'ok' => true,
202
            'result' => array(
203
                'userId' => $user->getId(),
204
                'userName' => $user->getInfo()->getDisplayName(),
205
                'userEmail' => $email
206
            )
207
        );
208
209
        $result = $this->target->process($email);
210
211
        $this->assertEquals($result, $expected);
212
        $this->assertTrue($user->isDraft(), 'User is not in draft mode!');
213
214
215
    }
216
217
    public function testReturnsErrorResultIfMailSendingFailed()
218
    {
219
        $email = '[email protected]';
220
        $this->setupMocks(true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
221
222
        $user = new User();
223
        $user->setId('testUserId');
224
        $user->setEmail($email);
225
        $user->getInfo()->setEmail($email);
226
227
        $message = 'Sending invitation mail failed.';
228
        $this->translatorMock->expects($this->once())
229
                             ->method('translate')
230
                             ->with($message)
231
                             ->will($this->returnArgument(0));
232
233
        $this->userRepositoryMock->expects($this->once())->method('findByEmail')->with($email, null)
234
                                 ->willReturn($user);
235
        $this->userRepositoryMock->expects($this->never())->method('create');
236
        $this->userTokenGeneratorMock->expects($this->once())->method('generate')->with($user, 7)->willReturn('testToken');
237
238
        $this->mailerPluginMock->expects($this->once())->method('__invoke')->will($this->throwException(new \Exception()));
239
240
        $expected = array(
241
            'ok' => false,
242
            'message' => $message
243
        );
244
245
        $result = $this->target->process($email);
246
247
        $this->assertEquals($expected, $result);
248
    }
249
}