Completed
Pull Request — master (#140)
by Zhukov
02:07
created

AuthMiddlewareTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\Auth;
4
5
use Nyholm\Psr7\Factory\Psr17Factory;
6
use Nyholm\Psr7\ServerRequest;
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Yiisoft\Yii\Web\Auth\AuthInterface;
12
use Yiisoft\Yii\Web\Auth\AuthMiddleware;
13
use PHPUnit\Framework\TestCase;
14
use Yiisoft\Yii\Web\User\IdentityInterface;
15
16
class AuthMiddlewareTest extends TestCase
17
{
18
    /** @var ResponseFactoryInterface */
19
    private $responseFactory;
20
21
    /** @var AuthInterface */
22
    private $authenticator;
23
24
    protected function setUp()
25
    {
26
        $this->responseFactory = new Psr17Factory();
27
        $this->authenticator = $this->createMock(AuthInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Yiisof...h\AuthInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Yiisoft\Yii\Web\Auth\AuthInterface of property $authenticator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function shouldAuthenticateAndSetAttribute()
34
    {
35
        $request = new ServerRequest('GET', '/');
36
        $identity = $this->createMock(IdentityInterface::class);
37
        $identityAttribute = 'identity';
38
39
        $this->authenticator
40
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Yiisoft\Yii\Web\Auth\AuthInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            ->/** @scrutinizer ignore-call */ 
41
              expects($this->once())

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...
41
            ->method('authenticate')
42
            ->willReturn($identity);
43
44
        $handler = $this->createMock(RequestHandlerInterface::class);
45
        $handler
46
            ->expects($this->once())
47
            ->method('handle')
48
            ->willReturnCallback(
49
                function (ServerRequestInterface $request) use ($identityAttribute, $identity) {
50
                    $this->assertEquals($identity, $request->getAttribute($identityAttribute));
51
52
                    return $this->responseFactory->createResponse();
53
                }
54
            );
55
56
        $auth = new AuthMiddleware($this->responseFactory, $this->authenticator);
57
        $auth->setRequestName($identityAttribute);
58
        $auth->process($request, $handler);
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function shouldAuthenticateOptionalPath()
65
    {
66
        $path = '/optional';
67
        $request = new ServerRequest('GET', $path);
68
69
        $this->authenticator
70
            ->expects($this->once())
71
            ->method('authenticate')
72
            ->willReturn(null);
73
74
        $handler = $this->createMock(RequestHandlerInterface::class);
75
        $handler
76
            ->expects($this->once())
77
            ->method('handle');
78
79
        $auth = new AuthMiddleware($this->responseFactory, $this->authenticator);
80
        $auth->setOptional([$path]);
81
        $auth->process($request, $handler);
82
    }
83
84
    /**
85
     * @test
86
     */
87
    public function shouldNotAuthenticate()
88
    {
89
        $request = new ServerRequest('GET', '/');
90
        $header = 'Authenticated';
91
        $headerValue = 'false';
92
93
        $this->authenticator
94
            ->expects($this->once())
95
            ->method('authenticate')
96
            ->willReturn(null);
97
98
        $this->authenticator
99
            ->expects($this->once())
100
            ->method('challenge')
101
            ->willReturnCallback(
102
                function (ResponseInterface $response) use ($header, $headerValue) {
103
                    return $response->withHeader($header, $headerValue);
104
                }
105
            );
106
107
        $handler = $this->createMock(RequestHandlerInterface::class);
108
        $handler
109
            ->expects($this->never())
110
            ->method('handle');
111
112
        $auth = new AuthMiddleware($this->responseFactory, $this->authenticator);
113
        $response = $auth->process($request, $handler);
114
        $this->assertEquals(401, $response->getStatusCode());
115
        $this->assertEquals($headerValue, $response->getHeaderLine($header));
116
    }
117
}
118