@@ -37,7 +37,81 @@ public function testItWillCreateXmlResponse()
3737
3838 $ body = <<<XML
3939<?xml version="1.0" encoding="UTF-8"?>
40- <problem xmlns="urn:ietf:rfc:XXXX">
40+ <problem xmlns="urn:ietf:rfc:7807">
41+ <title>Not Found</title>
42+ <status>404</status>
43+ <detail>User with id 5 not found.</detail>
44+ <type>http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html</type>
45+ </problem>
46+ XML ;
47+
48+ $ this ->assertEquals ($ response ->getBody ()->getContents (), $ body );
49+ $ this ->assertEquals ($ response ->getStatusCode (), 404 );
50+ $ this ->assertEquals ($ response ->getHeaderLine ('Content-type ' ), 'application/problem+xml ' );
51+ }
52+
53+ public function testToJson ()
54+ {
55+ $ response = ApiProblemResponse::json (404 , 'User with id 5 not found. ' , 'Not Found ' );
56+
57+ $ body = <<<JSON
58+ {
59+ "title": "Not Found",
60+ "status": 404,
61+ "detail": "User with id 5 not found."
62+ }
63+ JSON ;
64+
65+ $ this ->assertEquals ($ response ->getBody ()->getContents (), $ body );
66+ $ this ->assertEquals ($ response ->getStatusCode (), 404 );
67+ $ this ->assertEquals ($ response ->getHeaderLine ('Content-type ' ), 'application/problem+json ' );
68+ }
69+
70+ public function testToJsonFromException ()
71+ {
72+ $ exception = new \Exception ('User with id 5 not found. ' , 404 );
73+ $ response = ApiProblemResponse::fromExceptionToJson ($ exception );
74+
75+ $ body = <<<JSON
76+ {
77+ "title": "Not Found",
78+ "status": 404,
79+ "detail": "User with id 5 not found.",
80+ "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"
81+ }
82+ JSON ;
83+
84+ $ this ->assertEquals ($ response ->getBody ()->getContents (), $ body );
85+ $ this ->assertEquals ($ response ->getStatusCode (), 404 );
86+ $ this ->assertEquals ($ response ->getHeaderLine ('Content-type ' ), 'application/problem+json ' );
87+ }
88+
89+ public function testToXml ()
90+ {
91+ $ response = ApiProblemResponse::xml (404 , 'User with id 5 not found. ' , 'Not Found ' );
92+
93+ $ body = <<<XML
94+ <?xml version="1.0" encoding="UTF-8"?>
95+ <problem xmlns="urn:ietf:rfc:7807">
96+ <title>Not Found</title>
97+ <status>404</status>
98+ <detail>User with id 5 not found.</detail>
99+ </problem>
100+ XML ;
101+
102+ $ this ->assertEquals ($ response ->getBody ()->getContents (), $ body );
103+ $ this ->assertEquals ($ response ->getStatusCode (), 404 );
104+ $ this ->assertEquals ($ response ->getHeaderLine ('Content-type ' ), 'application/problem+xml ' );
105+ }
106+
107+ public function testToXmlFromException ()
108+ {
109+ $ exception = new \Exception ('User with id 5 not found. ' , 404 );
110+ $ response = ApiProblemResponse::fromExceptionToXml ($ exception );
111+
112+ $ body = <<<XML
113+ <?xml version="1.0" encoding="UTF-8"?>
114+ <problem xmlns="urn:ietf:rfc:7807">
41115<title>Not Found</title>
42116<status>404</status>
43117<detail>User with id 5 not found.</detail>
0 commit comments