33namespace SwoftTest \HttpServer ;
44
55use PHPUnit \Framework \TestCase ;
6+ use Swoft \App ;
7+ use Swoft \Helper \ArrayHelper ;
8+ use Swoft \Testing \SwooleRequest as TestSwooleRequest ;
9+ use Swoft \Testing \SwooleResponse as TestSwooleResponse ;
10+ use Swoft \Http \Message \Testing \Web \Request ;
11+ use Swoft \Http \Message \Testing \Web \Response ;
612
713/**
8- * @uses AbstractTestCase
9- * @version 2017年11月03日
10- * @author huangzhhui <[email protected] > 11- * @copyright Copyright 2010-2017 Swoft software
12- * @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
14+ * Class AbstractTestCase
15+ *
16+ * @package Swoft\Test\Cases
1317 */
14- abstract class AbstractTestCase extends TestCase
18+ class AbstractTestCase extends TestCase
1519{
20+ const ACCEPT_VIEW = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 ' ;
1621
22+ const ACCEPT_JSON = 'application/json ' ;
23+
24+ const ACCEPT_RAW = 'text/plain ' ;
25+
26+ /**
27+ * Send a mock request
28+ *
29+ * @param string $method
30+ * @param string $uri
31+ * @param array $parameters
32+ * @param string $accept
33+ * @param array $headers
34+ * @param string $rawContent
35+ * @return bool|\Swoft\Http\Message\Testing\Web\Response
36+ */
37+ public function request (
38+ string $ method ,
39+ string $ uri ,
40+ array $ parameters = [],
41+ string $ accept = self ::ACCEPT_JSON ,
42+ array $ headers = [],
43+ string $ rawContent = ''
44+ ) {
45+ $ method = strtoupper ($ method );
46+ $ swooleResponse = new TestSwooleResponse ();
47+ $ swooleRequest = new TestSwooleRequest ();
48+
49+ $ this ->buildMockRequest ($ method , $ uri , $ parameters , $ accept , $ swooleRequest , $ headers );
50+
51+ $ swooleRequest ->setRawContent ($ rawContent );
52+
53+ $ request = Request::loadFromSwooleRequest ($ swooleRequest );
54+ $ response = new Response ($ swooleResponse );
55+
56+ /** @var \Swoft\Http\Server\ServerDispatcher $dispatcher */
57+ $ dispatcher = App::getBean ('serverDispatcher ' );
58+ return $ dispatcher ->dispatch ($ request , $ response );
59+ }
60+
61+ /**
62+ * Send a mock json request
63+ *
64+ * @param string $method
65+ * @param string $uri
66+ * @param array $parameters
67+ * @param array $headers
68+ * @param string $rawContent
69+ * @return bool|\Swoft\Http\Message\Testing\Web\Response
70+ */
71+ public function json (
72+ string $ method ,
73+ string $ uri ,
74+ array $ parameters = [],
75+ array $ headers = [],
76+ string $ rawContent = ''
77+ ) {
78+ return $ this ->request ($ method , $ uri , $ parameters , self ::ACCEPT_JSON , $ headers , $ rawContent );
79+ }
80+
81+ /**
82+ * Send a mock view request
83+ *
84+ * @param string $method
85+ * @param string $uri
86+ * @param array $parameters
87+ * @param array $headers
88+ * @param string $rawContent
89+ * @return bool|\Swoft\Http\Message\Testing\Web\Response
90+ */
91+ public function view (
92+ string $ method ,
93+ string $ uri ,
94+ array $ parameters = [],
95+ array $ headers = [],
96+ string $ rawContent = ''
97+ ) {
98+ return $ this ->request ($ method , $ uri , $ parameters , self ::ACCEPT_VIEW , $ headers , $ rawContent );
99+ }
100+
101+ /**
102+ * Send a mock raw content request
103+ *
104+ * @param string $method
105+ * @param string $uri
106+ * @param array $parameters
107+ * @param array $headers
108+ * @param string $rawContent
109+ * @return bool|\Swoft\Http\Message\Testing\Web\Response
110+ */
111+ public function raw (
112+ string $ method ,
113+ string $ uri ,
114+ array $ parameters = [],
115+ array $ headers = [],
116+ string $ rawContent = ''
117+ ) {
118+ return $ this ->request ($ method , $ uri , $ parameters , self ::ACCEPT_RAW , $ headers , $ rawContent );
119+ }
120+
121+ /**
122+ * @param string $method
123+ * @param string $uri
124+ * @param array $parameters
125+ * @param string $accept
126+ * @param \Swoole\Http\Request $swooleRequest
127+ * @param array $headers
128+ */
129+ protected function buildMockRequest (
130+ string $ method ,
131+ string $ uri ,
132+ array $ parameters ,
133+ string $ accept ,
134+ &$ swooleRequest ,
135+ array $ headers = []
136+ ) {
137+ $ urlAry = parse_url ($ uri );
138+ $ urlParams = [];
139+ if (isset ($ urlAry ['query ' ])) {
140+ parse_str ($ urlAry ['query ' ], $ urlParams );
141+ }
142+ $ defaultHeaders = [
143+ 'host ' => '127.0.0.1 ' ,
144+ 'connection ' => 'keep-alive ' ,
145+ 'cache-control ' => 'max-age=0 ' ,
146+ 'user-agent ' => 'PHPUnit ' ,
147+ 'upgrade-insecure-requests ' => '1 ' ,
148+ 'accept ' => $ accept ,
149+ 'dnt ' => '1 ' ,
150+ 'accept-encoding ' => 'gzip, deflate, br ' ,
151+ 'accept-language ' => 'zh-CN,zh;q=0.8,en;q=0.6,it-IT;q=0.4,it;q=0.2 ' ,
152+ ];
153+
154+ $ swooleRequest ->fd = 1 ;
155+ $ swooleRequest ->header = ArrayHelper::merge ($ headers , $ defaultHeaders );
156+ $ swooleRequest ->server = [
157+ 'request_method ' => $ method ,
158+ 'request_uri ' => $ uri ,
159+ 'path_info ' => '/ ' ,
160+ 'request_time ' => microtime (),
161+ 'request_time_float ' => microtime (true ),
162+ 'server_port ' => 80 ,
163+ 'remote_port ' => 54235 ,
164+ 'remote_addr ' => '10.0.2.2 ' ,
165+ 'master_time ' => microtime (),
166+ 'server_protocol ' => 'HTTP/1.1 ' ,
167+ 'server_software ' => 'swoole-http-server ' ,
168+ ];
169+
170+ if ($ method == 'GET ' ) {
171+ $ swooleRequest ->get = $ parameters ;
172+ } elseif ($ method == 'POST ' ) {
173+ $ swooleRequest ->post = $ parameters ;
174+ }
175+
176+ if (! empty ($ urlParams )) {
177+ $ get = empty ($ swooleRequest ->get ) ? [] : $ swooleRequest ->get ;
178+ $ swooleRequest ->get = array_merge ($ urlParams , $ get );
179+ }
180+ }
17181}
0 commit comments