-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathFormatterTest.php
More file actions
130 lines (102 loc) · 3.94 KB
/
FormatterTest.php
File metadata and controls
130 lines (102 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace Http\HttplugBundle\Tests\Unit\Collector;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Http\Client\Exception\HttpException;
use Http\Client\Exception\TransferException;
use Http\HttplugBundle\Collector\Formatter;
use Http\Message\Formatter as MessageFormatter;
use Http\Message\Formatter\CurlCommandFormatter;
use Http\Message\Formatter\SimpleFormatter;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
final class FormatterTest extends TestCase
{
private MessageFormatter&MockObject $formatter;
private CurlCommandFormatter&MockObject $curlFormatter;
private Formatter $subject;
public function setUp(): void
{
$this->formatter = $this->createMock(MessageFormatter::class);
$this->curlFormatter = $this->createMock(CurlCommandFormatter::class);
$this->subject = new Formatter($this->formatter, $this->curlFormatter);
}
public function testFormatRequest(): void
{
$request = new Request('GET', '/');
$this->formatter
->expects($this->once())
->method('formatRequest')
->with($this->identicalTo($request))
->willReturn('foo')
;
$this->assertSame('foo', $this->subject->formatRequest($request));
}
public function testFormatResponseForRequest(): void
{
$formatter = $this->createMock(SimpleFormatter::class);
$subject = new Formatter($formatter, $this->curlFormatter);
$response = new Response();
$request = new Request('GET', '/');
$formatter
->expects($this->once())
->method('formatResponseForRequest')
->with($this->identicalTo($response), $this->identicalTo($request))
->willReturn('foo')
;
$this->assertSame('foo', $subject->formatResponseForRequest($response, $request));
}
/**
* @group legacy
*/
#[Group('legacy')]
public function testFormatResponse(): void
{
$response = new Response();
$this->formatter
->expects($this->once())
->method('formatResponse')
->with($this->identicalTo($response))
->willReturn('foo')
;
$this->assertSame('foo', $this->subject->formatResponse($response));
}
public function testFormatHttpException(): void
{
$formatter = $this->createMock(SimpleFormatter::class);
$subject = new Formatter($formatter, $this->curlFormatter);
$request = new Request('GET', '/');
$response = new Response();
$exception = new HttpException('', $request, $response);
$formatter
->expects($this->once())
->method('formatResponseForRequest')
->with($this->identicalTo($response), $this->identicalTo($request))
->willReturn('FormattedException')
;
$this->assertEquals('FormattedException', $subject->formatException($exception));
}
public function testFormatTransferException(): void
{
$exception = new TransferException('ExceptionMessage');
$this->assertEquals('Transfer error: ExceptionMessage', $this->subject->formatException($exception));
}
public function testFormatException(): void
{
$exception = new \RuntimeException('Unexpected error');
$this->assertEquals('Unexpected exception of type "RuntimeException": Unexpected error', $this->subject->formatException($exception));
}
public function testFormatAsCurlCommand(): void
{
$request = new Request('GET', '/');
$this->curlFormatter
->expects($this->once())
->method('formatRequest')
->with($this->identicalTo($request))
->willReturn('curl -L http://example.com')
;
$this->assertEquals('curl -L http://example.com', $this->subject->formatAsCurlCommand($request));
}
}