|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Pitchart\Phlunit\Exporter; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use SebastianBergmann\Exporter\Exporter; |
|
8
|
|
|
|
|
9
|
|
|
class HttpResponseExporter extends Exporter |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var bool |
|
13
|
|
|
*/ |
|
14
|
|
|
private $withHeaders = false; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var bool |
|
18
|
|
|
*/ |
|
19
|
|
|
private $withContent = false; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* HttpResponseExporter constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param bool $withHeaders |
|
25
|
|
|
* @param bool $withContent |
|
26
|
|
|
*/ |
|
27
|
3 |
|
public function __construct(bool $withHeaders = false, bool $withContent = false) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$this->withHeaders = $withHeaders; |
|
30
|
3 |
|
$this->withContent = $withContent; |
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ResponseInterface $value |
|
35
|
|
|
* @param int $indentation |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public function export($value, $indentation = 0) |
|
40
|
|
|
{ |
|
41
|
3 |
|
$content = parent::shortenedExport($value); |
|
42
|
3 |
|
$replace = ''; |
|
43
|
3 |
|
if ($this->withHeaders || $this->withContent) { |
|
44
|
3 |
|
$properties = parent::toArray($value); |
|
45
|
3 |
|
$whitespace = \str_repeat(' ', (int) (4 * ($indentation + 1))); |
|
46
|
|
|
/** @var array<string, array> $properties */ |
|
47
|
3 |
|
if (isset($properties['headers']) && \count($properties['headers'])) { |
|
48
|
3 |
|
$replace = "\n$whitespace'headers' (\n"; |
|
49
|
|
|
/** |
|
50
|
|
|
* @var string $name |
|
51
|
|
|
* @var array $values |
|
52
|
|
|
*/ |
|
53
|
3 |
|
foreach ($properties['headers'] as $name => $values) { |
|
54
|
3 |
|
$replace .= \sprintf("%s%s%s: %s\n", $whitespace, $whitespace, $name, \implode(', ', $values)); |
|
55
|
|
|
} |
|
56
|
3 |
|
$replace .= "$whitespace)\n"; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
return \preg_replace('/\.\.\./', $replace, $content); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|