Skip to content

Commit 62682cb

Browse files
committed
Add FPM tester logs printing for all errors
Closes GH-12902
1 parent 1b5a159 commit 62682cb

File tree

4 files changed

+158
-82
lines changed

4 files changed

+158
-82
lines changed

sapi/fpm/tests/logreader.inc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,11 @@ class LogReader
116116
*/
117117
public function printLogs(): void
118118
{
119+
if (empty($this->sources)) {
120+
return;
121+
}
119122
$hasMultipleDescriptors = count($this->sources) > 1;
120-
echo "LOGS:\n";
123+
echo "\nLOGS:\n";
121124
foreach ($this->sources as $name => $source) {
122125
if ($hasMultipleDescriptors) {
123126
echo ">>> source: $name\n";
@@ -128,6 +131,7 @@ class LogReader
128131
}
129132
$this->printSeparator();
130133
}
134+
echo "\n";
131135
}
132136

133137
/**
@@ -142,9 +146,8 @@ class LogReader
142146
if (is_null($errorMessage)) {
143147
return false;
144148
}
145-
echo "ERROR: " . $errorMessage . "\n\n";
149+
echo "ERROR: " . $errorMessage . "\n";
146150
$this->printLogs();
147-
echo "\n";
148151

149152
return false;
150153
}

sapi/fpm/tests/response.inc

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,68 @@
22

33
namespace 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
}

sapi/fpm/tests/status.inc

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@ class Status
3737
'slow requests' => '\d+',
3838
];
3939

40+
/**
41+
* @var Tester
42+
*/
43+
private Tester $tester;
44+
45+
/**
46+
* @param Tester $tester
47+
*/
48+
public function __construct(Tester $tester)
49+
{
50+
$this->tester = $tester;
51+
}
52+
53+
/**
54+
* @param string $body
55+
* @param string $pattern
56+
* @return void
57+
*/
58+
private function matchError(string $body, string $pattern): void
59+
{
60+
echo "ERROR: Expected body does not match pattern\n";
61+
echo "BODY:\n";
62+
var_dump($body);
63+
echo "PATTERN:\n";
64+
var_dump($pattern);
65+
$this->tester->printLogs();
66+
}
67+
4068
/**
4169
* Check status page.
4270
*
@@ -106,11 +134,7 @@ class Status
106134
$pattern .= $footer . ')';
107135

108136
if (!preg_match($pattern, $body)) {
109-
echo "ERROR: Expected body does not match pattern\n";
110-
echo "BODY:\n";
111-
var_dump($body);
112-
echo "PATTERN:\n";
113-
var_dump($pattern);
137+
$this->matchError($body, $pattern);
114138
}
115139
}
116140

@@ -245,11 +269,7 @@ class Status
245269
"# EOF)\n";
246270

247271
if (!preg_match($pattern, $body)) {
248-
echo "ERROR: Expected body does not match pattern\n";
249-
echo "BODY:\n";
250-
var_dump($body);
251-
echo "PATTERN:\n";
252-
var_dump($pattern);
272+
$this->matchError($body, $pattern);
253273
}
254274
}
255275
}

0 commit comments

Comments
 (0)