|
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); |
|
|
|
|
|
|
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')); |
|
|
|
|
|
|
40
|
|
|
return $response; |
|
41
|
|
|
} |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$response = $this->object->dispatchCommand($command)->getReturn(); |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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
|
|
|
|