Completed
Push — develop ( b0c1cf...96d23e )
by Christoffer
05:18
created

OAuth2MiddlewareTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 61
c 0
b 0
f 0
wmc 4
lcom 2
cbo 6
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 6 1
A testAssertValidAccessToken() 0 8 1
A testAssertInvalidAccessToken() 0 9 1
A createRequest() 0 8 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require_once __DIR__ . '/../_support/Mock/MockStorageServiceProvider.php';
4
5
use Nord\Lumen\OAuth2\OAuth2ServiceProvider;
6
use Nord\Lumen\OAuth2\Middleware\OAuth2Middleware;
7
8
class OAuth2MiddlewareTest 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...
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 () { return true; }))->equals(true);
41
       });
42
    }
43
44
    /**
45
     *
46
     */
47
    public function testAssertInvalidAccessToken()
48
    {
49
        $this->specify('verify middleware invalid access token', function () {
50
            $middleware = new OAuth2Middleware();
51
            $res = $middleware->handle($this->createRequest(), function () { return true; });
52
            verify($res)->isInstanceOf(Illuminate\Http\JsonResponse::class);
53
            verify((array)$res->getData())->equals(['message' => 'ERROR.ACCESS_DENIED']);
54
        });
55
    }
56
57
    /**
58
     * @return \Illuminate\Http\Request
59
     */
60
    private function createRequest()
61
    {
62
        $req = $this->getMockBuilder(\Illuminate\Http\Request::class)
63
            ->disableOriginalConstructor()
64
            ->getMock();
65
66
        return $req;
67
    }
68
}
69