2
2
3
3
namespace FPM ;
4
4
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
6
60
{
7
61
const HEADER_SEPARATOR = "\r\n\r\n" ;
8
62
9
63
/**
10
64
* @var array
11
65
*/
12
- private $ data ;
66
+ private array $ data ;
13
67
14
68
/**
15
69
* @var string
@@ -39,19 +93,17 @@ class Response
39
93
/**
40
94
* @var bool
41
95
*/
42
- private $ expectInvalid ;
43
-
44
- /**
45
- * @var bool
46
- */
47
- private bool $ debugOutputted = false ;
96
+ private bool $ expectInvalid ;
48
97
49
98
/**
99
+ * @param Tester $tester
50
100
* @param string|array|null $data
51
101
* @param bool $expectInvalid
52
102
*/
53
- public function __construct ($ data = null , $ expectInvalid = false )
103
+ public function __construct (Tester $ tester , $ data = null , bool $ expectInvalid = false )
54
104
{
105
+ parent ::__construct ($ tester );
106
+
55
107
if ( ! is_array ($ data )) {
56
108
$ data = [
57
109
'response ' => $ data ,
@@ -105,7 +157,7 @@ class Response
105
157
*
106
158
* @return Response
107
159
*/
108
- public function expectJsonBodyPatternForStatusProcessField (string $ fieldName , string $ pattern )
160
+ public function expectJsonBodyPatternForStatusProcessField (string $ fieldName , string $ pattern ): Response
109
161
{
110
162
$ rawData = $ this ->getBody ('application/json ' );
111
163
$ data = json_decode ($ rawData , true );
@@ -270,7 +322,7 @@ class Response
270
322
/**
271
323
* Debug response output
272
324
*/
273
- public function debugOutput ()
325
+ public function debugOutput (): void
274
326
{
275
327
echo ">>> Response \n" ;
276
328
echo "----------------- OUT ----------------- \n" ;
@@ -416,37 +468,24 @@ class Response
416
468
);
417
469
}
418
470
}
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
- }
436
471
}
437
472
438
- class ValuesResponse
473
+ class ValuesResponse extends BaseResponse
439
474
{
440
475
/**
441
476
* @var array
442
477
*/
443
478
private array $ values ;
444
479
445
480
/**
481
+ * @param Tester $tester
446
482
* @param string|array|null $values
483
+ * @throws \Exception
447
484
*/
448
- public function __construct ($ values = null )
485
+ public function __construct (Tester $ tester , $ values = null )
449
486
{
487
+ parent ::__construct ($ tester );
488
+
450
489
if ( ! is_array ($ values )) {
451
490
if ( ! is_null ($ values ) ) {
452
491
$ this ->error ('Invalid values supplied ' , true );
@@ -463,14 +502,15 @@ class ValuesResponse
463
502
* @param string $name
464
503
* @param mixed $value
465
504
* @return ValuesResponse
505
+ * @throws \Exception
466
506
*/
467
507
public function expectValue (string $ name , $ value = null )
468
508
{
469
509
if ( ! isset ($ this ->values [$ name ])) {
470
- return $ this ->error ("Value $ name not found in values " );
510
+ $ this ->error ("Value $ name not found in values " );
471
511
}
472
512
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 " );
474
514
}
475
515
return $ this ;
476
516
}
@@ -487,36 +527,12 @@ class ValuesResponse
487
527
488
528
/**
489
529
* Debug output data.
490
- *
491
- * @return ValuesResponse
492
530
*/
493
- public function debugOutput ()
531
+ public function debugOutput (): void
494
532
{
495
533
echo ">>> ValuesResponse \n" ;
496
534
echo "----------------- Values ----------------- \n" ;
497
535
var_dump ($ this ->values );
498
536
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 ;
521
537
}
522
538
}
0 commit comments