22
33namespace FPM ;
44
5- class Response
5+ abstract class BaseResponse
6+ {
7+ /**
8+ * Tester instance
9+ * @var Tester
10+ */
11+ private Tester $ tester ;
12+
13+ /**
14+ * @var bool
15+ */
16+ protected bool $ debugOutputted = false ;
17+
18+ /**
19+ * @param Tester $tester
20+ */
21+ public function __construct (Tester $ tester )
22+ {
23+ $ this ->tester = $ tester ;
24+ }
25+
26+ /**
27+ * Debug response output.
28+ *
29+ * @return void
30+ */
31+ abstract function debugOutput (): void ;
32+
33+ /**
34+ * Emit error message
35+ *
36+ * @param string $message
37+ * @param bool $throw
38+ *
39+ * @return bool
40+ * @throws \Exception
41+ */
42+ protected function error (string $ message , bool $ throw = false ): bool
43+ {
44+ $ errorMessage = "ERROR: $ message \n" ;
45+ if ($ throw ) {
46+ throw new \Exception ($ errorMessage );
47+ }
48+ if ( ! $ this ->debugOutputted ) {
49+ $ this ->debugOutput ();
50+ }
51+ echo $ errorMessage ;
52+
53+ $ this ->tester ->printLogs ();
54+
55+ return false ;
56+ }
57+ }
58+
59+ class Response extends BaseResponse
660{
761 const HEADER_SEPARATOR = "\r\n\r\n" ;
862
963 /**
1064 * @var array
1165 */
12- private $ data ;
66+ private array $ data ;
1367
1468 /**
1569 * @var string
@@ -39,19 +93,17 @@ class Response
3993 /**
4094 * @var bool
4195 */
42- private $ expectInvalid ;
43-
44- /**
45- * @var bool
46- */
47- private bool $ debugOutputted = false ;
96+ private bool $ expectInvalid ;
4897
4998 /**
99+ * @param Tester $tester
50100 * @param string|array|null $data
51101 * @param bool $expectInvalid
52102 */
53- public function __construct ($ data = null , $ expectInvalid = false )
103+ public function __construct (Tester $ tester , $ data = null , bool $ expectInvalid = false )
54104 {
105+ parent ::__construct ($ tester );
106+
55107 if ( ! is_array ($ data )) {
56108 $ data = [
57109 'response ' => $ data ,
@@ -105,7 +157,7 @@ class Response
105157 *
106158 * @return Response
107159 */
108- public function expectJsonBodyPatternForStatusProcessField (string $ fieldName , string $ pattern )
160+ public function expectJsonBodyPatternForStatusProcessField (string $ fieldName , string $ pattern ): Response
109161 {
110162 $ rawData = $ this ->getBody ('application/json ' );
111163 $ data = json_decode ($ rawData , true );
@@ -281,7 +333,7 @@ class Response
281333 /**
282334 * Debug response output
283335 */
284- public function debugOutput ()
336+ public function debugOutput (): void
285337 {
286338 echo ">>> Response \n" ;
287339 echo "----------------- OUT ----------------- \n" ;
@@ -442,37 +494,24 @@ class Response
442494 );
443495 }
444496 }
445-
446- /**
447- * Emit error message
448- *
449- * @param string $message
450- *
451- * @return bool
452- */
453- private function error (string $ message ): bool
454- {
455- if ( ! $ this ->debugOutputted ) {
456- $ this ->debugOutput ();
457- }
458- echo "ERROR: $ message \n" ;
459-
460- return false ;
461- }
462497}
463498
464- class ValuesResponse
499+ class ValuesResponse extends BaseResponse
465500{
466501 /**
467502 * @var array
468503 */
469504 private array $ values ;
470505
471506 /**
507+ * @param Tester $tester
472508 * @param string|array|null $values
509+ * @throws \Exception
473510 */
474- public function __construct ($ values = null )
511+ public function __construct (Tester $ tester , $ values = null )
475512 {
513+ parent ::__construct ($ tester );
514+
476515 if ( ! is_array ($ values )) {
477516 if ( ! is_null ($ values ) ) {
478517 $ this ->error ('Invalid values supplied ' , true );
@@ -489,14 +528,15 @@ class ValuesResponse
489528 * @param string $name
490529 * @param mixed $value
491530 * @return ValuesResponse
531+ * @throws \Exception
492532 */
493533 public function expectValue (string $ name , $ value = null )
494534 {
495535 if ( ! isset ($ this ->values [$ name ])) {
496- return $ this ->error ("Value $ name not found in values " );
536+ $ this ->error ("Value $ name not found in values " );
497537 }
498538 if ( ! is_null ($ value ) && $ value !== $ this ->values [$ name ]) {
499- return $ this ->error ("Value $ name is {$ this ->values [$ name ]} but expected $ value " );
539+ $ this ->error ("Value $ name is {$ this ->values [$ name ]} but expected $ value " );
500540 }
501541 return $ this ;
502542 }
@@ -513,36 +553,12 @@ class ValuesResponse
513553
514554 /**
515555 * Debug output data.
516- *
517- * @return ValuesResponse
518556 */
519- public function debugOutput ()
557+ public function debugOutput (): void
520558 {
521559 echo ">>> ValuesResponse \n" ;
522560 echo "----------------- Values ----------------- \n" ;
523561 var_dump ($ this ->values );
524562 echo "--------------------------------------- \n\n" ;
525-
526- return $ this ;
527- }
528-
529- /**
530- * Emit error message
531- *
532- * @param string $message
533- * @param bool $throw
534- *
535- * @return ValuesResponse
536- */
537- private function error (string $ message , $ throw = false ): bool
538- {
539- $ errorMessage = "ERROR: $ message \n" ;
540- if ($ throw ) {
541- throw new \Exception ($ errorMessage );
542- }
543- $ this ->debugOutput ();
544- echo $ errorMessage ;
545-
546- return $ this ;
547563 }
548564}
0 commit comments