Completed
Branch merging-leagues-tournaments (46817d)
by Benedikt
01:48
created

AuthenticateTest::testConstruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Created by PhpStorm.
6
 * User: benedikt
7
 * Date: 9/17/17
8
 * Time: 12:33 AM
9
 */
10
11
namespace Tfboe\FmLib\Tests\Unit\Http\Middleware;
12
13
use Illuminate\Contracts\Auth\Factory;
14
use Illuminate\Http\Request;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use ReflectionException;
17
use Tfboe\FmLib\Entity\UserInterface;
18
use Tfboe\FmLib\Exceptions\AuthenticationException;
19
use Tfboe\FmLib\Http\Middleware\Authenticate;
20
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
21
use Tymon\JWTAuth\Contracts\JWTSubject;
22
use Tymon\JWTAuth\JWTGuard;
23
use Tymon\JWTAuth\Payload;
24
25
/**
26
 * Class BaseControllerTest
27
 * @package Tests\Unit\App\Http\Controllers
28
 */
29
class AuthenticateTest extends UnitTestCase
30
{
31
  //tests also private method disable this tests as soon as all are used in public interfaces
32
//<editor-fold desc="Public Methods">
33
34
  /**
35
   * @covers \Tfboe\FmLib\Http\Middleware\Authenticate::__construct
36
   * @throws ReflectionException
37
   */
38
  public function testConstruct()
39
  {
40
    $authenticate = $this->authenticate();
41
    self::assertInstanceOf(Authenticate::class, $authenticate);
42
  }
43
44
  /**
45
   * @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle
46
   * @throws AuthenticationException
47
   * @throws ReflectionException
48
   * @uses   \Tfboe\FmLib\Exceptions\AuthenticationException::__construct
49
   * @uses   \Tfboe\FmLib\Http\Middleware\Authenticate::__construct
50
   */
51
  public function testHandleGuest()
52
  {
53
    $this->expectException(AuthenticationException::class);
54
    $this->expectExceptionMessage('Not logged in!');
55
56
    $auth = $this->getAuth(true, true, UserInterface::class, 6);
57
    $authenticate = $this->authenticate($auth);
0 ignored issues
show
Bug introduced by
It seems like $auth defined by $this->getAuth(true, tru...serInterface::class, 6) on line 56 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tfboe\FmLib\Tests\Unit\H...ateTest::authenticate() does only seem to accept object<Illuminate\Contracts\Auth\Factory>|null, 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
    $authenticate->handle(null, function () {
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Illuminate\Http\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method handle does only exist in Tfboe\FmLib\Http\Middleware\Authenticate, but not in PHPUnit\Framework\MockObject\MockObject.

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...
59
    }, "guardName");
60
  }
61
62
  /**
63
   * @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle
64
   * @throws AuthenticationException
65
   * @throws ReflectionException
66
   * @uses   \Tfboe\FmLib\Exceptions\AuthenticationException::__construct
67
   * @uses   \Tfboe\FmLib\Http\Middleware\Authenticate::__construct
68
   */
69
  public function testHandleNoVer()
70
  {
71
    $this->expectException(AuthenticationException::class);
72
    $this->expectExceptionMessage('Payload version expired!');
73
74
    $auth = $this->getAuth(false, false, UserInterface::class, 6);
75
    $authenticate = $this->authenticate($auth);
0 ignored issues
show
Bug introduced by
It seems like $auth defined by $this->getAuth(false, fa...serInterface::class, 6) on line 74 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tfboe\FmLib\Tests\Unit\H...ateTest::authenticate() does only seem to accept object<Illuminate\Contracts\Auth\Factory>|null, 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...
76
    $authenticate->handle(null, function () {
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Illuminate\Http\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method handle does only exist in Tfboe\FmLib\Http\Middleware\Authenticate, but not in PHPUnit\Framework\MockObject\MockObject.

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...
77
    }, "guardName");
78
  }
79
80
  /**
81
   * @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle
82
   * @throws AuthenticationException
83
   * @throws ReflectionException
84
   * @uses   \Tfboe\FmLib\Exceptions\AuthenticationException::__construct
85
   * @uses   \Tfboe\FmLib\Http\Middleware\Authenticate::__construct
86
   */
87
  public function testHandleOldVersion()
88
  {
89
    $this->expectException(AuthenticationException::class);
90
    $this->expectExceptionMessage('Payload version expired!');
91
92
    $auth = $this->getAuth(false, true, UserInterface::class, 8);
93
    $authenticate = $this->authenticate($auth);
0 ignored issues
show
Bug introduced by
It seems like $auth defined by $this->getAuth(false, tr...serInterface::class, 8) on line 92 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tfboe\FmLib\Tests\Unit\H...ateTest::authenticate() does only seem to accept object<Illuminate\Contracts\Auth\Factory>|null, 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...
94
    $authenticate->handle(null, function () {
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Illuminate\Http\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method handle does only exist in Tfboe\FmLib\Http\Middleware\Authenticate, but not in PHPUnit\Framework\MockObject\MockObject.

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...
95
    }, "guardName");
96
  }
97
98
  /**
99
   * @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle
100
   * @throws AuthenticationException
101
   * @throws ReflectionException
102
   * @uses   \Tfboe\FmLib\Http\Middleware\Authenticate::__construct
103
   */
104
  public function testHandleSuccess()
105
  {
106
    $auth = $this->getAuth(false, true, UserInterface::class, 6);
107
    /** @var Request $request */
108
    $request = $this->createMock(Request::class);
109
110
    $called = false;
111
    $next = function ($req) use (&$called, $request) {
112
      self::assertTrue($request === $req);
113
      self::assertFalse($called);
114
      $called = true;
115
    };
116
117
    $authenticate = $this->authenticate($auth);
0 ignored issues
show
Bug introduced by
It seems like $auth defined by $this->getAuth(false, tr...serInterface::class, 6) on line 106 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tfboe\FmLib\Tests\Unit\H...ateTest::authenticate() does only seem to accept object<Illuminate\Contracts\Auth\Factory>|null, 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...
118
    self::assertFalse($called);
119
    $authenticate->handle($request, $next, "guardName");
0 ignored issues
show
Bug introduced by
The method handle does only exist in Tfboe\FmLib\Http\Middleware\Authenticate, but not in PHPUnit\Framework\MockObject\MockObject.

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...
120
    self::assertTrue($called);
121
  }
122
123
  /**
124
   * @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle
125
   * @throws AuthenticationException
126
   * @throws ReflectionException
127
   * @uses   \Tfboe\FmLib\Exceptions\AuthenticationException::__construct
128
   * @uses   \Tfboe\FmLib\Http\Middleware\Authenticate::__construct
129
   */
130
  public function testHandleWrongUser()
131
  {
132
    $this->expectException(AuthenticationException::class);
133
    $this->expectExceptionMessage('Payload version expired!');
134
135
    $auth = $this->getAuth(false, true, JWTSubject::class, 6);
136
    $authenticate = $this->authenticate($auth);
0 ignored issues
show
Bug introduced by
It seems like $auth defined by $this->getAuth(false, tr...s\JWTSubject::class, 6) on line 135 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tfboe\FmLib\Tests\Unit\H...ateTest::authenticate() does only seem to accept object<Illuminate\Contracts\Auth\Factory>|null, 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...
137
    $authenticate->handle(null, function () {
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Illuminate\Http\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method handle does only exist in Tfboe\FmLib\Http\Middleware\Authenticate, but not in PHPUnit\Framework\MockObject\MockObject.

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...
138
    }, "guardName");
139
  }
140
//</editor-fold desc="Public Methods">
141
142
//<editor-fold desc="Private Methods">
143
  /**
144
   * @param Factory|null $auth
145
   * @param array $stubbedMethods
146
   * @return Authenticate|MockObject
147
   * @throws ReflectionException
148
   */
149
  private function authenticate(?Factory $auth = null,
150
                                array $stubbedMethods = []): MockObject
151
  {
152
    if ($auth === null) {
153
      $auth = $this->createMock(Factory::class);
154
    }
155
    return $this->getMockForAbstractClass(Authenticate::class, [$auth], '', true,
156
      true, true, $stubbedMethods);
157
  }
158
159
  /**
160
   * @param bool $guest
161
   * @param bool $hasVer
162
   * @param string $userClass
163
   * @param int $userConfirmedVersion
164
   * @return MockObject|Factory
165
   */
166
  private function getAuth(bool $guest, bool $hasVer, string $userClass, int $userConfirmedVersion): MockObject
167
  {
168
    $guard = $this->createMock(JWTGuard::class);
169
    $auth = $this->createMock(Factory::class);
170
    $payload = $this->createMock(Payload::class);
171
    $user = $this->createMock($userClass);
172
173
174
    $auth->expects(self::once())->method("guard")->with("guardName")->willReturn($guard);
175
176
    $guard->method('guest')->willReturn($guest);
177
    $guard->method('getPayload')->willReturn($payload);
178
    $guard->method('getUser')->willReturn($user);
179
180
    $payload->method('hasKey')->with('ver')->willReturn($hasVer);
181
    $payload->method('get')->with(['ver'])->willReturn([7]);
182
183
    if ($userClass === UserInterface::class) {
184
      $user->method('getJwtVersion')->willReturn($userConfirmedVersion);
185
    }
186
187
    return $auth;
188
  }
189
//</editor-fold desc="Private Methods">
190
}