|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Http\HttplugBundle\Tests\Unit\Collector; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Request; |
|
8
|
|
|
use GuzzleHttp\Psr7\Response; |
|
9
|
|
|
use Http\Client\Exception\HttpException; |
|
10
|
|
|
use Http\Client\Exception\TransferException; |
|
11
|
|
|
use Http\HttplugBundle\Collector\Formatter; |
|
12
|
|
|
use Http\Message\Formatter as MessageFormatter; |
|
13
|
|
|
use Http\Message\Formatter\CurlCommandFormatter; |
|
14
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
class FormatterTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var MessageFormatter|MockObject |
|
21
|
|
|
*/ |
|
22
|
|
|
private $formatter; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var CurlCommandFormatter|MockObject |
|
26
|
|
|
*/ |
|
27
|
|
|
private $curlFormatter; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Formatter |
|
31
|
|
|
*/ |
|
32
|
|
|
private $subject; |
|
33
|
|
|
|
|
34
|
|
|
public function setUp(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->formatter = $this->getMockBuilder(MessageFormatter::class)->getMock(); |
|
37
|
|
|
$this->curlFormatter = $this->getMockBuilder(CurlCommandFormatter::class)->getMock(); |
|
38
|
|
|
|
|
39
|
|
|
$this->subject = new Formatter($this->formatter, $this->curlFormatter); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testFormatRequest(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$request = new Request('GET', '/'); |
|
45
|
|
|
|
|
46
|
|
|
$this->formatter |
|
|
|
|
|
|
47
|
|
|
->expects($this->once()) |
|
48
|
|
|
->method('formatRequest') |
|
49
|
|
|
->with($this->identicalTo($request)) |
|
50
|
|
|
; |
|
51
|
|
|
|
|
52
|
|
|
$this->subject->formatRequest($request); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testFormatResponse(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$response = new Response(); |
|
58
|
|
|
|
|
59
|
|
|
$this->formatter |
|
|
|
|
|
|
60
|
|
|
->expects($this->once()) |
|
61
|
|
|
->method('formatResponse') |
|
62
|
|
|
->with($this->identicalTo($response)) |
|
63
|
|
|
; |
|
64
|
|
|
|
|
65
|
|
|
$this->subject->formatResponse($response); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testFormatHttpException(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$request = new Request('GET', '/'); |
|
71
|
|
|
$response = new Response(); |
|
72
|
|
|
$exception = new HttpException('', $request, $response); |
|
73
|
|
|
|
|
74
|
|
|
$this->formatter |
|
|
|
|
|
|
75
|
|
|
->expects($this->once()) |
|
76
|
|
|
->method('formatResponse') |
|
77
|
|
|
->with($this->identicalTo($response)) |
|
78
|
|
|
->willReturn('FormattedException') |
|
79
|
|
|
; |
|
80
|
|
|
|
|
81
|
|
|
$this->assertEquals('FormattedException', $this->subject->formatException($exception)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testFormatTransferException(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$exception = new TransferException('ExceptionMessage'); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertEquals('Transfer error: ExceptionMessage', $this->subject->formatException($exception)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testFormatException(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$exception = new \RuntimeException('Unexpected error'); |
|
94
|
|
|
$this->assertEquals('Unexpected exception of type "RuntimeException": Unexpected error', $this->subject->formatException($exception)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testFormatAsCurlCommand(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$request = new Request('GET', '/'); |
|
100
|
|
|
|
|
101
|
|
|
$this->curlFormatter |
|
|
|
|
|
|
102
|
|
|
->expects($this->once()) |
|
103
|
|
|
->method('formatRequest') |
|
104
|
|
|
->with($this->identicalTo($request)) |
|
105
|
|
|
->willReturn('curl -L http://example.com') |
|
106
|
|
|
; |
|
107
|
|
|
|
|
108
|
|
|
$this->assertEquals('curl -L http://example.com', $this->subject->formatAsCurlCommand($request)); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: