1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fitbug\Guzzle\SwaggerValidation; |
4
|
|
|
|
5
|
|
|
use FR3D\SwaggerAssertions\PhpUnit\Psr7AssertsTrait; |
6
|
|
|
use FR3D\SwaggerAssertions\SchemaManager; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Middleware that wraps around the PSR7 Assertion handler to make it work for guzzle. |
12
|
|
|
*/ |
13
|
|
|
class SwaggerSchemaValidationHandler |
14
|
|
|
{ |
15
|
|
|
use Psr7AssertsTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var SchemaManager |
19
|
|
|
*/ |
20
|
|
|
private $schemaManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $skip; |
26
|
|
|
/** |
27
|
|
|
* @var Psr7RequestPrinterInterface |
28
|
|
|
*/ |
29
|
|
|
private $psr7RequestPrinter; |
30
|
|
|
/** |
31
|
|
|
* @var Psr7ResponsePrinterInterface |
32
|
|
|
*/ |
33
|
|
|
private $psr7ResponsePrinter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $uri; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* SwaggerSchemaValidationHandler constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $uri |
44
|
|
|
* @param Psr7RequestPrinterInterface|null $psr7RequestPrinter |
45
|
|
|
* @param Psr7ResponsePrinterInterface|null $psr7ResponsePrinter |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct( |
48
|
|
|
$uri, |
49
|
|
|
Psr7RequestPrinterInterface $psr7RequestPrinter = null, |
50
|
|
|
Psr7ResponsePrinterInterface $psr7ResponsePrinter = null |
51
|
|
|
) { |
52
|
1 |
|
$this->uri = $uri; |
53
|
|
|
|
54
|
1 |
|
if (!$psr7RequestPrinter) { |
55
|
1 |
|
$psr7RequestPrinter = new Psr7RequestPrinter(); |
56
|
|
|
} |
57
|
1 |
|
$this->psr7RequestPrinter = $psr7RequestPrinter; |
58
|
|
|
|
59
|
1 |
|
if (!$psr7ResponsePrinter) { |
60
|
1 |
|
$psr7ResponsePrinter = new Psr7ResponsePrinter(); |
61
|
|
|
} |
62
|
1 |
|
$this->psr7ResponsePrinter = $psr7ResponsePrinter; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Disable (or enable) the middleware's testing temporarily. |
67
|
|
|
* |
68
|
|
|
* @param bool $skip |
69
|
|
|
*/ |
70
|
|
|
public function setSkip($skip) |
71
|
|
|
{ |
72
|
|
|
$this->skip = $skip; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Guzzle Middleware. |
77
|
|
|
* |
78
|
|
|
* @param callable $handler |
79
|
|
|
* |
80
|
|
|
* @return \Closure |
81
|
|
|
*/ |
82
|
|
|
public function __invoke(callable $handler) |
83
|
|
|
{ |
84
|
|
|
return function ( |
85
|
|
|
RequestInterface $request, |
86
|
|
|
array $options |
87
|
|
|
) use ($handler) { |
88
|
|
|
$promise = $handler($request, $options); |
89
|
|
|
|
90
|
|
|
return $promise->then( |
91
|
|
|
function (ResponseInterface $response) use ($request) { |
92
|
|
|
if (!$this->skip) { |
93
|
|
|
// This needs to be done every time as it modifies itself during the validation |
94
|
|
|
// meaning the second request will fail. |
95
|
|
|
$schemaManager = $this->schemaManager = SchemaManager::fromUri($this->uri); |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$this->assertResponseAndRequestMatch($response, $request, $schemaManager); |
99
|
|
|
} catch (\Exception $e) { |
100
|
|
|
$requestString = $this->psr7RequestPrinter->toString($request); |
101
|
|
|
$responseString = $this->psr7ResponsePrinter->toString($response); |
102
|
|
|
|
103
|
|
|
throw new \Exception( |
104
|
|
|
"{$e->getMessage()}\n\n$requestString\n\n\n$responseString\n", |
105
|
|
|
$e->getCode(), |
106
|
|
|
$e |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $response; |
112
|
|
|
} |
113
|
|
|
); |
114
|
|
|
}; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|