1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace VonageTest ;
6
+
7
+ use PHPUnit \Framework \ExpectationFailedException ;
8
+ use Psr \Http \Message \RequestInterface ;
9
+
10
+ use function http_build_query ;
11
+ use function is_array ;
12
+ use function json_decode ;
13
+ use function json_encode ;
14
+ use function parse_str ;
15
+
16
+ trait Psr7AssertionTrait
17
+ {
18
+ /**
19
+ * @param $expected
20
+ * @param RequestInterface $request
21
+ */
22
+ public static function assertRequestMethod ($ expected , RequestInterface $ request ): void
23
+ {
24
+ self ::assertEquals ($ expected , $ request ->getMethod ());
25
+ }
26
+
27
+ public static function assertRequestBodyIsEmpty (RequestInterface $ request ): void
28
+ {
29
+ $ request ->getBody ()->rewind ();
30
+ $ body = $ request ->getBody ()->getContents ();
31
+ $ request ->getBody ()->rewind ();
32
+
33
+ self ::assertEmpty ($ body );
34
+ }
35
+
36
+ /**
37
+ * @param $expected
38
+ * @param RequestInterface $request
39
+ */
40
+ public static function assertRequestBodyIsJson ($ expected , RequestInterface $ request ): void
41
+ {
42
+ $ request ->getBody ()->rewind ();
43
+ $ body = $ request ->getBody ()->getContents ();
44
+ $ request ->getBody ()->rewind ();
45
+
46
+ self ::assertJsonStringEqualsJsonString ($ expected , $ body );
47
+ }
48
+
49
+ /**
50
+ * @param $host
51
+ * @param $path
52
+ * @param $method
53
+ * @param RequestInterface $request
54
+ */
55
+ public static function assertRequestUrl ($ host , $ path , $ method , RequestInterface $ request ): void
56
+ {
57
+ self ::assertEquals ($ host , $ request ->getUri ()->getHost ());
58
+ self ::assertEquals ($ path , $ request ->getUri ()->getPath ());
59
+ self ::assertEquals ($ method , $ request ->getMethod ());
60
+ }
61
+
62
+ /**
63
+ * @param $key
64
+ * @param RequestInterface $request
65
+ */
66
+ public static function assertRequestQueryNotContains ($ key , RequestInterface $ request ): void
67
+ {
68
+ $ query = $ request ->getUri ()->getQuery ();
69
+ $ params = [];
70
+ parse_str ($ query , $ params );
71
+
72
+ self ::assertArrayNotHasKey ($ key , $ params , 'query string has key when it should not: ' . $ key );
73
+ }
74
+
75
+ /**
76
+ * @param $key
77
+ * @param $value
78
+ * @param RequestInterface $request
79
+ */
80
+ public static function assertRequestQueryContains ($ key , $ value , RequestInterface $ request ): void
81
+ {
82
+ $ query = $ request ->getUri ()->getQuery ();
83
+ $ params = [];
84
+ parse_str ($ query , $ params );
85
+
86
+ self ::assertArrayHasKey ($ key , $ params , 'query string does not have key: ' . $ key );
87
+
88
+ $ errorValue = $ value ;
89
+
90
+ if (is_array ($ errorValue )) {
91
+ $ errorValue = json_encode ($ errorValue );
92
+ }
93
+
94
+ self ::assertSame ($ value , $ params [$ key ], 'query string does not have value: ' . $ errorValue );
95
+ }
96
+
97
+ /**
98
+ * @param $key
99
+ * @param RequestInterface $request
100
+ */
101
+ public static function assertRequestQueryHas ($ key , RequestInterface $ request ): void
102
+ {
103
+ $ query = $ request ->getUri ()->getQuery ();
104
+ $ params = [];
105
+ parse_str ($ query , $ params );
106
+ self ::assertArrayHasKey ($ key , $ params , 'query string does not have key: ' . $ key );
107
+ }
108
+
109
+ /**
110
+ * @param $key
111
+ * @param $value
112
+ * @param RequestInterface $request
113
+ */
114
+ public static function assertRequestFormBodyContains ($ key , $ value , RequestInterface $ request ): void
115
+ {
116
+ self ::assertEquals (
117
+ 'application/x-www-form-urlencoded ' ,
118
+ $ request ->getHeaderLine ('content-type ' ),
119
+ 'incorrect `Content-Type` for POST body '
120
+ );
121
+
122
+ $ request ->getBody ()->rewind ();
123
+ $ data = $ request ->getBody ()->getContents ();
124
+ $ params = [];
125
+ parse_str ($ data , $ params );
126
+
127
+ self ::assertArrayHasKey ($ key , $ params , 'body does not have key: ' . $ key );
128
+ self ::assertSame ($ value , $ params [$ key ], 'body does not have value: ' . $ value );
129
+ }
130
+
131
+ public static function assertRequestJsonBodyContains (
132
+ $ key ,
133
+ $ value ,
134
+ RequestInterface $ request ,
135
+ bool $ nested = false
136
+ ): void
137
+ {
138
+ self ::assertEquals (
139
+ 'application/json ' ,
140
+ $ request ->getHeaderLine ('content-type ' ),
141
+ 'incorrect `Content-Type` for JSON body '
142
+ );
143
+
144
+ $ request ->getBody ()->rewind ();
145
+ $ params = json_decode ($ request ->getBody ()->getContents (), true );
146
+
147
+ if (!$ nested ) {
148
+ self ::assertArrayHasKey ($ key , $ params , 'body does not have key: ' . $ key );
149
+ self ::assertSame ($ value , $ params [$ key ]);
150
+
151
+ return ;
152
+ }
153
+
154
+ try {
155
+ $ keyValue = self ::findNestedKey ($ params , $ key , $ value );
156
+ self ::assertSame ($ value , $ keyValue );
157
+ } catch (\OutOfBoundsException $ e ) {
158
+ throw new ExpectationFailedException ('Body does not have nested key: ' . $ key );
159
+ }
160
+ }
161
+
162
+ protected static function findNestedKey (array $ params , string $ searchKey , mixed $ searchValue )
163
+ {
164
+ $ iterator = new \RecursiveIteratorIterator (new \RecursiveArrayIterator ($ params ));
165
+
166
+ while ($ iterator ->valid ()) {
167
+ if ($ iterator ->key () === $ searchKey && $ iterator ->current () === $ searchValue ) {
168
+ return $ iterator ->current ();
169
+ }
170
+
171
+ $ iterator ->next ();
172
+ }
173
+
174
+ throw new \OutOfBoundsException ('Cannot find given Key ' );
175
+ }
176
+
177
+ /**
178
+ * @param $key
179
+ * @param $value
180
+ * @param RequestInterface $request
181
+ */
182
+ public static function assertRequestJsonBodyMissing ($ key , RequestInterface $ request ): void
183
+ {
184
+ self ::assertEquals (
185
+ 'application/json ' ,
186
+ $ request ->getHeaderLine ('content-type ' ),
187
+ 'incorrect `Content-Type` for JSON body '
188
+ );
189
+
190
+ $ request ->getBody ()->rewind ();
191
+ $ params = json_decode ($ request ->getBody ()->getContents (), true );
192
+
193
+ self ::assertArrayNotHasKey ($ key , $ params , 'body does not have key: ' . $ key );
194
+ }
195
+
196
+ /**
197
+ * @param $url
198
+ * @param RequestInterface $request
199
+ */
200
+ public static function assertRequestMatchesUrl ($ url , RequestInterface $ request ): void
201
+ {
202
+ self ::assertEquals ($ url , $ request ->getUri ()->withQuery ('' )->__toString (), 'url did not match request ' );
203
+ }
204
+
205
+ /**
206
+ * @param $url
207
+ * @param RequestInterface $request
208
+ */
209
+ public static function assertRequestMatchesUrlWithQueryString ($ url , RequestInterface $ request ): void
210
+ {
211
+ $ query = [];
212
+
213
+ parse_str ($ request ->getUri ()->getQuery (), $ query );
214
+
215
+ unset($ query ['api_key ' ], $ query ['api_secret ' ]);
216
+
217
+ $ query = http_build_query ($ query );
218
+
219
+ self ::assertEquals ($ url , $ request ->getUri ()->withQuery ($ query )->__toString (), 'url did not match request ' );
220
+ }
221
+ }
0 commit comments