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 );
@@ -270,7 +322,7 @@ class Response
270322 /**
271323 * Debug response output
272324 */
273- public function debugOutput ()
325+ public function debugOutput (): void
274326 {
275327 echo ">>> Response \n" ;
276328 echo "----------------- OUT ----------------- \n" ;
@@ -416,37 +468,24 @@ class Response
416468 );
417469 }
418470 }
419-
420- /**
421- * Emit error message
422- *
423- * @param string $message
424- *
425- * @return bool
426- */
427- private function error (string $ message ): bool
428- {
429- if ( ! $ this ->debugOutputted ) {
430- $ this ->debugOutput ();
431- }
432- echo "ERROR: $ message \n" ;
433-
434- return false ;
435- }
436471}
437472
438- class ValuesResponse
473+ class ValuesResponse extends BaseResponse
439474{
440475 /**
441476 * @var array
442477 */
443478 private array $ values ;
444479
445480 /**
481+ * @param Tester $tester
446482 * @param string|array|null $values
483+ * @throws \Exception
447484 */
448- public function __construct ($ values = null )
485+ public function __construct (Tester $ tester , $ values = null )
449486 {
487+ parent ::__construct ($ tester );
488+
450489 if ( ! is_array ($ values )) {
451490 if ( ! is_null ($ values ) ) {
452491 $ this ->error ('Invalid values supplied ' , true );
@@ -463,14 +502,15 @@ class ValuesResponse
463502 * @param string $name
464503 * @param mixed $value
465504 * @return ValuesResponse
505+ * @throws \Exception
466506 */
467507 public function expectValue (string $ name , $ value = null )
468508 {
469509 if ( ! isset ($ this ->values [$ name ])) {
470- return $ this ->error ("Value $ name not found in values " );
510+ $ this ->error ("Value $ name not found in values " );
471511 }
472512 if ( ! is_null ($ value ) && $ value !== $ this ->values [$ name ]) {
473- return $ this ->error ("Value $ name is {$ this ->values [$ name ]} but expected $ value " );
513+ $ this ->error ("Value $ name is {$ this ->values [$ name ]} but expected $ value " );
474514 }
475515 return $ this ;
476516 }
@@ -487,36 +527,12 @@ class ValuesResponse
487527
488528 /**
489529 * Debug output data.
490- *
491- * @return ValuesResponse
492530 */
493- public function debugOutput ()
531+ public function debugOutput (): void
494532 {
495533 echo ">>> ValuesResponse \n" ;
496534 echo "----------------- Values ----------------- \n" ;
497535 var_dump ($ this ->values );
498536 echo "--------------------------------------- \n\n" ;
499-
500- return $ this ;
501- }
502-
503- /**
504- * Emit error message
505- *
506- * @param string $message
507- * @param bool $throw
508- *
509- * @return ValuesResponse
510- */
511- private function error (string $ message , $ throw = false ): bool
512- {
513- $ errorMessage = "ERROR: $ message \n" ;
514- if ($ throw ) {
515- throw new \Exception ($ errorMessage );
516- }
517- $ this ->debugOutput ();
518- echo $ errorMessage ;
519-
520- return $ this ;
521537 }
522538}
0 commit comments