22
33namespace PhpDevCommunity \tests ;
44
5+ use Exception ;
6+ use LogicException ;
57use PhpDevCommunity \HttpClient \HttpClient ;
68use PHPUnit \Framework \TestCase ;
79
@@ -10,38 +12,46 @@ class HttpClientTest extends TestCase
1012 const URL = 'http://localhost:4245 ' ;
1113
1214 protected static ?string $ serverProcess = null ;
15+
1316 public static function setUpBeforeClass (): void
1417 {
15- $ fileToRun = __DIR__ . DIRECTORY_SEPARATOR . 'test_server.php ' ;
16- $ command = sprintf ('php -S %s %s > /dev/null 2>&1 & echo $!; ' ,str_replace ('http:// ' , '' , self ::URL ), $ fileToRun );
18+ $ fileToRun = __DIR__ . DIRECTORY_SEPARATOR . 'test_server.php ' ;
19+ $ command = sprintf ('php -S %s %s > /dev/null 2>&1 & echo $!; ' , str_replace ('http:// ' , '' , self ::URL ), $ fileToRun );
1720 self ::$ serverProcess = exec ($ command );
1821 if (empty (self ::$ serverProcess ) || !is_numeric (self ::$ serverProcess )) {
19- throw new \ Exception ('Could not start test server ' );
22+ throw new Exception ('Could not start test server ' );
2023 }
2124 sleep (1 );
2225 }
2326
2427 public function testGetRequest ()
2528 {
26- $ response = http_client (['base_url ' => self ::URL , 'headers ' => ['Authorization ' => 'Bearer secret_token ' ]])->get ('/api/data ' );
27- $ this ->assertEquals ( 200 , $ response ->getStatusCode () );
29+ $ response = http_client (
30+ ['base_url ' => self ::URL , 'headers ' => ['Authorization ' => 'Bearer secret_token ' ]],
31+ function ($ info ) {
32+ $ this ->assertEquals ( 'GET ' , $ info ['request ' ]['method ' ]);
33+ $ this ->assertEquals ( 'Bearer secret_token ' , $ info ['request ' ]['headers ' ]['Authorization ' ]);
34+ $ this ->assertEquals ( '{"message":"GET request received"} ' , $ info ['response ' ]['body ' ]);
35+ }
36+ )->get ('/api/data ' );
37+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
2838 $ this ->assertNotEmpty ($ response ->getBody ());
2939 }
3040
3141 public function testGetWithQueryRequest ()
3242 {
33- $ client = new HttpClient (['base_url ' => self ::URL ,'headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
43+ $ client = new HttpClient (['base_url ' => self ::URL , 'headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
3444 $ response = $ client ->get ('/api/search ' , [
3545 'name ' => 'foo ' ,
3646 ]);
3747
38- $ this ->assertEquals ( 200 , $ response ->getStatusCode () );
48+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
3949 $ this ->assertNotEmpty ($ response ->getBody ());
4050
4151 $ data = $ response ->bodyToArray ();
42- $ this ->assertEquals ( 'foo ' , $ data ['name ' ] );
43- $ this ->assertEquals ( 1 , $ data ['page ' ] );
44- $ this ->assertEquals ( 10 , $ data ['limit ' ] );
52+ $ this ->assertEquals ('foo ' , $ data ['name ' ]);
53+ $ this ->assertEquals (1 , $ data ['page ' ]);
54+ $ this ->assertEquals (10 , $ data ['limit ' ]);
4555
4656
4757 $ response = $ client ->get ('/api/search ' , [
@@ -50,13 +60,13 @@ public function testGetWithQueryRequest()
5060 'limit ' => 100
5161 ]);
5262
53- $ this ->assertEquals ( 200 , $ response ->getStatusCode () );
63+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
5464 $ this ->assertNotEmpty ($ response ->getBody ());
5565
5666 $ data = $ response ->bodyToArray ();
57- $ this ->assertEquals ( 'foo ' , $ data ['name ' ] );
58- $ this ->assertEquals ( 10 , $ data ['page ' ] );
59- $ this ->assertEquals ( 100 , $ data ['limit ' ] );
67+ $ this ->assertEquals ('foo ' , $ data ['name ' ]);
68+ $ this ->assertEquals (10 , $ data ['page ' ]);
69+ $ this ->assertEquals (100 , $ data ['limit ' ]);
6070 }
6171
6272 public function testPostJsonRequest ()
@@ -67,14 +77,14 @@ public function testPostJsonRequest()
6777 'userId ' => 1
6878 ];
6979 $ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
70- $ response = $ client ->post (self ::URL . '/api/post/data ' , [
80+ $ response = $ client ->post (self ::URL . '/api/post/data ' , [
7181 'title ' => 'foo ' ,
7282 'body ' => 'bar ' ,
7383 'userId ' => 1
7484 ], true );
7585
76- $ this ->assertEquals ( 200 , $ response ->getStatusCode ());
77- $ this ->assertEquals ( $ dataToPost , $ response ->bodyToArray ());
86+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
87+ $ this ->assertEquals ($ dataToPost , $ response ->bodyToArray ());
7888 }
7989
8090 public function testPostFormRequest ()
@@ -85,18 +95,50 @@ public function testPostFormRequest()
8595 'userId ' => 1
8696 ];
8797 $ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
88- $ response = $ client ->post (self ::URL . '/api/post/data/form ' , $ dataToPost );
98+ $ response = $ client ->post (self ::URL . '/api/post/data/form ' , $ dataToPost );
8999
90- $ this ->assertEquals ( 200 , $ response ->getStatusCode ());
91- $ this ->assertEquals ( $ dataToPost , $ response ->bodyToArray ());
100+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
101+ $ this ->assertEquals ($ dataToPost , $ response ->bodyToArray ());
92102 }
93103
94104 public function testPostEmptyFormRequest ()
95105 {
96106 $ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
97- $ response = $ client ->post (self ::URL .'/api/post/data/form ' , []);
107+ $ response = $ client ->post (self ::URL . '/api/post/data/form ' , []);
108+
109+ $ this ->assertEquals (400 , $ response ->getStatusCode ());
110+ }
111+
112+ public function testWrongOptions ()
113+ {
114+ $ this ->expectException (LogicException::class);
115+ new HttpClient (['headers ' => 'string ' ]);
116+ }
117+
118+ public function testWrongOptions2 ()
119+ {
120+ $ this ->expectException (LogicException::class);
121+ new HttpClient (['options_not_supported ' => 'value ' ]);
122+ }
123+
124+ public function testWrongOptions3 ()
125+ {
126+ $ this ->expectException (LogicException::class);
127+ new HttpClient (['timeout ' => 'string ' ]);
128+ }
129+
130+ public function testWrongMethod ()
131+ {
132+ $ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
133+ $ this ->expectException (LogicException::class);
134+ $ client ->fetch (self ::URL . '/api/data ' , ['method ' => 'WRONG ' ]);
135+ }
98136
99- $ this ->assertEquals ( 400 , $ response ->getStatusCode () );
137+ public function testWrongUrl ()
138+ {
139+ $ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
140+ $ this ->expectException (LogicException::class);
141+ $ client ->fetch ('WRONG_URL ' , ['method ' => 'GET ' ]);
100142 }
101143
102144
0 commit comments