BodyParserTest::testBodyParser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 7
loc 7
rs 9.4285
1
<?php
2
3
namespace Tests;
4
5
use App\Middleware\Api;
6
use Zend\Diactoros\Uri;
7
use tests\mocks\Delegate;
8
use App\Middleware\Database;
9
use App\Middleware\BodyParser;
10
use PHPUnit\Framework\TestCase;
11
use Zend\Diactoros\CallbackStream;
12
use Zend\Diactoros\ServerRequestFactory;
13
use Zend\Diactoros\Response\JsonResponse;
14
15
class BodyParserTest extends TestCase
16
{
17
    /**
18
     * HTTP Api Request
19
     *
20
     * @return void
21
     */
22
    protected $request;
23
24
    /**
25
     * Initialize our api and request.
26
     *
27
     * @return void
28
     */
29
    public function setUp(): void
30
    {
31
        require_once __DIR__ . "/../app/helpers.php";        
32
        $request = ServerRequestFactory::fromGlobals(
33
            $server = [
34
                'REQUEST_METHOD' => 'GET',
35
                'REQUEST_URI' => '/redirects'
36
            ],
37
            $query = [],
38
            $body = [],
39
            $cookies = [],
40
            $files = []
41
        );
42
        $this->request = $request->withBody(new CallbackStream(function () {
43
            return json_encode(['hello' => 'world']);
44
        }));
45
    }
46
47
    /**
48
     * Test the middleware delegate.
49
     *
50
     * @return void
51
     */
52 View Code Duplication
    public function testBodyParser(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $delegate = new Delegate();
55
        $parser = new BodyParser();
56
        $parser->process($this->request, $delegate);
57
        $parsed = $delegate->getRequest();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $parsed is correct as $delegate->getRequest() targeting tests\mocks\Delegate::getRequest() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
        $this->assertEquals(['hello' => 'world'], $parsed->getAttribute('body'));
59
    }
60
}
61