Completed
Push — master ( d5bbb9...ab4758 )
by Milos
04:50 queued 02:32
created

Provider/SamlSpAuthenticationProviderTest.php (31 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 AerialShip\SamlSPBundle\Tests\Security\Core\Authentication\Provider;
4
5
use AerialShip\SamlSPBundle\Security\Core\Authentication\Provider\SamlSpAuthenticationProvider;
6
use AerialShip\SamlSPBundle\Security\Core\Authentication\Token\SamlSpToken;
7
use AerialShip\SamlSPBundle\Tests\Bridge\SamlSpInfoHelper;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
10
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
11
12
class SamlSpAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @test
16
     */
17
    public function couldBeConstructedWithProviderKey()
18
    {
19
        new SamlSpAuthenticationProvider('key');
20
    }
21
22
23
    /**
24
     * @test
25
     */
26
    public function couldBeConstructedWithUserManagerAndUserChecker()
27
    {
28
        new SamlSpAuthenticationProvider(
29
            'main',
30
            $this->createUserManagerMock(),
0 ignored issues
show
It seems like $this->createUserManagerMock() targeting AerialShip\SamlSPBundle\...createUserManagerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
31
            $this->createUserCheckerMock()
0 ignored issues
show
It seems like $this->createUserCheckerMock() targeting AerialShip\SamlSPBundle\...createUserCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
32
        );
33
    }
34
35
    /**
36
     * @test
37
     */
38
    public function couldBeConstructedWithUserManagerAndUserCheckerAndCreateUserIfNotExist()
39
    {
40
        new SamlSpAuthenticationProvider(
41
            'main',
42
            $this->createUserManagerMock(),
0 ignored issues
show
It seems like $this->createUserManagerMock() targeting AerialShip\SamlSPBundle\...createUserManagerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
43
            $this->createUserCheckerMock(),
0 ignored issues
show
It seems like $this->createUserCheckerMock() targeting AerialShip\SamlSPBundle\...createUserCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44
            true
45
        );
46
    }
47
48
49
    /**
50
     * @test
51
     * @expectedException \InvalidArgumentException
52
     */
53
    public function throwIfTryConstructWithUserManagerButWithoutUserChecker()
54
    {
55
        new SamlSpAuthenticationProvider(
56
            'main',
57
            $this->createUserManagerMock(),
0 ignored issues
show
It seems like $this->createUserManagerMock() targeting AerialShip\SamlSPBundle\...createUserManagerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
58
            null,
59
            false
60
        );
61
    }
62
63
    /**
64
     * @test
65
     * @expectedException \InvalidArgumentException
66
     */
67
    public function throwIfTryConstructWithCreateIfNotExistSetTrueButWithoutUserManager()
68
    {
69
        new SamlSpAuthenticationProvider(
70
            'main',
71
            null,
72
            null,
73
            true
74
        );
75
    }
76
77
78
    /**
79
     * @test
80
     */
81
    public function shouldSupportSamlSpToken()
82
    {
83
        $providerKey = 'main';
84
        $authProvider = new SamlSpAuthenticationProvider($providerKey);
85
        $this->assertTrue($authProvider->supports(new SamlSpToken($providerKey)));
86
    }
87
88
    /**
89
     * @test
90
     */
91
    public function shouldNotSupportNonOpenIdToken()
92
    {
93
        $authProvider = new SamlSpAuthenticationProvider('main');
94
95
        /** @var $nonOpenIdToken TokenInterface */
96
        $nonOpenIdToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
97
98
        $this->assertFalse($authProvider->supports($nonOpenIdToken));
99
        $this->assertNull($authProvider->authenticate($nonOpenIdToken));
100
    }
101
102
103
    /**
104
     * @test
105
     */
106
    public function shouldNotSupportSamlSpTokenIfProviderKeyDiffers()
107
    {
108
        $token = new SamlSpToken('the_other_provider_key');
109
        $authProvider = new SamlSpAuthenticationProvider('the_provider_key');
110
        $this->assertFalse($authProvider->supports($token));
111
    }
112
113
114
    /**
115
     * @test
116
     */
117
    public function shouldCreateAuthenticatedTokenUsingUserAndHisRolesFromToken()
118
    {
119
        $samlSpInfoHelper = new SamlSpInfoHelper();
120
121
        $providerKey = 'main';
122
        $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
123
124
        $expectedUserMock = $this->createUserMock();
125
        $expectedUserMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Security\Core\User\UserInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
126
                ->expects($this->any())
127
                ->method('getRoles')
128
                ->will($this->returnValue(array('foo', 'bar')))
129
        ;
130
131
        $authProvider = new SamlSpAuthenticationProvider(
132
            $providerKey,
133
            $userManager = null,
134
            $this->createUserCheckerMock()
0 ignored issues
show
It seems like $this->createUserCheckerMock() targeting AerialShip\SamlSPBundle\...createUserCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
135
        );
136
137
        $token = new SamlSpToken($providerKey);
138
        $token->setUser($expectedUserMock);
139
        $token->setSamlSpInfo($expectedSamlSpInfo);
140
141
        /** @var  $authenticatedToken SamlSpToken */
142
        $authenticatedToken = $authProvider->authenticate($token);
143
144
        $this->assertInstanceOf('AerialShip\SamlSPBundle\Security\Core\Authentication\Token\SamlSpToken', $authenticatedToken);
145
        $this->assertNotSame($token, $authenticatedToken);
146
        $this->assertTrue($authenticatedToken->isAuthenticated());
147
        $this->assertEquals($expectedSamlSpInfo, $authenticatedToken->getSamlSpInfo());
148
        $this->assertSame($authenticatedToken->getUser(), $expectedUserMock);
149
150
        $roles = $authenticatedToken->getRoles();
151
        $this->assertInternalType('array', $roles);
152
        $this->assertCount(2, $roles);
153
154
        $this->assertEquals('foo', $roles[0]->getRole());
155
        $this->assertEquals('bar', $roles[1]->getRole());
156
    }
157
158
159
160
161
    /**
162
     * @test
163
     */
164
    public function shouldCreateAuthenticatedTokenUsingUserFromTokenAndCallPostAuthCheck()
165
    {
166
        $samlSpInfoHelper = new SamlSpInfoHelper();
167
168
        $providerKey = 'main';
169
        $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
170
171
        $userMock = $this->createUserMock();
172
        $userMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Security\Core\User\UserInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
173
                ->expects($this->any())
174
                ->method('getRoles')
175
                ->will($this->returnValue(array()))
176
        ;
177
178
        $userCheckerMock = $this->createUserCheckerMock();
179
        $userCheckerMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Securi...er\UserCheckerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
180
                ->expects($this->once())
181
                ->method('checkPostAuth')
182
                ->with($userMock)
183
        ;
184
185
        $authProvider = new SamlSpAuthenticationProvider(
186
            $providerKey,
187
            $this->createUserManagerMock(),
0 ignored issues
show
It seems like $this->createUserManagerMock() targeting AerialShip\SamlSPBundle\...createUserManagerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
188
            $userCheckerMock
0 ignored issues
show
It seems like $userCheckerMock defined by $this->createUserCheckerMock() on line 178 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
189
        );
190
191
        $token = new SamlSpToken($providerKey);
192
        $token->setUser($userMock);
193
        $token->setSamlSpInfo($expectedSamlSpInfo);
194
195
        $authenticatedToken = $authProvider->authenticate($token);
196
197
        $this->assertInstanceOf('AerialShip\SamlSPBundle\Security\Core\Authentication\Token\SamlSpToken', $authenticatedToken);
198
        $this->assertSame($authenticatedToken->getUser(), $userMock);
199
    }
200
201
202
    /**
203
     * @test
204
     */
205
    public function shouldCreateAuthenticatedTokenUsingIdentityIfUserManagerNotSet()
206
    {
207
        $samlSpInfoHelper = new SamlSpInfoHelper();
208
209
        $expectedProviderKey = 'the_provider_key';
210
        $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
211
        $expectedUsername = $expectedSamlSpInfo->getNameID()->getValue();
212
213
        $authProvider = new SamlSpAuthenticationProvider($expectedProviderKey);
214
215
        $token = new SamlSpToken($expectedProviderKey);
216
        $token->setUser('');
217
        $token->setSamlSpInfo($expectedSamlSpInfo);
218
219
        /** @var $authenticatedToken SamlSpToken */
220
        $authenticatedToken = $authProvider->authenticate($token);
221
222
        $this->assertInstanceOf('AerialShip\SamlSPBundle\Security\Core\Authentication\Token\SamlSpToken', $authenticatedToken);
223
        $this->assertNotSame($token, $authenticatedToken);
224
        $this->assertTrue($authenticatedToken->isAuthenticated());
225
        $this->assertEquals($expectedSamlSpInfo, $authenticatedToken->getSamlSpInfo());
226
        $this->assertEquals($expectedProviderKey, $authenticatedToken->getProviderKey());
227
228
        /** @var $user \Symfony\Component\Security\Core\User\User */
229
        $user = $authenticatedToken->getUser();
230
        $this->assertInstanceOf('Symfony\Component\Security\Core\User\User', $user);
231
        $this->assertEquals($expectedUsername, $user->getUsername());
232
233
        $roles = $authenticatedToken->getRoles();
234
        $this->assertInternalType('array', $roles);
235
        $this->assertCount(1, $roles);
236
        $this->assertEquals('ROLE_USER', $roles[0]->getRole());
237
    }
238
239
240
    /**
241
     * @test
242
     */
243
    public function shouldCreateAuthenticatedTokenUsingUserManagerAndSearchBySamlSpInfo()
244
    {
245
        $samlSpInfoHelper = new SamlSpInfoHelper();
246
247
        $expectedProviderKey = 'the_provider_key';
248
        $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
249
250
        $expectedUserMock = $this->createUserMock();
251
        $expectedUserMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Security\Core\User\UserInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
252
                ->expects($this->any())
253
                ->method('getRoles')
254
                ->will($this->returnValue(array('foo', 'bar')))
255
        ;
256
257
        $userManagerMock = $this->createUserManagerMock();
258
        $userManagerMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in AerialShip\SamlSPBundle\...er\UserManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
259
                ->expects($this->once())
260
                ->method('loadUserBySamlInfo')
261
                ->with($expectedSamlSpInfo)
262
                ->will($this->returnValue($expectedUserMock))
263
        ;
264
265
        $authProvider = new SamlSpAuthenticationProvider(
266
            $expectedProviderKey,
267
            $userManagerMock,
0 ignored issues
show
It seems like $userManagerMock defined by $this->createUserManagerMock() on line 257 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
268
            $this->createUserCheckerMock()
0 ignored issues
show
It seems like $this->createUserCheckerMock() targeting AerialShip\SamlSPBundle\...createUserCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
269
        );
270
271
        $token = new SamlSpToken($expectedProviderKey);
272
        $token->setUser('');
273
        $token->setSamlSpInfo($expectedSamlSpInfo);
274
275
        /** @var $authenticatedToken SamlSpToken */
276
        $authenticatedToken = $authProvider->authenticate($token);
277
278
        $this->assertInstanceOf('AerialShip\SamlSPBundle\Security\Core\Authentication\Token\SamlSpToken', $authenticatedToken);
279
        $this->assertNotSame($token, $authenticatedToken);
280
        $this->assertTrue($authenticatedToken->isAuthenticated());
281
        $this->assertEquals($expectedSamlSpInfo, $authenticatedToken->getSamlSpInfo());
282
        $this->assertEquals($expectedProviderKey, $authenticatedToken->getProviderKey());
283
        $this->assertEquals($expectedUserMock, $authenticatedToken->getUser());
284
285
        $roles = $authenticatedToken->getRoles();
286
        $this->assertInternalType('array', $roles);
287
        $this->assertCount(2, $roles);
288
289
        $this->assertEquals('foo', $roles[0]->getRole());
290
        $this->assertEquals('bar', $roles[1]->getRole());
291
    }
292
293
294
    /**
295
     * @test
296
     *
297
     * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
298
     * @expectedExceptionMessage User provider did not return an implementation of user interface.
299
     */
300
    public function throwIfUserManagerReturnNonUserInstance()
301
    {
302
        $samlSpInfoHelper = new SamlSpInfoHelper();
303
304
        $providerKey = 'main';
305
        $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
306
307
        $userProviderMock = $this->createUserManagerMock();
308
        $userProviderMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in AerialShip\SamlSPBundle\...er\UserManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
309
                ->expects($this->once())
310
                ->method('loadUserBySamlInfo')
311
                ->with($expectedSamlSpInfo)
312
                ->will($this->returnValue('not-valid-user-instance'))
313
        ;
314
315
        $authProvider = new SamlSpAuthenticationProvider(
316
            $providerKey,
317
            $userProviderMock,
0 ignored issues
show
It seems like $userProviderMock defined by $this->createUserManagerMock() on line 307 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
318
            $this->createUserCheckerMock()
0 ignored issues
show
It seems like $this->createUserCheckerMock() targeting AerialShip\SamlSPBundle\...createUserCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
319
        );
320
321
        $token = new SamlSpToken($providerKey);
322
        $token->setUser('');
323
        $token->setSamlSpInfo($expectedSamlSpInfo);
324
325
        $authProvider->authenticate($token);
326
    }
327
328
329
    /**
330
     * @test
331
     *
332
     * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
333
     * @expectedExceptionMessage Cannot find user by saml sp info
334
     */
335
    public function shouldNotCreateUserIfCreateIfNotExistParamIsNotSet()
336
    {
337
        $samlSpInfoHelper = new SamlSpInfoHelper();
338
339
        $providerKey = 'main';
340
        $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
341
342
        $userManagerMock = $this->createUserManagerMock();
343
        $userManagerMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in AerialShip\SamlSPBundle\...er\UserManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
344
                ->expects($this->once())
345
                ->method('loadUserBySamlInfo')
346
                ->with($expectedSamlSpInfo)
347
                ->will($this->throwException(new UsernameNotFoundException('Cannot find user by saml sp info')))
348
        ;
349
        $userManagerMock
350
                ->expects($this->never())
351
                ->method('createUserFromSamlInfo')
352
        ;
353
354
        $authProvider = new SamlSpAuthenticationProvider(
355
            $providerKey,
356
            $userManagerMock,
0 ignored issues
show
It seems like $userManagerMock defined by $this->createUserManagerMock() on line 342 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
357
            $this->createUserCheckerMock(),
0 ignored issues
show
It seems like $this->createUserCheckerMock() targeting AerialShip\SamlSPBundle\...createUserCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
358
            $createIfNotExist = false
359
        );
360
361
        $token = new SamlSpToken($providerKey);
362
        $token->setUser('');
363
        $token->setSamlSpInfo($expectedSamlSpInfo);
364
365
        $authProvider->authenticate($token);
366
    }
367
368
369
370
    /**
371
     * @test
372
     */
373
    public function shouldCreateAuthenticatedTokenUsingUserManagerCreateFromSamlSpInfoMethod()
374
    {
375
        $samlSpInfoHelper = new SamlSpInfoHelper();
376
377
        $expectedProviderKey = 'main';
378
        $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
379
380
        $expectedUserMock = $this->createUserMock();
381
        $expectedUserMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Security\Core\User\UserInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
382
                ->expects($this->any())
383
                ->method('getRoles')
384
                ->will($this->returnValue(array('foo', 'bar')))
385
        ;
386
387
        $userManagerMock = $this->createUserManagerMock();
388
        $userManagerMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in AerialShip\SamlSPBundle\...er\UserManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
389
                ->expects($this->once())
390
                ->method('loadUserBySamlInfo')
391
                ->with($expectedSamlSpInfo)
392
                ->will($this->throwException(new UsernameNotFoundException('Cannot find user by saml sp info')))
393
        ;
394
        $userManagerMock
395
                ->expects($this->once())
396
                ->method('createUserFromSamlInfo')
397
                ->with($expectedSamlSpInfo)
398
                ->will($this->returnValue($expectedUserMock))
399
        ;
400
401
        $authProvider = new SamlSpAuthenticationProvider(
402
            $expectedProviderKey,
403
            $userManagerMock,
0 ignored issues
show
It seems like $userManagerMock defined by $this->createUserManagerMock() on line 387 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
404
            $this->createUserCheckerMock(),
0 ignored issues
show
It seems like $this->createUserCheckerMock() targeting AerialShip\SamlSPBundle\...createUserCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
405
            $createIfNotExist = true
406
        );
407
408
        $token = new SamlSpToken($expectedProviderKey);
409
        $token->setUser('');
410
        $token->setSamlSpInfo($expectedSamlSpInfo);
411
412
        /** @var $authenticatedToken SamlSpToken */
413
        $authenticatedToken = $authProvider->authenticate($token);
414
415
        $this->assertInstanceOf('AerialShip\SamlSPBundle\Security\Core\Authentication\Token\SamlSpToken', $authenticatedToken);
416
        $this->assertNotSame($token, $authenticatedToken);
417
        $this->assertTrue($authenticatedToken->isAuthenticated());
418
        $this->assertEquals($expectedSamlSpInfo, $authenticatedToken->getSamlSpInfo());
419
        $this->assertEquals($expectedProviderKey, $authenticatedToken->getProviderKey());
420
        $this->assertEquals($expectedUserMock, $authenticatedToken->getUser());
421
422
        $roles = $authenticatedToken->getRoles();
423
        $this->assertInternalType('array', $roles);
424
        $this->assertCount(2, $roles);
425
426
        $this->assertEquals('foo', $roles[0]->getRole());
427
        $this->assertEquals('bar', $roles[1]->getRole());
428
    }
429
430
431
    /**
432
     * @test
433
     *
434
     * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
435
     * @expectedExceptionMessage User provider did not return an implementation of user interface.
436
     */
437
    public function throwIfUserManagerCreateNotUserInstance()
438
    {
439
        $samlSpInfoHelper = new SamlSpInfoHelper();
440
441
        $providerKey = 'main';
442
        $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
443
444
        $userManagerMock = $this->createUserManagerMock();
445
        $userManagerMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in AerialShip\SamlSPBundle\...er\UserManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
446
                ->expects($this->once())
447
                ->method('loadUserBySamlInfo')
448
                ->with($expectedSamlSpInfo)
449
                ->will($this->throwException(new UsernameNotFoundException('Cannot find user by saml sp info')))
450
        ;
451
        $userManagerMock
452
                ->expects($this->once())
453
                ->method('createUserFromSamlInfo')
454
                ->with($expectedSamlSpInfo)
455
                ->will($this->returnValue('not-a-user-instance'))
456
        ;
457
458
        $authProvider = new SamlSpAuthenticationProvider(
459
            $providerKey,
460
            $userManagerMock,
0 ignored issues
show
It seems like $userManagerMock defined by $this->createUserManagerMock() on line 444 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
461
            $this->createUserCheckerMock(),
0 ignored issues
show
It seems like $this->createUserCheckerMock() targeting AerialShip\SamlSPBundle\...createUserCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
462
            $createIfNotExist = true
463
        );
464
465
        $token = new SamlSpToken($providerKey);
466
        $token->setUser('');
467
        $token->setSamlSpInfo($expectedSamlSpInfo);
468
469
        $authProvider->authenticate($token);
470
    }
471
472
473
474
    /**
475
     * @test
476
     */
477
    public function shouldWrapAnyThrownExceptionsAsAuthenticatedServiceException()
478
    {
479
        $samlSpInfoHelper = new SamlSpInfoHelper();
480
        $providerKey = 'main';
481
        $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
482
        $expectedPreviousException = new \Exception(
483
            $expectedMessage = 'Something goes wrong',
484
            $expectedCode = 21
485
        );
486
487
        $userProviderMock = $this->createUserManagerMock();
488
        $userProviderMock
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in AerialShip\SamlSPBundle\...er\UserManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
489
                ->expects($this->once())
490
                ->method('loadUserBySamlInfo')
491
                ->will($this->throwException($expectedPreviousException))
492
        ;
493
494
        $authProvider = new SamlSpAuthenticationProvider(
495
            $providerKey,
496
            $userProviderMock,
0 ignored issues
show
It seems like $userProviderMock defined by $this->createUserManagerMock() on line 487 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<AerialShip\S...r\UserManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
497
            $this->createUserCheckerMock()
0 ignored issues
show
It seems like $this->createUserCheckerMock() targeting AerialShip\SamlSPBundle\...createUserCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, AerialShip\SamlSPBundle\...Provider::__construct() does only seem to accept null|object<Symfony\Comp...r\UserCheckerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
498
        );
499
500
        $token = new SamlSpToken($providerKey);
501
        $token->setUser('');
502
        $token->setSamlSpInfo($expectedSamlSpInfo);
503
504
        try {
505
            $authProvider->authenticate($token);
506
        } catch (AuthenticationServiceException $e) {
507
            $this->assertSame($expectedPreviousException, $e->getPrevious(), $e->getPrevious());
508
            $this->assertEquals($expectedMessage, $e->getMessage());
509
            $this->assertEquals($expectedCode, $e->getCode());
510
            $this->assertNull($e->getToken());
511
512
            return;
513
        }
514
515
        $this->fail('Expected exception: AuthenticationServiceException was not thrown');
516
    }
517
518
519
520
    /**
521
     * @return \PHPUnit_Framework_MockObject_MockObject|\AerialShip\SamlSPBundle\Security\Core\User\UserManagerInterface
522
     */
523
    protected function createUserManagerMock()
524
    {
525
        return $this->getMock('AerialShip\SamlSPBundle\Security\Core\User\UserManagerInterface');
526
    }
527
528
    /**
529
     * @return \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Security\Core\User\UserCheckerInterface
530
     */
531
    protected function createUserCheckerMock()
532
    {
533
        return $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
534
    }
535
536
    /**
537
     * @return \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Security\Core\User\UserInterface
538
     */
539
    protected function createUserMock()
540
    {
541
        return $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
542
    }
543
}
544