Completed
Push — master ( 93b50d...02fb34 )
by Dominik
10:08 queued 03:07
created

testEncryptDecryptEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Azine\EmailUpdateConfirmationBundle\Tests;
4
5
use Azine\EmailUpdateConfirmationBundle\Services\EmailUpdateConfirmation;
6
use FOS\UserBundle\Mailer\MailerInterface;
7
use FOS\UserBundle\Model\User;
8
use FOS\UserBundle\Util\TokenGenerator;
9
use Symfony\Component\EventDispatcher\EventDispatcher;
10
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
11
use Symfony\Component\Routing\RouterInterface;
12
use Symfony\Component\Validator\ConstraintViolationList;
13
use Symfony\Component\Validator\Validator\ValidatorInterface;
14
15
class EmailUpdateConfirmationTest extends \PHPUnit_Framework_TestCase
16
{
17
    /** @var ExpressionFunctionProviderInterface */
18
    private $provider;
19
    /** @var RouterInterface */
20
    private $router;
21
    /** @var TokenGenerator */
22
    private $tokenGenerator;
23
    /** @var MailerInterface */
24
    private $mailer;
25
    /** @var EventDispatcher */
26
    private $eventDispatcher;
27
    /** @var string */
28
    private $redirectRoute = 'redirect_route';
29
    /** @var string */
30
    private $token = 'test_token';
31
    /** @var EmailUpdateConfirmation */
32
    private $emailUpdateConfirmation;
33
    /** @var string */
34
    private $emailTest = '[email protected]';
35
    /** @var User */
36
    private $user;
37
    private $cypher_method = 'AES-128-CBC';
38
39
    /** @var ValidatorInterface */
40
    private $emailValidator;
41
    /** @var ConstraintViolationList */
42
    private $constraintViolationList;
43
44
    protected function setUp()
45
    {
46
        $this->emailValidator = $this->getMockBuilder('Symfony\Component\Validator\Validator\RecursiveValidator')->disableOriginalConstructor()->getMock();
47
        $this->constraintViolationList = $this->getMockBuilder('Symfony\Component\Validator\ConstraintViolationList')->disableOriginalConstructor()->getMock();
48
49
        $this->provider = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')->getMock();
50
        $this->user = $this->getMockBuilder('FOS\UserBundle\Model\User')
51
            ->disableOriginalConstructor()
52
            ->getMock();
53
        $this->router = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Routing\Router')
54
            ->disableOriginalConstructor()
55
            ->getMock();
56
57
        $this->tokenGenerator = $this->getMockBuilder('FOS\UserBundle\Util\TokenGenerator')->disableOriginalConstructor()->getMock();
58
        $this->mailer = $this->getMockBuilder('Azine\EmailUpdateConfirmationBundle\Mailer\AzineEmailUpdateConfirmationMailer')->disableOriginalConstructor()->getMock();
59
        $this->eventDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
60
61
        $this->emailUpdateConfirmation = new EmailUpdateConfirmation($this->router, $this->tokenGenerator, $this->mailer, $this->eventDispatcher, $this->emailValidator, $this->redirectRoute, $this->cypher_method);
62
63
        $this->user->expects($this->any())
64
            ->method('getConfirmationToken')
65
            ->will($this->returnValue($this->token));
66
    }
67
68
    public function testFetchEncryptedEmailFromConfirmationLinkMethod()
69
    {
70
        $this->emailValidator->expects($this->once())->method('validate')->will($this->returnValue($this->constraintViolationList));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
        $encryptedEmail = $this->emailUpdateConfirmation->encryptEmailValue($this->token, $this->emailTest);
72
73
        $email = $this->emailUpdateConfirmation->fetchEncryptedEmailFromConfirmationLink($this->user, $encryptedEmail);
74
        $this->assertSame($this->emailTest, $email);
75
    }
76
77
    public function testEncryptDecryptEmail()
78
    {
79
        $this->emailValidator->expects($this->once())->method('validate')->will($this->returnValue($this->constraintViolationList));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
        $encryptedEmail = $this->emailUpdateConfirmation->encryptEmailValue($this->user->getConfirmationToken(), $this->emailTest);
81
        $this->assertSame($this->emailTest, $this->emailUpdateConfirmation->decryptEmailValue($this->user->getConfirmationToken(), $encryptedEmail));
82
    }
83
84
    /**
85
     * @expectedException \InvalidArgumentException
86
     */
87
    public function testDecryptFromWrongEmailFormat()
88
    {
89
        $this->emailValidator->expects($this->once())->method('validate')->will($this->returnValue($this->constraintViolationList));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
91
        $this->constraintViolationList->expects($this->once())->method('count')->will($this->returnValue(1));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...onstraintViolationList>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
        $wrongEmail = 'fooexample.com';
93
94
        $encryptedEmail = $this->emailUpdateConfirmation->encryptEmailValue($this->user->getConfirmationToken(), $wrongEmail);
95
        $this->emailUpdateConfirmation->decryptEmailValue($this->user->getConfirmationToken(), $encryptedEmail);
96
    }
97
98
    /**
99
     * @expectedException \InvalidArgumentException
100
     */
101
    public function testIntegerIsSetInsteadOfEmailString()
102
    {
103
        $this->emailUpdateConfirmation->encryptEmailValue($this->user->getConfirmationToken(), 123);
104
    }
105
106
    /**
107
     * @expectedException \InvalidArgumentException
108
     */
109
    public function testIntegerIsSetInsteadOfConfirmationTokenStringForEncryption()
110
    {
111
        $this->emailUpdateConfirmation->encryptEmailValue(123, $this->emailTest);
112
    }
113
114
    /**
115
     * @expectedException \InvalidArgumentException
116
     */
117
    public function testIntegerIsSetInsteadOfConfirmationTokenStringForDecryption()
118
    {
119
        $this->emailUpdateConfirmation->decryptEmailValue(123, $this->emailTest);
120
    }
121
}
122