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

testAssertInvalidAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Nord\Lumen\OAuth2\Tests;
4
5
use Nord\Lumen\OAuth2\OAuth2ServiceProvider;
6
use Nord\Lumen\OAuth2\Middleware\OAuth2Middleware;
7
8
class OAuth2MiddlewareTest extends \Codeception\TestCase\Test
9
{
10
    use \Codeception\Specify;
11
12
    /**
13
     * @var \UnitTester
14
     */
15
    protected $tester;
16
17
    /**
18
     * @var MockApplication
19
     */
20
    protected $app;
21
22
    /**
23
     * @inheritdoc
24
     */
25
    protected function setup()
26
    {
27
        $this->app = new MockApplication();
28
        $this->app->register(MockStorageServiceProvider::class);
29
        $this->app->register(OAuth2ServiceProvider::class);
30
    }
31
32
    /**
33
     *
34
     */
35
    public function testAssertValidAccessToken()
0 ignored issues
show
Coding Style introduced by
testAssertValidAccessToken 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...
36
    {
37
        $this->specify('verify middleware valid access token', function () {
38
            $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer mF_9.B5f-4.1JqM';
39
            $middleware = new OAuth2Middleware();
40
            verify($middleware->handle($this->createRequest(), function () {
41
                return true;
42
            }))->equals(true);
43
        });
44
    }
45
46
    /**
47
     *
48
     */
49
    public function testAssertInvalidAccessToken()
50
    {
51
        $this->specify('verify middleware invalid access token', function () {
52
            $middleware = new OAuth2Middleware();
53
            $res = $middleware->handle($this->createRequest(), function () {
54
                return true;
55
            });
56
            verify($res)->isInstanceOf(\Illuminate\Http\JsonResponse::class);
57
            verify((array)$res->getData())->equals(['message' => 'ERROR.ACCESS_DENIED']);
58
        });
59
    }
60
61
    /**
62
     * @return \Illuminate\Http\Request
63
     */
64
    private function createRequest()
65
    {
66
        $req = $this->getMockBuilder(\Illuminate\Http\Request::class)
67
            ->disableOriginalConstructor()
68
            ->getMock();
69
70
        return $req;
71
    }
72
}
73