|
1
|
|
|
<?php namespace Phprest\Router; |
|
2
|
|
|
|
|
3
|
|
|
use Phprest\Application; |
|
4
|
|
|
use Phprest\Service; |
|
5
|
|
|
use League\Container\Container; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Phprest\HttpFoundation\Response; |
|
9
|
|
|
|
|
10
|
|
|
class StrategyTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Container |
|
14
|
|
|
*/ |
|
15
|
|
|
private $container; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Strategy |
|
19
|
|
|
*/ |
|
20
|
|
|
private $strategy; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->container = new Container(); |
|
25
|
|
|
$this->strategy = new Strategy($this->container); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testDispatchWithClosure(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$result = $this->strategy->dispatch( |
|
31
|
|
|
static function () { |
|
32
|
|
|
return 42; |
|
33
|
|
|
}, |
|
34
|
|
|
[] |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertEquals(42, $result); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testDispatchWithClassAndMethod(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$result = $this->strategy->dispatch( |
|
43
|
|
|
'Phprest\Stub\Controller\Simple::getTheAnswerOfEverything', |
|
44
|
|
|
[] |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals(42, $result); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testDispatchWithClassAndMethodAndResponseObject(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->setRequestParameters('phprest-test', 1, '*/*'); |
|
53
|
|
|
|
|
54
|
|
|
$result = $this->strategy->dispatch( |
|
55
|
|
|
'Phprest\Stub\Controller\Simple::getSampleResponse', |
|
56
|
|
|
[] |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertInstanceOf(Response::class, $result); |
|
60
|
|
|
if ($result instanceof Response) { |
|
61
|
|
|
$this->assertEquals(json_encode('sample'), $result->getContent()); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $vendor |
|
67
|
|
|
* @param string|integer $apiVersion |
|
68
|
|
|
* @param string $acceptHeader |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function setRequestParameters($vendor, $apiVersion, $acceptHeader): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->container->add(Application::CONTAINER_ID_VENDOR, $vendor); |
|
73
|
|
|
$this->container->add(Application::CONTAINER_ID_API_VERSION, $apiVersion); |
|
74
|
|
|
|
|
75
|
|
|
(new Service\Hateoas\Service())-> |
|
76
|
|
|
register($this->container, new Service\Hateoas\Config(true)); |
|
77
|
|
|
|
|
78
|
|
|
$request = new Request(); |
|
79
|
|
|
$request->headers->set('Accept', $acceptHeader, true); |
|
80
|
|
|
|
|
81
|
|
|
$this->container->add(Request::class, $request); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|