/
TraceableResponseTest.php
143 lines (111 loc) · 4.7 KB
/
TraceableResponseTest.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\Tests\Tracing\HttpClient;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\Tracing\HttpClient\TraceableResponse;
use Sentry\State\HubInterface;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
final class TraceableResponseTest extends TestCase
{
/**
* @var MockObject&HttpClientInterface
*/
private $client;
/**
* @var MockObject&HubInterface
*/
private $hub;
protected function setUp(): void
{
$this->client = $this->createMock(HttpClientInterface::class);
$this->hub = $this->createMock(HubInterface::class);
}
public function testInstanceCannotBeSerialized(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Serializing instances of this class is forbidden.');
serialize(new TraceableResponse($this->client, new MockResponse(), null));
}
public function testInstanceCannotBeUnserialized(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Unserializing instances of this class is forbidden.');
unserialize(sprintf('O:%u:"%s":0:{}', \strlen(TraceableResponse::class), TraceableResponse::class));
}
public function testDestructor(): void
{
$transaction = new Transaction(new TransactionContext(), $this->hub);
$context = new SpanContext();
$span = $transaction->startChild($context);
$response = new TraceableResponse($this->client, new MockResponse(), $span);
// Call gc to invoke destructors at the right time.
unset($response);
gc_mem_caches();
gc_collect_cycles();
$this->assertNotNull($span->getEndTimestamp());
}
public function testGetStatusCode(): void
{
$response = new TraceableResponse($this->client, new MockResponse(), null);
$this->assertSame(200, $response->getStatusCode());
}
public function testGetHeaders(): void
{
$expectedHeaders = ['content-length' => ['0']];
$response = new TraceableResponse($this->client, new MockResponse('', ['response_headers' => $expectedHeaders]), null);
$this->assertSame($expectedHeaders, $response->getHeaders());
}
public function testGetContent(): void
{
$span = new Span();
$httpClient = new MockHttpClient(new MockResponse('foobar'));
$response = new TraceableResponse($httpClient, $httpClient->request('GET', 'https://www.example.org/'), $span);
$this->assertSame('foobar', $response->getContent());
$this->assertNotNull($span->getEndTimestamp());
}
public function testToArray(): void
{
$span = new Span();
$httpClient = new MockHttpClient(new MockResponse('{"foo":"bar"}'));
$response = new TraceableResponse($this->client, $httpClient->request('GET', 'https://www.example.org/'), $span);
$this->assertSame(['foo' => 'bar'], $response->toArray());
$this->assertNotNull($span->getEndTimestamp());
}
public function testCancel(): void
{
$span = new Span();
$response = new TraceableResponse($this->client, new MockResponse(), $span);
$response->cancel();
$this->assertTrue($response->getInfo('canceled'));
$this->assertNotNull($span->getEndTimestamp());
}
public function testGetInfo(): void
{
$response = new TraceableResponse($this->client, new MockResponse(), null);
$this->assertSame(200, $response->getInfo('http_code'));
}
public function testToStream(): void
{
$httpClient = new MockHttpClient(new MockResponse('foobar'));
$response = new TraceableResponse($this->client, $httpClient->request('GET', 'https://www.example.org/'), null);
if (!method_exists($response, 'toStream')) {
$this->markTestSkipped('The TraceableResponse::toStream() method is not supported');
}
$this->assertSame('foobar', stream_get_contents($response->toStream()));
}
public function testStreamWithWrongObjectsShouldThrow(): void
{
$httpClient = new MockHttpClient(new MockResponse('foobar'));
$response = $this->createMock(ResponseInterface::class);
$this->expectException(\TypeError::class);
iterator_to_array(TraceableResponse::stream($httpClient, [$response], null));
}
}