1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: Milad Rahimi <[email protected] > 5+ * Date: 7/13/2018 AD
6+ * Time: 18:21
7+ */
8+
9+ namespace MiladRahimi \Router \Tests ;
10+
11+ use Throwable ;
12+ use Zend \Diactoros \Response \EmptyResponse ;
13+ use Zend \Diactoros \Response \HtmlResponse ;
14+ use Zend \Diactoros \Response \JsonResponse ;
15+ use Zend \Diactoros \Response \RedirectResponse ;
16+ use Zend \Diactoros \Response \TextResponse ;
17+
18+ class ResponseTest extends TestCase
19+ {
20+ /**
21+ * @throws Throwable
22+ */
23+ public function test_empty_response_with_code_204 ()
24+ {
25+ $ router = $ this ->createRouterWithMockedProperties ();
26+
27+ $ router ->map ('GET ' , '/ ' , function () {
28+ return new EmptyResponse (204 );
29+ });
30+
31+ $ router ->dispatch ();
32+
33+ $ this ->assertEquals (204 , $ this ->getPublisher ($ router )->responseCode );
34+ }
35+
36+ /**
37+ * @throws Throwable
38+ */
39+ public function test_html_response_with_code_200 ()
40+ {
41+ $ router = $ this ->createRouterWithMockedProperties ();
42+
43+ $ router ->map ('GET ' , '/ ' , function () {
44+ return new HtmlResponse ('<html></html> ' , 200 );
45+ });
46+
47+ $ router ->dispatch ();
48+
49+ $ this ->assertEquals (200 , $ this ->getPublisher ($ router )->responseCode );
50+ $ this ->assertEquals ('<html></html> ' , $ this ->getPublisher ($ router )->output );
51+ }
52+
53+ /**
54+ * @throws Throwable
55+ */
56+ public function test_json_response_with_code_201 ()
57+ {
58+ $ router = $ this ->createRouterWithMockedProperties ();
59+
60+ $ router ->map ('GET ' , '/ ' , function () {
61+ return new JsonResponse (['a ' => 'x ' , 'b ' => 'y ' ], 201 );
62+ });
63+
64+ $ router ->dispatch ();
65+
66+ $ this ->assertEquals (201 , $ this ->getPublisher ($ router )->responseCode );
67+ $ this ->assertEquals (json_encode (['a ' => 'x ' , 'b ' => 'y ' ]), $ this ->getPublisher ($ router )->output );
68+ }
69+
70+ /**
71+ * @throws Throwable
72+ */
73+ public function test_text_response_with_code_203 ()
74+ {
75+ $ router = $ this ->createRouterWithMockedProperties ();
76+
77+ $ router ->map ('GET ' , '/ ' , function () {
78+ return new TextResponse ('Content ' , 203 );
79+ });
80+
81+ $ router ->dispatch ();
82+
83+ $ this ->assertEquals (203 , $ this ->getPublisher ($ router )->responseCode );
84+ $ this ->assertEquals ('Content ' , $ this ->getPublisher ($ router )->output );
85+ }
86+
87+ /**
88+ * @throws Throwable
89+ */
90+ public function test_redirect_response_with_code_203 ()
91+ {
92+ $ router = $ this ->createRouterWithMockedProperties ();
93+
94+ $ router ->map ('GET ' , '/ ' , function () {
95+ return new RedirectResponse ('https://miladrahimi.com ' );
96+ });
97+
98+ $ router ->dispatch ();
99+
100+ $ this ->assertEquals (302 , $ this ->getPublisher ($ router )->responseCode );
101+ $ this ->assertEquals ('' , $ this ->getPublisher ($ router )->output );
102+ $ this ->assertContains ('location: https://miladrahimi.com ' , $ this ->getPublisher ($ router )->headerLines );
103+ }
104+ }
0 commit comments