Completed
Pull Request — master (#11)
by
unknown
05:40 queued 02:59
created

FormAuthenticatorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 7
c 3
b 2
f 1
lcom 1
cbo 14
dl 0
loc 66
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A testFormAuthenticatorGetUser() 0 7 1
A testCheckCredentials() 0 6 1
A testBadCredentialsException() 0 5 1
A testOnAuthenticationSuccess() 0 14 1
A getCredentials() 0 4 1
A getUser() 0 4 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Tests\Security;
4
5
use PHPUnit_Framework_TestCase;
6
use SumoCoders\FrameworkMultiUserBundle\Security\FormAuthenticator;
7
use SumoCoders\FrameworkMultiUserBundle\Security\FormCredentials;
8
use SumoCoders\FrameworkMultiUserBundle\Security\ObjectUserProvider;
9
use SumoCoders\FrameworkMultiUserBundle\User\InMemoryUserRepository;
10
use SumoCoders\FrameworkMultiUserBundle\User\User;
11
use Symfony\Bundle\FrameworkBundle\Routing\Router;
12
use Symfony\Component\DependencyInjection\Container;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Session\Session;
15
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
16
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
17
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
18
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
19
20
class FormAuthenticatorTest extends PHPUnit_Framework_TestCase
21
{
22
    private $formAuthenticator;
23
    private $router;
24
25
    public function setUp()
26
    {
27
        $this->router = new Router(new Container(), '');
28
        $encoders['SumoCoders\FrameworkMultiUserBundle\User\User'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$encoders was never initialized. Although not strictly required by PHP, it is generally a good practice to add $encoders = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
29
            'class' => 'Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder',
30
            'arguments' => [12],
31
        ];
32
        $encoder = new UserPasswordEncoder(new EncoderFactory($encoders));
33
        $redirectRoutes = [];
34
        $this->formAuthenticator = new FormAuthenticator($encoder, $this->router, $redirectRoutes);
35
    }
36
37
    public function testFormAuthenticatorGetUser()
38
    {
39
        $provider = new ObjectUserProvider(new InMemoryUserRepository());
40
        $user = $this->formAuthenticator->getUser($this->getCredentials(), $provider);
41
42
        $this->assertEquals($this->getUser(), $user);
43
    }
44
45
    public function testCheckCredentials()
46
    {
47
        $this->assertTrue(
48
            $this->formAuthenticator->checkCredentials($this->getCredentials(), $this->getUser())
49
        );
50
    }
51
52
    /**
53
     * @expectedException Symfony\Component\Security\Core\Exception\BadCredentialsException
54
     */
55
    public function testBadCredentialsException()
56
    {
57
        $this->expectException('Symfony\Component\Security\Core\Exception\BadCredentialsException');
58
        $this->formAuthenticator->checkCredentials($this->getCredentials('wouter', 'wrongPassword'), $this->getUser());
59
    }
60
61
    public function testOnAuthenticationSuccess()
62
    {
63
        $request = new Request();
64
        $providerKey = 'main';
65
        $token = new UsernamePasswordToken('wouter', null, $providerKey, ['ROLE_USER']);
66
67
        $session = new Session(new MockArraySessionStorage());
68
        $session->set('_security_'.$providerKey, serialize($token));
69
        $session->set('_security.'.$providerKey.'.target_path', '/randomURL');
70
        $session->save();
71
        $request->setSession($session);
72
73
        $this->formAuthenticator->onAuthenticationSuccess($request, $token, $providerKey);
74
    }
75
76
    private function getCredentials($username = 'wouter', $password = 'test')
77
    {
78
        return new FormCredentials($username, $password);
79
    }
80
81
    private function getUser($username = 'wouter', $password = 'test', $displayName = 'Wouter Sioen')
82
    {
83
        return new User($username, $password, $displayName);
84
    }
85
}
86