Completed
Push — master ( c76949...c15727 )
by Wouter
10s
created

FormAuthenticatorTest::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
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\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Session\Session;
14
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
15
use Symfony\Component\Routing\RouterInterface;
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 = $this->getMock(RouterInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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->setExpectedException('Symfony\Component\Security\Core\Exception\BadCredentialsException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
        $this->formAuthenticator->checkCredentials(
59
            $this->getCredentials('wouter', 'wrongPassword'),
60
            $this->getUser()
61
        );
62
    }
63
64
    public function testOnAuthenticationSuccess()
65
    {
66
        $request = new Request();
67
        $providerKey = 'main';
68
        $token = new UsernamePasswordToken('wouter', null, $providerKey, ['ROLE_USER']);
69
70
        $session = new Session(new MockArraySessionStorage());
71
        $session->set('_security_'.$providerKey, serialize($token));
72
        $session->set('_security.'.$providerKey.'.target_path', '/randomURL');
73
        $session->save();
74
        $request->setSession($session);
75
76
        $this->formAuthenticator->onAuthenticationSuccess($request, $token, $providerKey);
77
    }
78
79
    private function getCredentials($username = 'wouter', $password = 'test')
80
    {
81
        $mock = $this->getMock(FormCredentials::class, [], [$username, $password]);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
        $mock->method('getUserName')->willReturn($username);
83
        $mock->method('getPlainPassword')->willReturn($password);
84
85
        return $mock;
86
    }
87
88
    private function getUser($username = 'wouter', $password = 'test', $displayName = 'Wouter Sioen')
89
    {
90
        return new User($username, $password, $displayName);
91
    }
92
}
93