DispatcherTest::testEmptyCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Nip\Dispatcher\Tests;
5
6
use Nip\Dispatcher\Commands\Command;
7
use Nip\Dispatcher\Dispatcher;
8
use Nip\Dispatcher\Exceptions\InvalidCommandException;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Class DispatcherTest
14
 * @package Nip\Dispatcher\Tests
15
 */
16
class DispatcherTest extends AbstractTest
17
{
18
    /**
19
     * @var Dispatcher
20
     */
21
    protected $object;
22
23
24
    public function testEmptyCommand()
25
    {
26
        $command = new Command();
27
28
        self::expectException(InvalidCommandException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

28
        self::/** @scrutinizer ignore-call */ 
29
              expectException(InvalidCommandException::class);
Loading history...
29
        $this->object->dispatchCommand($command);
30
    }
31
32
    public function testClosure()
33
    {
34
        $command = new Command();
35
        $command->setAutoInitRequest(true);
36
        $command->getRequest(true)->query->set('variable', 'value');
37
        $command->setAction(
38
            function (RequestInterface $request, ResponseInterface $response) {
39
                $response->setContent($request->query->get('variable'));
0 ignored issues
show
Bug introduced by
Accessing query on the interface Psr\Http\Message\RequestInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
The method setContent() does not exist on Psr\Http\Message\ResponseInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Nyholm\Psr7\Response. Are you sure you never get one of those? ( Ignorable by Annotation )

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

39
                $response->/** @scrutinizer ignore-call */ 
40
                           setContent($request->query->get('variable'));
Loading history...
40
                return $response;
41
            }
42
        );
43
44
        $response = $this->object->dispatchCommand($command)->getReturn();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $response is correct as $this->object->dispatchC...($command)->getReturn() targeting Nip\Dispatcher\Commands\Command::getReturn() 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...
45
        self::assertInstanceOf(ResponseInterface::class, $response);
46
        self::assertSame('value', $response->getContent());
47
    }
48
49
    public function testRequestModuleController()
50
    {
51
        $command = new Command();
52
        $command->setAutoInitRequest(true);
53
        $command->getRequest(true)
54
            ->setModuleName('frontend')
55
            ->setControllerName('baseTrait');
56
57
        $response = $this->object->dispatchCommand($command)->getReturn();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $response is correct as $this->object->dispatchC...($command)->getReturn() targeting Nip\Dispatcher\Commands\Command::getReturn() 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
        self::assertInstanceOf(ResponseInterface::class, $response);
59
        self::assertEquals('index response', $response->getContent());
60
    }
61
62
    protected function setUp(): void
63
    {
64
        parent::setUp();
65
        $this->object = new Dispatcher();
66
    }
67
}
68