|
1
|
|
|
<?php namespace Phprest\Service\Hateoas; |
|
2
|
|
|
|
|
3
|
|
|
use Phprest\Application; |
|
4
|
|
|
use League\Container\Container; |
|
5
|
|
|
use Phprest\Exception\NotAcceptable; |
|
6
|
|
|
use Phprest\Exception\UnsupportedMediaType; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
11
|
|
|
use Phprest\Stub\Entity\Sample; |
|
12
|
|
|
|
|
13
|
|
|
class UtilTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
use Getter; |
|
16
|
|
|
use Util; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Container |
|
20
|
|
|
*/ |
|
21
|
|
|
private $container; |
|
22
|
|
|
|
|
23
|
|
|
public static function setUpBeforeClass() |
|
24
|
|
|
{ |
|
25
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->container = new Container(); |
|
31
|
|
|
|
|
32
|
|
|
$service = new Service(); |
|
33
|
|
|
$service->register($this->container, new Config(true)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testJsonSerialize(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$request = $this->setRequestParameters('phprest', '2.4', 'application/json'); |
|
39
|
|
|
|
|
40
|
|
|
$result = $this->serialize(['a' => 1, 'b' => 2], $request, new Response()); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertEquals('{"a":1,"b":2}', $result->getContent()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testXmlSerialize(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$request = $this->setRequestParameters('phprest', '2.4', 'application/xml'); |
|
48
|
|
|
|
|
49
|
|
|
$result = $this->serialize(['a' => 1, 'b' => 2], $request, new Response()); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertEquals( |
|
52
|
|
|
<<<EOD |
|
53
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
|
54
|
|
|
<result> |
|
55
|
|
|
<entry>1</entry> |
|
56
|
|
|
<entry>2</entry> |
|
57
|
|
|
</result> |
|
58
|
|
|
|
|
59
|
|
|
EOD |
|
60
|
|
|
, |
|
61
|
|
|
$result->getContent() |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testDefaultSerialize(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$request = $this->setRequestParameters('phprest', '2.4', '*/*'); |
|
68
|
|
|
|
|
69
|
|
|
$result = $this->serialize(['a' => 1, 'b' => 2], $request, new Response()); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals('{"a":1,"b":2}', $result->getContent()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @expectedException NotAcceptable |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testNotAcceptableSerialize(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$request = $this->setRequestParameters('phprest', '2.4', 'yaml'); |
|
80
|
|
|
|
|
81
|
|
|
$this->serialize(['a' => 1, 'b' => 2], $request, new Response()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testJsonDeserialize(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$this->container->add(Application::CONTAINER_ID_VENDOR, 'phprest'); |
|
87
|
|
|
$this->container->add(Application::CONTAINER_ID_API_VERSION, '3.2'); |
|
88
|
|
|
|
|
89
|
|
|
$request = new Request([], [], [], [], [], [], '{"a":1,"b":2}'); |
|
90
|
|
|
$request->headers->set('Content-Type', 'application/json'); |
|
91
|
|
|
|
|
92
|
|
|
$sample = $this->deserialize(Sample::class, $request); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertInstanceOf(Sample::class, $sample); |
|
95
|
|
|
$this->assertEquals(1, $sample->a); |
|
96
|
|
|
$this->assertEquals(2, $sample->b); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @expectedException UnsupportedMediaType |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testJsonDeserializeWithUnsopportedFormat(): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->container->add(Application::CONTAINER_ID_VENDOR, 'phprest'); |
|
105
|
|
|
$this->container->add(Application::CONTAINER_ID_API_VERSION, '3.2'); |
|
106
|
|
|
|
|
107
|
|
|
$request = new Request([], [], [], [], [], [], '{"a":1,"b":2}'); |
|
108
|
|
|
$request->headers->set('Content-Type', 'application/yaml'); |
|
109
|
|
|
|
|
110
|
|
|
$this->deserialize(Sample::class, $request); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $vendor |
|
115
|
|
|
* @param string|integer $apiVersion |
|
116
|
|
|
* @param string $acceptHeader |
|
117
|
|
|
* |
|
118
|
|
|
* @return Request |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function setRequestParameters($vendor, $apiVersion, $acceptHeader): Request |
|
121
|
|
|
{ |
|
122
|
|
|
$this->container->add(Application::CONTAINER_ID_VENDOR, $vendor); |
|
123
|
|
|
$this->container->add(Application::CONTAINER_ID_API_VERSION, $apiVersion); |
|
124
|
|
|
|
|
125
|
|
|
(new Service())-> |
|
126
|
|
|
register($this->container, new Config(true)); |
|
127
|
|
|
|
|
128
|
|
|
$request = new Request(); |
|
129
|
|
|
$request->headers->set('Accept', $acceptHeader); |
|
130
|
|
|
|
|
131
|
|
|
$this->container->add('Orno\Http\Request', $request); |
|
132
|
|
|
|
|
133
|
|
|
return $request; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
protected function getContainer(): Container |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->container; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|