Completed
Push — develop ( 6fd965...1bc1b2 )
by Christoffer
02:13
created

OAuth2ServiceTest::testAssertGetResourceOwnerId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Nord\Lumen\OAuth2\Tests;
4
5
use Nord\Lumen\OAuth2\OAuth2Service;
6
7
class OAuth2ServiceTest extends \Codeception\TestCase\Test
8
{
9
    use \Codeception\Specify;
10
11
    /**
12
     * @var \UnitTester
13
     */
14
    protected $tester;
15
16
    /**
17
     * @var array
18
     */
19
    protected static $token = [
20
        'access_token'  => 'mF_9.B5f-4.1JqM',
21
        'token_type'    => 'Bearer',
22
        'expires_in'    => 3600,
23
        'refresh_token' => 'tGzv3JOkF0XG5Qx2TlKWIA'
24
    ];
25
26
    /**
27
     * @inheritdoc
28
     */
29
    protected function setup()
0 ignored issues
show
Coding Style introduced by
setup uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
30
    {
31
        $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer mF_9.B5f-4.1JqM';
32
    }
33
34
    /**
35
     *
36
     */
37
    public function testAssertIssueAccessToken()
38
    {
39
        $this->specify('verify service issueAccessToken', function () {
40
            $authorizationServer = $this->createAuthorizationServer();
41
            $authorizationServer->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in League\OAuth2\Server\AuthorizationServer.

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...
42
               ->method('issueAccessToken')
43
               ->will($this->returnValue(self::$token));
44
45
            $service = new OAuth2Service($authorizationServer, $this->createResourceServer());
0 ignored issues
show
Bug introduced by
It seems like $authorizationServer defined by $this->createAuthorizationServer() on line 40 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Nord\Lumen\OAuth2\OAuth2Service::__construct() does only seem to accept object<League\OAuth2\Server\AuthorizationServer>, 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...
46
            verify($service->issueAccessToken())->equals(self::$token);
47
        });
48
    }
49
50
    /**
51
     *
52
     */
53 View Code Duplication
    public function testAssertValidateAccessToken()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $this->specify('verify service validateAccessToken', function () {
56
            $service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer());
0 ignored issues
show
Bug introduced by
It seems like $this->createAuthorizationServer() targeting Nord\Lumen\OAuth2\Tests\...teAuthorizationServer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Nord\Lumen\OAuth2\OAuth2Service::__construct() does only seem to accept object<League\OAuth2\Server\AuthorizationServer>, 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...
57
            verify($service->validateAccessToken(true, self::$token['access_token']))->true();
58
        });
59
    }
60
61
    /**
62
     *
63
     */
64 View Code Duplication
    public function testAssertGetResourceOwnerId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $this->specify('verify service can getResourceOwnerId', function () {
67
            $service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer());
0 ignored issues
show
Bug introduced by
It seems like $this->createAuthorizationServer() targeting Nord\Lumen\OAuth2\Tests\...teAuthorizationServer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Nord\Lumen\OAuth2\OAuth2Service::__construct() does only seem to accept object<League\OAuth2\Server\AuthorizationServer>, 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...
68
            $service->validateAccessToken(true, self::$token['access_token']);
69
            verify($service->getResourceOwnerId())->equals('test');
70
        });
71
    }
72
73
    /**
74
     *
75
     */
76 View Code Duplication
    public function testAssertGetResourceOwnerType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $this->specify('verify service can getResourceOwnerType', function () {
79
            $service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer());
0 ignored issues
show
Bug introduced by
It seems like $this->createAuthorizationServer() targeting Nord\Lumen\OAuth2\Tests\...teAuthorizationServer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Nord\Lumen\OAuth2\OAuth2Service::__construct() does only seem to accept object<League\OAuth2\Server\AuthorizationServer>, 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...
80
            $service->validateAccessToken(true, self::$token['access_token']);
81
            verify($service->getResourceOwnerType())->equals('test');
82
        });
83
    }
84
85
    /**
86
     *
87
     */
88 View Code Duplication
    public function testAssertGetClientId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        $this->specify('verify service can getClientId', function () {
91
            $service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer());
0 ignored issues
show
Bug introduced by
It seems like $this->createAuthorizationServer() targeting Nord\Lumen\OAuth2\Tests\...teAuthorizationServer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Nord\Lumen\OAuth2\OAuth2Service::__construct() does only seem to accept object<League\OAuth2\Server\AuthorizationServer>, 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...
92
            $service->validateAccessToken(true, self::$token['access_token']);
93
            verify($service->getClientId())->equals('test');
94
        });
95
    }
96
97
    /**
98
     * @return \League\OAuth2\Server\AuthorizationServer|\PHPUnit_Framework_MockObject_MockObject
99
     */
100
    private function createAuthorizationServer()
101
    {
102
        $authorizationServer = $this->getMockBuilder(\League\OAuth2\Server\AuthorizationServer::class)
103
            ->disableOriginalConstructor()
104
            ->getMock();
105
106
        return $authorizationServer;
107
    }
108
109
    /**
110
     * @return \League\OAuth2\Server\ResourceServer
111
     */
112
    private function createResourceServer()
113
    {
114
        return new \League\OAuth2\Server\ResourceServer(
115
            new MockSessionStorage(),
116
            new MockAccessTokenStorage(),
117
            new MockClientStorage(),
118
            new MockScopeStorage()
119
        );
120
    }
121
}
122