Skip to content

Commit dd2b05e

Browse files
committed
Fix #3
1 parent e32ae35 commit dd2b05e

File tree

8 files changed

+262
-17
lines changed

8 files changed

+262
-17
lines changed

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/OpenApiValidation.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
namespace HKarlstrom\Middleware;
1313

1414
use Exception;
15+
use HKarlstrom\Middleware\OpenApiValidation\Exception\BeforeHandlerException;
16+
use HKarlstrom\Middleware\OpenApiValidation\Exception\FileNotFoundException;
17+
use HKarlstrom\Middleware\OpenApiValidation\Exception\InvalidOptionException;
18+
use HKarlstrom\Middleware\OpenApiValidation\Exception\MissingFormatException;
19+
use HKarlstrom\Middleware\OpenApiValidation\Exception\PathNotFoundException;
1520
use HKarlstrom\Middleware\OpenApiValidation\Helpers\Json as JsonHelper;
1621
use HKarlstrom\Middleware\OpenApiValidation\Helpers\Schema as SchemaHelper;
1722
use HKarlstrom\Middleware\OpenApiValidation\Property;
@@ -50,18 +55,18 @@ class OpenApiValidation implements MiddlewareInterface
5055
];
5156
private $formatContainer;
5257

53-
public function __construct(string $file, array $options = [])
58+
public function __construct(string $filename, array $options = [])
5459
{
55-
if (!file_exists($file)) {
56-
throw new Exception(sprintf("The file '%s' does not exist", $file));
60+
if (!file_exists($filename)) {
61+
throw new FileNotFoundException($filename);
5762
}
58-
$this->openapi = new OpenApiReader($file);
63+
$this->openapi = new OpenApiReader($filename);
5964
$allOptions = array_keys($this->options);
6065
foreach ($options as $option => $value) {
6166
if (in_array($option, $allOptions)) {
6267
$this->options[$option] = $value;
6368
} else {
64-
throw new Exception(sprintf('Invalid option: %s', $option));
69+
throw new InvalidOptionException($option);
6570
}
6671
}
6772
$this->formatContainer = new FormatContainer();
@@ -85,20 +90,19 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8590
}
8691

8792
if (null === $path && $this->options['pathNotFoundException']) {
88-
throw new Exception(sprintf('%s %s not defined in OpenAPI document.', mb_strtoupper($method), $request->getRequestTarget()));
93+
throw new PathNotFoundException($method, $request->getRequestTarget());
8994
}
9095
if (null === $path) {
9196
return $handler->handle($request);
9297
}
93-
9498
if ($this->options['validateRequest']
9599
&& $errors = $this->validateRequest($request, $path, $method, $pathParameters)) {
96100
if (is_callable($this->options['beforeHandler'])) {
97101
$beforeRequest = $this->options['beforeHandler']($request, $errors);
98102
if ($beforeRequest instanceof ServerRequestInterface) {
99103
$response = $handler->handle($beforeRequest);
100104
} else {
101-
throw new Exception("Invalid return value from 'beforeHandler' handler");
105+
throw new BeforeHandlerException(gettype($beforeRequest));
102106
}
103107
} else {
104108
$response = $this->error(400, 'Request validation failed', $errors);
@@ -218,7 +222,7 @@ private function checkFormat(string $type, string $format)
218222
$this->formatContainer->add($type, $format, new OpenApiValidation\Formats\RespectValidator($format));
219223
} catch (Exception $e) {
220224
if ($this->options['missingFormatException']) {
221-
throw new Exception(sprintf('Missing validator for type=%s, format=%s', $type, $format));
225+
throw new MissingFormatException($type, $format);
222226
}
223227
}
224228
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* OpenAPI Validation Middleware.
5+
*
6+
* @see https://github.com/hkarlstrom/openapi-validation-middleware
7+
*
8+
* @copyright Copyright (c) 2018 Henrik Karlström
9+
* @license MIT
10+
*/
11+
12+
namespace HKarlstrom\Middleware\OpenApiValidation\Exception;
13+
14+
use Throwable;
15+
use UnexpectedValueException;
16+
17+
class BeforeHandlerException extends UnexpectedValueException
18+
{
19+
/** @var string */
20+
protected $type;
21+
22+
/**
23+
* BeforeHandlerException constructor.
24+
*
25+
* @param string $type
26+
* @param Throwable|null $previous
27+
*/
28+
public function __construct(string $type, Throwable $previous = null)
29+
{
30+
$this->type = $type;
31+
parent::__construct(sprintf("Return value of 'beforeHandler' should be 'ServerRequestInterface', not '%s'", $type), 0, $previous);
32+
}
33+
34+
public function type() : string
35+
{
36+
return $this->type;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* OpenAPI Validation Middleware.
5+
*
6+
* @see https://github.com/hkarlstrom/openapi-validation-middleware
7+
*
8+
* @copyright Copyright (c) 2018 Henrik Karlström
9+
* @license MIT
10+
*/
11+
12+
namespace HKarlstrom\Middleware\OpenApiValidation\Exception;
13+
14+
use RuntimeException;
15+
use Throwable;
16+
17+
class FileNotFoundException extends RuntimeException
18+
{
19+
/** @var string */
20+
protected $filename;
21+
22+
/**
23+
* FileNotFoundException constructor.
24+
*
25+
* @param string $filename
26+
* @param Throwable|null $previous
27+
*/
28+
public function __construct(string $filename, Throwable $previous = null)
29+
{
30+
$this->filename = $filename;
31+
parent::__construct(sprintf("The file '%s' was not found", $filename), 0, $previous);
32+
}
33+
34+
public function filename() : string
35+
{
36+
return $this->filename;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* OpenAPI Validation Middleware.
5+
*
6+
* @see https://github.com/hkarlstrom/openapi-validation-middleware
7+
*
8+
* @copyright Copyright (c) 2018 Henrik Karlström
9+
* @license MIT
10+
*/
11+
12+
namespace HKarlstrom\Middleware\OpenApiValidation\Exception;
13+
14+
use InvalidArgumentException;
15+
use Throwable;
16+
17+
class InvalidOptionException extends InvalidArgumentException
18+
{
19+
/** @var string */
20+
protected $option;
21+
22+
/**
23+
* InvalidOptionException constructor.
24+
*
25+
* @param string $option
26+
* @param Throwable|null $previous
27+
*/
28+
public function __construct(string $option, Throwable $previous = null)
29+
{
30+
$this->option = $option;
31+
parent::__construct(sprintf("The option '%s' is invalid", $option), 0, $previous);
32+
}
33+
34+
public function option() : string
35+
{
36+
return $this->option;
37+
}
38+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* OpenAPI Validation Middleware.
5+
*
6+
* @see https://github.com/hkarlstrom/openapi-validation-middleware
7+
*
8+
* @copyright Copyright (c) 2018 Henrik Karlström
9+
* @license MIT
10+
*/
11+
12+
namespace HKarlstrom\Middleware\OpenApiValidation\Exception;
13+
14+
use RuntimeException;
15+
use Throwable;
16+
17+
class MissingFormatException extends RuntimeException
18+
{
19+
/** @var string */
20+
protected $type;
21+
22+
/** @var string */
23+
protected $format;
24+
25+
/**
26+
* MissingFormatException constructor.
27+
*
28+
* @param string $type
29+
* @param string $format
30+
* @param Throwable|null $previous
31+
*/
32+
public function __construct(string $type, string $format, Throwable $previous = null)
33+
{
34+
$this->type = $type;
35+
$this->format = $format;
36+
parent::__construct(sprintf('Missing validator for type=%s, format=%s', $type, $format), 0, $previous);
37+
}
38+
39+
public function type() : string
40+
{
41+
return $this->type;
42+
}
43+
44+
public function format() : string
45+
{
46+
return $this->format;
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* OpenAPI Validation Middleware.
5+
*
6+
* @see https://github.com/hkarlstrom/openapi-validation-middleware
7+
*
8+
* @copyright Copyright (c) 2018 Henrik Karlström
9+
* @license MIT
10+
*/
11+
12+
namespace HKarlstrom\Middleware\OpenApiValidation\Exception;
13+
14+
use InvalidArgumentException;
15+
use Throwable;
16+
17+
class PathNotFoundException extends InvalidArgumentException
18+
{
19+
/** @var string */
20+
protected $method;
21+
22+
/** @var string */
23+
protected $path;
24+
25+
/**
26+
* PathNotFoundException constructor.
27+
*
28+
* @param string $method
29+
* @param string $path
30+
* @param Throwable|null $previous
31+
*/
32+
public function __construct(string $method, string $path, Throwable $previous = null)
33+
{
34+
$this->method = mb_strtoupper($method);
35+
$this->path = $path;
36+
parent::__construct(sprintf('%s %s not defined in OpenAPI document', $this->method, $path), 0, $previous);
37+
}
38+
39+
public function method() : string
40+
{
41+
return $this->method;
42+
}
43+
44+
public function path() : string
45+
{
46+
return $this->path;
47+
}
48+
}

tests/ExceptionsTest.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,62 @@
1212
namespace HKarlstrom\Middleware\OpenApiValidation;
1313

1414
use HKarlstrom\Middleware\OpenApiValidation;
15+
use Psr\Http\Message\ServerRequestInterface;
1516

1617
class ExceptionsTest extends BaseTest
1718
{
1819
public function testFileNotExist()
1920
{
20-
$this->expectException('Exception');
21-
$mw = new OpenApiValidation('not_a_file.json');
21+
try {
22+
$mw = new OpenApiValidation('not_a_file.json');
23+
} catch (\HKarlstrom\Middleware\OpenApiValidation\Exception\FileNotFoundException $e) {
24+
$this->assertSame('not_a_file.json', $e->filename());
25+
}
2226
}
2327

2428
public function testInvalidOption()
2529
{
26-
$this->expectException('Exception');
27-
$mw = new OpenApiValidation($this->openapiFile, ['invalidOption' => true]);
30+
try {
31+
$mw = new OpenApiValidation($this->openapiFile, ['invalidOption' => true]);
32+
} catch (\HKarlstrom\Middleware\OpenApiValidation\Exception\InvalidOptionException $e) {
33+
$this->assertSame('invalidOption', $e->option());
34+
}
2835
}
2936

3037
public function testPathNotFound()
3138
{
32-
$this->expectException('Exception');
33-
$response = $this->response('get', '/not/defined');
39+
try {
40+
$response = $this->response('get', '/not/defined');
41+
} catch (\HKarlstrom\Middleware\OpenApiValidation\Exception\PathNotFoundException $e) {
42+
$this->assertSame('GET', $e->method());
43+
$this->assertSame('/not/defined', $e->path());
44+
}
45+
}
46+
47+
public function testInvalidBeforeHandlerReturnValue()
48+
{
49+
try {
50+
$response = $this->response('get', '/parameters', [
51+
'options' => [
52+
'beforeHandler' => function (ServerRequestInterface $request, array $errors) {
53+
return 'no';
54+
},
55+
],
56+
]);
57+
} catch (\HKarlstrom\Middleware\OpenApiValidation\Exception\BeforeHandlerException $e) {
58+
$this->assertSame('string', $e->type());
59+
}
3460
}
3561

3662
public function testFormatMissingException()
3763
{
3864
$this->expectException('Exception');
39-
$response = $this->response('get', '/missing/format', ['query' => ['test' => 'foo']]);
65+
try {
66+
$response = $this->response('get', '/missing/format', ['query' => ['test' => 'foo']]);
67+
} catch (\HKarlstrom\Middleware\OpenApiValidation\Exception\PathNotFoundException $e) {
68+
$this->assertSame('string', $e->type());
69+
$this->assertSame('uid', $e->format());
70+
}
4071
}
4172

4273
public function testPathNotFoundNoException()

0 commit comments

Comments
 (0)