1
+ <?php
2
+
3
+ /**
4
+ * MIT License
5
+ *
6
+ * Copyright (c) 2018 Samuel CHEMLA
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ */
26
+
27
+ namespace PhpBg \MiniHttpd \Tests \Renderer ;
28
+
29
+ use GuzzleHttp \Psr7 \Response ;
30
+ use GuzzleHttp \Psr7 \ServerRequest ;
31
+ use PhpBg \MiniHttpd \HttpException \NotFoundException ;
32
+ use PhpBg \MiniHttpd \Renderer \Json ;
33
+ use PHPUnit \Framework \TestCase ;
34
+
35
+ class JsonTest extends TestCase
36
+ {
37
+ public function testRenderOK ()
38
+ {
39
+ $ renderer = new Json ();
40
+ $ request = new ServerRequest ('GET ' , '/ ' );
41
+ $ response = new Response ();
42
+ $ data = ['foo ' => 'bar ' ];
43
+
44
+ $ response = $ renderer ->render ($ request , $ response , [], $ data );
45
+
46
+ $ this ->assertSame (200 , $ response ->getStatusCode ());
47
+ $ this ->assertSame (json_encode ($ data ), $ response ->getBody ()->getContents ());
48
+ $ this ->assertSame ('application/json ' , $ response ->getHeaderLine ('Content-Type ' ));
49
+ }
50
+
51
+ public function testRenderKO ()
52
+ {
53
+ $ renderer = new Json ();
54
+ $ request = new ServerRequest ('GET ' , '/ ' );
55
+ $ response = new Response ();
56
+ $ data = ['foo ' => log (0 )];
57
+
58
+ $ response = $ renderer ->render ($ request , $ response , [], $ data );
59
+
60
+ $ this ->assertSame (500 , $ response ->getStatusCode ());
61
+ $ msg = $ response ->getBody ()->getContents ();
62
+ $ this ->assertNotEmpty ($ msg );
63
+ $ this ->assertSame (0 , strpos ($ msg , "Error " ));
64
+ }
65
+
66
+ public function testRenderHttpException ()
67
+ {
68
+ $ renderer = new Json ();
69
+ $ request = new ServerRequest ('GET ' , '/ ' );
70
+ $ response = new Response ();
71
+ $ exception = new NotFoundException ();
72
+
73
+ $ response = $ renderer ->renderException ($ request , $ response , [], $ exception );
74
+
75
+ $ this ->assertSame (404 , $ response ->getStatusCode ());
76
+ }
77
+
78
+ public function testRenderException ()
79
+ {
80
+ $ renderer = new Json ();
81
+ $ request = new ServerRequest ('GET ' , '/ ' );
82
+ $ response = new Response ();
83
+ $ exception = new \Exception ();
84
+
85
+ $ response = $ renderer ->renderException ($ request , $ response , [], $ exception );
86
+
87
+ $ this ->assertSame (500 , $ response ->getStatusCode ());
88
+ }
89
+ }
0 commit comments