Completed
Push — master ( 62e173...8ef644 )
by Christoffer
02:02
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
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
use Nord\Lumen\OAuth2\OAuth2Service;
4
5
class OAuth2ServiceTest extends \Codeception\TestCase\Test
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    use Codeception\Specify;
8
9
    /**
10
     * @var \UnitTester
11
     */
12
    protected $tester;
13
14
    /**
15
     * @var array
16
     */
17
    protected static $token = [
18
        'access_token'  => 'mF_9.B5f-4.1JqM',
19
        'token_type'    => 'Bearer',
20
        'expires_in'    => 3600,
21
        'refresh_token' => 'tGzv3JOkF0XG5Qx2TlKWIA'
22
    ];
23
24
    /**
25
     * @inheritdoc
26
     */
27
    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...
28
    {
29
        $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer mF_9.B5f-4.1JqM';
30
    }
31
32
    /**
33
     *
34
     */
35
    public function testAssertIssueAccessToken()
36
    {
37
        $this->specify('verify service issueAccessToken', function () {
38
            $authorizationServer = $this->createAuthorizationServer();
39
            $authorizationServer->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<League\OAuth2\Server\AuthorizationServer>.

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...
40
               ->method('issueAccessToken')
41
               ->will($this->returnValue(self::$token));
42
43
            $service = new OAuth2Service($authorizationServer, $this->createResourceServer());
44
            verify($service->issueAccessToken())->equals(self::$token);
45
        });
46
    }
47
48
    /**
49
     *
50
     */
51 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...
52
    {
53
        $this->specify('verify service validateAccessToken', function () {
54
            $service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer());
55
            verify($service->validateAccessToken(true, self::$token['access_token']))->true();
56
        });
57
    }
58
59
    /**
60
     *
61
     */
62 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...
63
    {
64
        $this->specify('verify service can getResourceOwnerId', function () {
65
            $service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer());
66
            $service->validateAccessToken(true, self::$token['access_token']);
67
            verify($service->getResourceOwnerId())->equals('test');
68
        });
69
    }
70
71
    /**
72
     *
73
     */
74 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...
75
    {
76
        $this->specify('verify service can getResourceOwnerType', function () {
77
            $service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer());
78
            $service->validateAccessToken(true, self::$token['access_token']);
79
            verify($service->getResourceOwnerType())->equals('test');
80
        });
81
    }
82
83
    /**
84
     *
85
     */
86 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...
87
    {
88
        $this->specify('verify service can getClientId', function () {
89
            $service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer());
90
            $service->validateAccessToken(true, self::$token['access_token']);
91
            verify($service->getClientId())->equals('test');
92
        });
93
    }
94
95
    /**
96
     * @return League\OAuth2\Server\AuthorizationServer
97
     */
98
    private function createAuthorizationServer()
99
    {
100
        $authorizationServer = $this->getMockBuilder(League\OAuth2\Server\AuthorizationServer::class)
101
            ->disableOriginalConstructor()
102
            ->getMock();
103
104
        return $authorizationServer;
105
    }
106
107
    /**
108
     * @return League\OAuth2\Server\ResourceServer
109
     */
110
    private function createResourceServer()
111
    {
112
        return new League\OAuth2\Server\ResourceServer(
113
            new MockSessionStorage(),
114
            new MockAccessTokenStorage(),
115
            new MockClientStorage(),
116
            new MockScopeStorage()
117
        );
118
    }
119
}
120