Completed
Pull Request — master (#2)
by Eugene
02:20
created

testEncryptDecryptEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
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\EmailEncryption;
6
use Azine\EmailUpdateConfirmationBundle\Services\EmailUpdateConfirmation;
7
use FOS\UserBundle\Mailer\MailerInterface;
8
use FOS\UserBundle\Model\User;
9
use FOS\UserBundle\Util\TokenGenerator;
10
use Symfony\Component\EventDispatcher\EventDispatcher;
11
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
12
use Symfony\Component\Routing\RouterInterface;
13
use Symfony\Component\Validator\ConstraintViolationList;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
class EmailUpdateConfirmationTest extends \PHPUnit_Framework_TestCase
17
{
18
    /** @var ExpressionFunctionProviderInterface */
19
    private $provider;
20
    /** @var RouterInterface */
21
    private $router;
22
    /** @var TokenGenerator */
23
    private $tokenGenerator;
24
    /** @var MailerInterface */
25
    private $mailer;
26
    /** @var EmailEncryption */
27
    private $emailEncryption;
28
    /** @var EventDispatcher */
29
    private $eventDispatcher;
30
    /** @var string */
31
    private $redirectRoute = 'redirect_route';
32
    /** @var string */
33
    private $token = 'test_token';
34
    /** @var EmailUpdateConfirmation */
35
    private $emailUpdateConfirmation;
36
    /** @var string */
37
    private $emailTest = '[email protected]';
38
    /** @var User */
39
    private $user;
40
    private $cypher_method = 'AES-128-CBC';
41
42
    /** @var ValidatorInterface */
43
    private $emailValidator;
44
    /** @var ConstraintViolationList */
45
    private $constraintViolationList;
46
47
    protected function setUp()
48
    {
49
        $this->emailValidator = $this->getMockBuilder('Symfony\Component\Validator\Validator\RecursiveValidator')->disableOriginalConstructor()->getMock();
50
        $this->constraintViolationList = new ConstraintViolationList(array());
51
        $this->emailValidator->expects($this->once())->method('validate')->will($this->returnValue($this->constraintViolationList));
52
53
        $this->provider = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')->getMock();
54
        $this->user = $this->getMockBuilder('FOS\UserBundle\Model\User')
55
            ->disableOriginalConstructor()
56
            ->getMock();
57
        $this->router = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Routing\Router')
58
            ->disableOriginalConstructor()
59
            ->getMock();
60
61
        $this->tokenGenerator = $this->getMockBuilder('FOS\UserBundle\Util\TokenGenerator')->disableOriginalConstructor()->getMock();
62
        $this->mailer = $this->getMockBuilder('Azine\EmailUpdateConfirmationBundle\Mailer\AzineUpdateEmailMailer')->disableOriginalConstructor()->getMock();
63
        $this->emailEncryption = new EmailEncryption($this->emailValidator, $this->cypher_method);
64
        $this->eventDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
65
66
        $this->emailUpdateConfirmation = new EmailUpdateConfirmation($this->router, $this->tokenGenerator, $this->mailer, $this->eventDispatcher, $this->emailValidator, $this->redirectRoute, $this->cypher_method);
67
68
        $this->user->expects($this->any())
69
            ->method('getConfirmationToken')
70
            ->will($this->returnValue($this->token));
71
    }
72
73
    public function testFetchEncryptedEmailFromConfirmationLinkMethod()
74
    {
75
        $this->emailTest = '[email protected]';
76
        $encryptedEmail = $this->emailUpdateConfirmation->encryptEmailValue($this->token, $this->emailTest);
77
78
        $email = $this->emailUpdateConfirmation->fetchEncryptedEmailFromConfirmationLink($this->user, $encryptedEmail);
79
        $this->assertSame($this->emailTest, $email);
80
    }
81
82
    public function testEncryptDecryptEmail()
83
    {
84
        $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...
85
        $this->constraintViolationList->remove(0);
86
        $encryptedEmail = $this->emailUpdateConfirmation->encryptEmailValue($this->user->getConfirmationToken(), $this->emailTest);
87
        $this->assertSame($this->emailTest, $this->emailUpdateConfirmation->decryptEmailValue($this->user->getConfirmationToken(), $encryptedEmail));
88
    }
89
90
    /**
91
     * @expectedException \InvalidArgumentException
92
     */
93
    public function testDecryptFromWrongEmailFormat()
94
    {
95
        $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...
96
        $wrongEmail ='fooexample.com';
97
98
        $encryptedEmail = $this->emailUpdateConfirmation->encryptEmailValue($this->user->getConfirmationToken(), $wrongEmail);
99
        $this->emailUpdateConfirmation->decryptEmailValue($this->user->getConfirmationToken(), $encryptedEmail);
100
    }
101
}
102