1
|
|
|
<?php namespace Phprest\Service\Hateoas; |
2
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
4
|
|
|
use Phprest\Stub\Service\SampleConfig; |
5
|
|
|
use League\Container\Container; |
6
|
|
|
use Hateoas\Hateoas; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class HateoasTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
use Getter; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var Container |
15
|
|
|
*/ |
16
|
|
|
private $container; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Config |
20
|
|
|
*/ |
21
|
|
|
private $hateoasConfig; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->container = new Container(); |
26
|
|
|
$this->hateoasConfig = new Config(true); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testInstansiation(): void |
30
|
|
|
{ |
31
|
|
|
$service = new Service(); |
32
|
|
|
$service->register($this->container, $this->hateoasConfig); |
33
|
|
|
|
34
|
|
|
$hateoasService = $this->container->get(Config::getServiceName()); |
35
|
|
|
|
36
|
|
|
$this->assertInstanceOf(Hateoas::class, $hateoasService); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testUrlGeneratorRelative(): void |
40
|
|
|
{ |
41
|
|
|
$url = call_user_func_array($this->hateoasConfig->urlGenerator, ['/foo', [], false]); |
42
|
|
|
|
43
|
|
|
$this->assertEquals('/foo', $url); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testUrlGeneratorRelativeWithParameters(): void |
47
|
|
|
{ |
48
|
|
|
$url = call_user_func_array($this->hateoasConfig->urlGenerator, ['/foo', ['a' => 1, 'b' => 2], false]); |
49
|
|
|
|
50
|
|
|
$this->assertEquals('/foo?a=1&b=2', $url); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testUrlGeneratorRelativeWithIdAndParameters(): void |
54
|
|
|
{ |
55
|
|
|
$url = call_user_func_array($this->hateoasConfig->urlGenerator, ['/foo', ['id' => 5, 'name' => 'Adam'], false]); |
56
|
|
|
|
57
|
|
|
$this->assertEquals('/foo/5?name=Adam', $url); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testUrlGeneratorAbsolute(): void |
61
|
|
|
{ |
62
|
|
|
$url = call_user_func_array($this->hateoasConfig->urlGenerator, ['/foo', ['id' => 5, 'name' => 'Adam'], true]); |
63
|
|
|
|
64
|
|
|
$this->assertEquals('http://:/foo/5?name=Adam', $url); # no host and port in CLI |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @expectedException InvalidArgumentException |
69
|
|
|
*/ |
70
|
|
|
public function testWithWrongConfig(): void |
71
|
|
|
{ |
72
|
|
|
$service = new Service(); |
73
|
|
|
$service->register($this->container, new SampleConfig()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testGetter(): void |
77
|
|
|
{ |
78
|
|
|
$service = new Service(); |
79
|
|
|
$service->register($this->container, $this->hateoasConfig); |
80
|
|
|
|
81
|
|
|
$this->assertInstanceOf(Hateoas::class, $this->serviceHateoas()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getContainer(): Container |
85
|
|
|
{ |
86
|
|
|
return $this->container; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|