Passed
Push — master ( 149dd5...081af3 )
by Charles
02:27
created

AuthenticationTest::testV1EncryptedRequestAndResponse()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 73
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 73
rs 8.4032
c 0
b 0
f 0
cc 6
nc 3
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace ncryptf\Tests;
4
5
use DateTime;
6
use ncryptf\Token;
7
use ncryptf\Request;
8
use ncryptf\Response;
9
use ncryptf\Authorization;
10
use Middlewares\Utils\Factory;
11
use WildWolf\Psr16MemoryCache;
12
use ncryptf\Tests\AbstractTest;
13
use PHPUnit\Framework\TestCase;
14
15
use Middlewares\Utils\Dispatcher;
16
use ncryptf\Tests\mock\EchoResponse;
17
use ncryptf\middleware\RequestParser;
18
use ncryptf\Tests\mock\EncryptionKey;
19
use ncryptf\Tests\mock\Authentication;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Server\MiddlewareInterface;
22
use ncryptf\middleware\ResponseFormatter;
23
use Zend\Diactoros\Response\JsonResponse;
24
use Psr\Http\Message\ServerRequestInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
27
final class AuthenticationTest extends AbstractTest
28
{
29
    public function testSuccessfulLogin()
30
    {
31
        foreach ($this->testCases as $k => $params) {
32
            $auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]);
33
            $response = Dispatcher::run(
34
                [
35
                    new Authentication,
36
                    function ($request, $next) {
37
                        $this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token'));
38
                        $this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user')));
39
                        return $next->handle($request);
40
                    }
41
                ],
42
                Factory::createServerRequest($params[0], $params[1])
43
                    ->withHeader('Authorization', $auth->getHeader())
44
                    ->withHeader('Content-Type', 'application/json')
45
                    ->withHeader('Accept', 'application/json')
46
                    ->withBody((function () use ($params) {
47
                        $stream = fopen('php://memory', 'r+');
48
                        fwrite($stream, \is_array($params[2]) ? \json_encode($params[2]): $params[2]);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

48
                        fwrite(/** @scrutinizer ignore-type */ $stream, \is_array($params[2]) ? \json_encode($params[2]): $params[2]);
Loading history...
49
                        rewind($stream);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

49
                        rewind(/** @scrutinizer ignore-type */ $stream);
Loading history...
50
                        return new \Zend\Diactoros\Stream($stream);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $stream of Zend\Diactoros\Stream::__construct() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
                        return new \Zend\Diactoros\Stream(/** @scrutinizer ignore-type */ $stream);
Loading history...
51
                    })())
52
            );
53
54
            $this->assertSame(200, $response->getStatusCode());
55
        }
56
    }
57
58
    public function testError()
59
    {
60
        $auth = new Authorization('GET', '/api/v1/user/index', $this->token, new DateTime, '{"foo":"bar"}');
61
        $response = Dispatcher::run(
62
            [
63
                new Authentication,
64
                function ($request, $next) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

64
                function (/** @scrutinizer ignore-unused */ $request, $next) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $next is not used and could be removed. ( Ignorable by Annotation )

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

64
                function ($request, /** @scrutinizer ignore-unused */ $next) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
                    return new JsonResponse(['foo' => 'bar']);
66
                }
67
            ],
68
            Factory::createServerRequest('GET', '/api/v1/user/index')
69
                ->withHeader('Authorization', $auth->getHeader())
70
                ->withHeader('Content-Type', 'application/json')
71
                ->withHeader('Accept', 'application/json')
72
        );
73
74
        $this->assertSame(401, $response->getStatusCode());
75
    }
76
}
77