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 );
@@ -281,7 +333,7 @@ class Response
281
333
/**
282
334
* Debug response output
283
335
*/
284
- public function debugOutput ()
336
+ public function debugOutput (): void
285
337
{
286
338
echo ">>> Response \n" ;
287
339
echo "----------------- OUT ----------------- \n" ;
@@ -442,37 +494,24 @@ class Response
442
494
);
443
495
}
444
496
}
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
- }
462
497
}
463
498
464
- class ValuesResponse
499
+ class ValuesResponse extends BaseResponse
465
500
{
466
501
/**
467
502
* @var array
468
503
*/
469
504
private array $ values ;
470
505
471
506
/**
507
+ * @param Tester $tester
472
508
* @param string|array|null $values
509
+ * @throws \Exception
473
510
*/
474
- public function __construct ($ values = null )
511
+ public function __construct (Tester $ tester , $ values = null )
475
512
{
513
+ parent ::__construct ($ tester );
514
+
476
515
if ( ! is_array ($ values )) {
477
516
if ( ! is_null ($ values ) ) {
478
517
$ this ->error ('Invalid values supplied ' , true );
@@ -489,14 +528,15 @@ class ValuesResponse
489
528
* @param string $name
490
529
* @param mixed $value
491
530
* @return ValuesResponse
531
+ * @throws \Exception
492
532
*/
493
533
public function expectValue (string $ name , $ value = null )
494
534
{
495
535
if ( ! isset ($ this ->values [$ name ])) {
496
- return $ this ->error ("Value $ name not found in values " );
536
+ $ this ->error ("Value $ name not found in values " );
497
537
}
498
538
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 " );
500
540
}
501
541
return $ this ;
502
542
}
@@ -513,36 +553,12 @@ class ValuesResponse
513
553
514
554
/**
515
555
* Debug output data.
516
- *
517
- * @return ValuesResponse
518
556
*/
519
- public function debugOutput ()
557
+ public function debugOutput (): void
520
558
{
521
559
echo ">>> ValuesResponse \n" ;
522
560
echo "----------------- Values ----------------- \n" ;
523
561
var_dump ($ this ->values );
524
562
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 ;
547
563
}
548
564
}
0 commit comments