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