Skip to content

Commit cb3e75c

Browse files
committed
Phan error fixes and more unit test
1 parent d3bd833 commit cb3e75c

4 files changed

Lines changed: 130 additions & 8 deletions

File tree

sample/src/Routes/Post.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace PhpApiSample\Routes;
44

55
use PhpApi\Model\Request\AbstractRequest;
6-
use PhpApi\Model\Request\Attribute\CookieRequestParam;
7-
use PhpApi\Model\Request\Attribute\HeaderRequestParam;
86
use PhpApi\Model\Response\AbstractJsonResponse;
97
use PhpApi\Swagger\Attribute\SwaggerTag;
108

@@ -20,8 +18,6 @@ public function execute(PostRequest $request): PostResponse
2018
class PostRequest extends AbstractRequest
2119
{
2220
public function __construct(
23-
#[HeaderRequestParam(name: 'X-My-Authorization')]
24-
public readonly string $authorization,
2521
public readonly string $someVar,
2622
public readonly string $someMessage,
2723
public readonly PostRequestSubObject $subObject,

src/Model/Request/RequestParser.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ public static function getParamTypes(string $requestClass, ?string $httpMethod,
150150
&& $inputParamType != $contentType
151151
) {
152152
throw new InvalidArgumentException(
153-
"Request class $requestClass has conflicting input types for property $name. "
154-
. "Cannot be both $inputParamType and $contentType",
153+
"Request class $requestClass has conflicting input types for property $name.",
155154
);
156155
}
157156

src/Router.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ public function route(?Request $request = null): ?Response
104104
} else {
105105
throw new InvalidArgumentException("Error handler must return a Response object");
106106
}
107-
108-
return null;
109107
}
110108

111109
$action = ($this->controllerFactory)($route->class);

tests/RouterTest.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
namespace PhpApi\Test;
44

5+
require_once __DIR__ . '/../sample/vendor/autoload.php';
6+
57
use InvalidArgumentException;
68
use PhpApi\Model\RouterOptions;
79
use PhpApi\Router;
810
use PHPUnit\Framework\TestCase;
11+
use Sapien\Request;
912

1013
class 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

Comments
 (0)