Completed
Push — master ( 22f213...24f6ce )
by Guilherme
17:25
created

testGenerateBackupCodes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Tests\Security;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\BackupCode;
15
use LoginCidadao\CoreBundle\Model\PersonInterface;
16
use LoginCidadao\CoreBundle\Security\TwoFactorAuthenticationService;
17
use Google\Authenticator\GoogleAuthenticator as Google;
18
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator;
19
20
class TwoFactorAuthenticationServiceTest extends \PHPUnit_Framework_TestCase
21
{
22
    public function testEnable()
23
    {
24
        $person = $this->getPerson();
25
26
        $em = $this->getEntityManager();
27
        $em->expects($this->exactly(11))->method('persist'); // 11 times: 10 BackupCodes and 1 PersonInterface
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
28
        $em->expects($this->once())->method('flush');
29
30
        $google = new Google();
31
        $twoFactor = new TwoFactorAuthenticationService($em, $this->getGoogleAuthenticator($google));
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 26 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...nService::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
32
33
        $secret = $twoFactor->generateSecret();
34
        $person->expects($this->once())->method('getGoogleAuthenticatorSecret')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
35
            ->willReturn($secret);
36
37
        $twoFactor->enable($person, $google->getCode($secret));
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 24 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...cationService::enable() does only seem to accept object<LoginCidadao\Core...\Model\PersonInterface>, 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...
38
    }
39
40
    public function testEnableWrongCode()
41
    {
42
        $this->setExpectedException('\InvalidArgumentException');
43
44
        /** @var \PHPUnit_Framework_MockObject_MockObject|PersonInterface $person */
45
        $person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
46
47
        $twoFactor = new TwoFactorAuthenticationService($this->getEntityManager(), $this->getGoogleAuthenticator());
0 ignored issues
show
Bug introduced by
It seems like $this->getEntityManager() targeting LoginCidadao\CoreBundle\...est::getEntityManager() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...nService::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
48
        $person->expects($this->once())->method('getGoogleAuthenticatorSecret')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
49
            ->willReturn($twoFactor->generateSecret());
50
        $twoFactor->enable($person, 'WRONG');
51
    }
52
53
    public function testDisable()
54
    {
55
        $person = $this->getPerson();
56
        $person->expects($this->once())->method('setGoogleAuthenticatorSecret')->with(null);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
57
        $person->expects($this->once())->method('getBackupCodes')->willReturn($this->getBackupCodes($person));
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 55 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...eTest::getBackupCodes() does only seem to accept object<LoginCidadao\Core...\Model\PersonInterface>, 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...
58
59
        $em = $this->getEntityManager();
60
        $em->expects($this->exactly(10))->method('remove')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
61
            ->with($this->isInstanceOf('LoginCidadao\CoreBundle\Entity\BackupCode'));
62
        $em->expects($this->once())->method('persist')->with($person);
63
        $em->expects($this->once())->method('flush');
64
65
        $twoFactor = new TwoFactorAuthenticationService($em, $this->getGoogleAuthenticator());
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 59 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...nService::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
66
        $this->assertTrue($twoFactor->disable($person));
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 55 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...ationService::disable() does only seem to accept object<LoginCidadao\Core...\Model\PersonInterface>, 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...
67
    }
68
69
    public function testGenerateBackupCodes()
70
    {
71
        $person = $this->getPerson();
72
73
        $em = $this->getEntityManager();
74
        $em->expects($this->exactly(10))->method('persist')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
75
            ->with($this->isInstanceOf('LoginCidadao\CoreBundle\Entity\BackupCode'));
76
77
        $twoFactor = new TwoFactorAuthenticationService($em, $this->getGoogleAuthenticator());
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 73 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...nService::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
78
        $backupCodes = $twoFactor->generateBackupCodes($person);
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 71 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...::generateBackupCodes() does only seem to accept object<LoginCidadao\Core...\Model\PersonInterface>, 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...
79
80
        foreach ($backupCodes as $backupCode) {
81
            $this->assertEquals($person, $backupCode->getPerson());
82
            $this->assertNotNull($backupCode->getCode());
83
        }
84
    }
85
86
    public function testRemoveBackupCodes()
87
    {
88
        $person = $this->getPerson();
89
90
        $person->expects($this->once())->method('getBackupCodes')->willReturn($this->getBackupCodes($person));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
Bug introduced by
It seems like $person defined by $this->getPerson() on line 88 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...eTest::getBackupCodes() does only seem to accept object<LoginCidadao\Core...\Model\PersonInterface>, 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...
91
92
        $em = $this->getEntityManager();
93
        $em->expects($this->exactly(10))->method('remove')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
94
            ->with($this->isInstanceOf('LoginCidadao\CoreBundle\Entity\BackupCode'));
95
96
        $twoFactor = new TwoFactorAuthenticationService($em, $this->getGoogleAuthenticator());
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 92 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...nService::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
97
        $twoFactor->removeBackupCodes($person);
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 88 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...ce::removeBackupCodes() does only seem to accept object<LoginCidadao\Core...\Model\PersonInterface>, 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...
98
    }
99
100
    public function testGetSecretUrl()
101
    {
102
        $url = 'https://secret.url';
103
        $person = $this->getPerson();
104
105
        $googleAuthClass = 'Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator';
106
        $googleAuth = $this->getMockBuilder($googleAuthClass)->disableOriginalConstructor()->getMock();
107
        $googleAuth->expects($this->once())->method('getUrl')->with($person)->willReturn($url);
108
109
        $twoFactor = new TwoFactorAuthenticationService($this->getEntityManager(), $googleAuth);
0 ignored issues
show
Bug introduced by
It seems like $this->getEntityManager() targeting LoginCidadao\CoreBundle\...est::getEntityManager() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...nService::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
110
        $this->assertEquals($url, $twoFactor->getSecretUrl($person));
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 103 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...Service::getSecretUrl() does only seem to accept object<LoginCidadao\Core...\Model\PersonInterface>, 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...
111
    }
112
113
    /**
114
     * @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface
115
     */
116
    private function getEntityManager()
117
    {
118
        return $this->getMock('Doctrine\ORM\EntityManagerInterface');
119
    }
120
121
    private function getGoogleAuthenticator($googleAuth = null)
122
    {
123
        if (!$googleAuth) {
124
            $googleAuth = new Google();
125
        }
126
127
        return new GoogleAuthenticator($googleAuth, 'server', 'issuer');
128
    }
129
130
    /**
131
     * @return \PHPUnit_Framework_MockObject_MockObject|PersonInterface
132
     */
133
    private function getPerson()
134
    {
135
        return $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
136
    }
137
138
    /**
139
     * @param PersonInterface $person
140
     * @return BackupCode[]
141
     */
142
    private function getBackupCodes(PersonInterface $person)
143
    {
144
        $backupCodes = [];
145
        while (count($backupCodes) < 10) {
146
            $backupCodes[] = (new BackupCode())
147
                ->setPerson($person)
148
                ->setCode(random_int(1111, 9999));
149
        }
150
151
        return $backupCodes;
152
    }
153
}
154