22
33namespace PhpApi \Test ;
44
5+ require_once __DIR__ . '/../sample/vendor/autoload.php ' ;
6+
57use InvalidArgumentException ;
68use PhpApi \Model \RouterOptions ;
79use PhpApi \Router ;
810use PHPUnit \Framework \TestCase ;
11+ use Sapien \Request ;
912
1013class RouterTest extends TestCase
1114{
15+ public const Directory = __DIR__ . '/../sample/src/Routes ' ;
16+
1217 public function test_construct_hasGoodOptions_constructs (): void
1318 {
1419 $ router = new Router (
1520 new RouterOptions (
1621 namespace: 'PhpApiSample \\Routes ' ,
22+ directory: self ::Directory,
1723 )
1824 );
1925
@@ -27,10 +33,133 @@ public function test_construct_invalidFactory_throwsException(): void
2733 $ router = new Router (
2834 new RouterOptions (
2935 namespace: 'PhpApiSample \\Routes ' ,
36+ directory: self ::Directory,
3037 ),
3138 controllerFactory: 'test ' ,
3239 );
3340
3441 $ this ->assertInstanceOf (Router::class, $ router );
3542 }
43+
44+ public function test_route_getEndpoint_returnsResponse (): void
45+ {
46+ $ router = new Router (
47+ new RouterOptions (
48+ namespace: 'PhpApiSample \\Routes ' ,
49+ directory: self ::Directory,
50+ )
51+ );
52+
53+ $ response = $ router ->route (new Request (
54+ method: 'GET ' ,
55+ url: [
56+ '/ '
57+ ],
58+ ));
59+
60+ $ this ->assertNotNull ($ response );
61+ $ this ->assertInstanceOf (\PhpApiSample \Routes \GetResponse::class, $ response );
62+ }
63+
64+ public function test_route_postEndpoint_returnsResponse (): void
65+ {
66+ $ router = new Router (
67+ new RouterOptions (
68+ namespace: 'PhpApiSample \\Routes ' ,
69+ directory: self ::Directory,
70+ )
71+ );
72+
73+ $ response = $ router ->route (new Request (
74+ method: 'POST ' ,
75+ url: [
76+ '/ '
77+ ],
78+ globals: [
79+ '_POST ' => [
80+ 'someVar ' => 'test ' ,
81+ 'someMessage ' => 'test ' ,
82+ 'subObject ' => [
83+ 'subMessage ' => 'test ' ,
84+ 'subID ' => 123 ,
85+ ],
86+ ],
87+ ],
88+ ));
89+
90+ $ this ->assertNotNull ($ response );
91+ $ this ->assertInstanceOf (\PhpApiSample \Routes \PostResponse::class, $ response );
92+ $ this ->assertEquals ('{"someVar":"test","someMessage":"test","subObject":{"subMessage":"test","subID":123}} ' , $ response ->message );
93+ }
94+
95+ public function test_route_putEndpoint_returnsResponse (): void
96+ {
97+ $ router = new Router (
98+ new RouterOptions (
99+ namespace: 'PhpApiSample \\Routes ' ,
100+ directory: self ::Directory,
101+ )
102+ );
103+
104+ $ response = $ router ->route (new Request (
105+ method: 'PUT ' ,
106+ url: [
107+ '/ '
108+ ],
109+ ));
110+
111+ $ this ->assertNotNull ($ response );
112+ $ this ->assertInstanceOf (\PhpApiSample \Routes \PutResponse::class, $ response );
113+ }
114+
115+ public function test_route_deleteEndpoint_returnsResponse (): void
116+ {
117+ $ router = new Router (
118+ new RouterOptions (
119+ namespace: 'PhpApiSample \\Routes ' ,
120+ directory: self ::Directory,
121+ )
122+ );
123+
124+ $ response = $ router ->route (new Request (
125+ method: 'DELETE ' ,
126+ url: [
127+ '/ '
128+ ],
129+ ));
130+
131+ $ this ->assertNotNull ($ response );
132+ $ this ->assertInstanceOf (\PhpApiSample \Routes \DeleteResponse::class, $ response );
133+ }
134+
135+ public function test_route_invalidEndpoint_throwsException (): void
136+ {
137+ $ router = new Router (
138+ new RouterOptions (
139+ namespace: 'PhpApiSample \\Routes ' ,
140+ directory: self ::Directory,
141+ )
142+ );
143+
144+ $ router ->handleNotFound (
145+ 'four04 ' ,
146+ );
147+
148+ $ response = $ router ->route (new Request (
149+ method: 'GET ' ,
150+ url: [
151+ 'http:// ' ,
152+ 'localhost ' ,
153+ 80 ,
154+ '' ,
155+ '' ,
156+ '/invalid ' ,
157+ '' ,
158+ '' ,
159+ ],
160+ ));
161+
162+ $ this ->assertNotNull ($ response );
163+ $ this ->assertEquals (404 , $ response ->getCode ());
164+ }
36165}
0 commit comments