1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ntb\RestAPI; |
4
|
|
|
|
5
|
|
|
use SapphireTest; |
6
|
|
|
use SS_HTTPRequest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Tests for the serializer factory. |
10
|
|
|
* @author Christian Blank <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class SerializerFactoryTest extends SapphireTest { |
13
|
|
|
|
14
|
|
|
public function testNotProvidedMimeType() { |
15
|
|
|
$this->assertTrue(SerializerFactory::create() instanceof JsonSerializer); |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testMimeTypes() { |
19
|
|
|
$this->assertTrue(SerializerFactory::create('application/json') instanceof JsonSerializer); |
|
|
|
|
20
|
|
|
$this->assertTrue(SerializerFactory::create('application/xml') instanceof XmlSerializer); |
|
|
|
|
21
|
|
|
$this->assertTrue(SerializerFactory::create('application/yaml') instanceof YamlSerializer); |
|
|
|
|
22
|
|
|
$this->assertTrue(SerializerFactory::create('text/html') instanceof HtmlSerializer); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testEmptyMimeType() { |
26
|
|
|
TestHelper::assertException(function() { |
27
|
|
|
SerializerFactory::create(""); |
28
|
|
|
}, 'Ntb\RestAPI\RestUserException'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testUnsupportedMimeType() { |
32
|
|
|
TestHelper::assertException(function() { |
33
|
|
|
SerializerFactory::create("foo/bar"); |
34
|
|
|
}, 'Ntb\RestAPI\RestUserException'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testCreateFromRequestWithAcceptHeader() { |
38
|
|
|
$request = new SS_HTTPRequest("GET", ""); |
39
|
|
|
$request->addHeader('Accept', 'text/html,multipart/mixed,*/*'); |
40
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request) instanceof HtmlSerializer); |
|
|
|
|
41
|
|
|
$request1 = new SS_HTTPRequest("GET", ""); |
42
|
|
|
$request1->addHeader('Accept', 'application/json,text/html,multipart/mixed,*/*'); |
43
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request1) instanceof JsonSerializer); |
|
|
|
|
44
|
|
|
$request2 = new SS_HTTPRequest("GET", ""); |
45
|
|
|
$request2->addHeader('Accept', 'foo/bar,application/yaml,text/html,multipart/mixed,*/*'); |
46
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request2) instanceof YamlSerializer); |
|
|
|
|
47
|
|
|
$request3 = new SS_HTTPRequest("GET", ""); |
48
|
|
|
$request3->addHeader('Accept', 'foo/bar,application/xml,text/html,multipart/mixed,*/*'); |
49
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request3) instanceof XmlSerializer); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testCreateFromRequestWithUnsupportedAcceptHeader() { |
53
|
|
|
$request = new SS_HTTPRequest("GET", ""); |
54
|
|
|
$request->addHeader('Accept', 'foo/bar, multipart/mixed, */*'); |
55
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request) instanceof JsonSerializer); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testCreateFromRequestWithGetVar() { |
59
|
|
|
$request = new SS_HTTPRequest("GET", "", ['accept' => 'json']); |
60
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request) instanceof JsonSerializer); |
|
|
|
|
61
|
|
|
$request1 = new SS_HTTPRequest("GET", "", ['accept' => 'yaml']); |
62
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request1) instanceof YamlSerializer); |
|
|
|
|
63
|
|
|
$request2 = new SS_HTTPRequest("GET", "", ['accept' => 'xml']); |
64
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request2) instanceof XmlSerializer); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testCreateFromRequestWithWrongGetVar() { |
68
|
|
|
$request = new SS_HTTPRequest("GET", "", ['accept' => 'foo']); |
69
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request) instanceof JsonSerializer); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetVarBeforeHeader() { |
73
|
|
|
$request = new SS_HTTPRequest("GET", "", ['accept' => 'yaml']); |
74
|
|
|
$request->addHeader('Accept', 'text/html,multipart/mixed,*/*'); |
75
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request) instanceof YamlSerializer); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGetVarAndHeaderEmpty() { |
79
|
|
|
$request = new SS_HTTPRequest("GET", ""); |
80
|
|
|
$request->addHeader('Accept', ''); |
81
|
|
|
$this->assertTrue(SerializerFactory::create_from_request($request) instanceof JsonSerializer); |
|
|
|
|
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.