Skip to content

Commit

Permalink
Improve the tests for the TraceableResponse class
Browse files Browse the repository at this point in the history
  • Loading branch information
ste93cry committed Jul 7, 2022
1 parent c6d5f2b commit 8a824d7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 88 deletions.
20 changes: 10 additions & 10 deletions src/Tracing/HttpClient/AbstractTraceableResponse.php
Expand Up @@ -37,16 +37,6 @@ public function __construct(HttpClientInterface $client, ResponseInterface $resp
$this->span = $span;
}

public function __sleep(): array
{
throw new \BadMethodCallException('Cannot serialize ' . __CLASS__);
}

public function __wakeup()
{
throw new \BadMethodCallException('Cannot unserialize ' . __CLASS__);
}

public function __destruct()
{
try {
Expand All @@ -58,6 +48,16 @@ public function __destruct()
}
}

public function __sleep(): array
{
throw new \BadMethodCallException('Serializing instances of this class is forbidden.');
}

public function __wakeup()
{
throw new \BadMethodCallException('Unserializing instances of this class is forbidden.');
}

public function getStatusCode(): int
{
return $this->response->getStatusCode();
Expand Down

This file was deleted.

110 changes: 44 additions & 66 deletions tests/Tracing/HttpClient/TraceableResponseTest.php
Expand Up @@ -6,23 +6,19 @@

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\Tests\Tracing\HttpClient\Fixtures\DestructibleResponseInterface;
use Sentry\SentryBundle\Tests\Tracing\HttpClient\Fixtures\StreamableResponseInterface;
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&ResponseInterface
*/
private $mockedResponse;

/**
* @var MockObject&HttpClientInterface
*/
Expand All @@ -31,30 +27,27 @@ final class TraceableResponseTest extends TestCase
/**
* @var MockObject&HubInterface
*/
protected $hub;

/**
* @var TraceableResponse
*/
private $response;
private $hub;

protected function setUp(): void
{
$this->mockedResponse = $this->createMock(ResponseInterface::class);
$this->client = $this->createMock(HttpClientInterface::class);
$this->hub = $this->createMock(HubInterface::class);
$this->response = new TraceableResponse($this->client, $this->mockedResponse, null);
}

public function testCannotBeSerialized(): void
public function testInstanceCannotBeSerialized(): void
{
$this->expectException(\BadMethodCallException::class);
serialize($this->response);
$this->expectExceptionMessage('Serializing instances of this class is forbidden.');

serialize(new TraceableResponse($this->client, new MockResponse(), null));
}

public function testCannotBeDeserialized(): void
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));
}

Expand All @@ -63,94 +56,79 @@ public function testDestructor(): void
$transaction = new Transaction(new TransactionContext(), $this->hub);
$context = new SpanContext();
$span = $transaction->startChild($context);

$this->mockedResponse = $this->createMock(DestructibleResponseInterface::class);
$this->mockedResponse
->expects($this->once())
->method('__destruct');

$this->response = new TraceableResponse($this->client, $this->mockedResponse, $span);
$response = new TraceableResponse($this->client, new MockResponse(), $span);

// Call gc to invoke destructors at the right time.
unset($this->response);
unset($response);

gc_mem_caches();
gc_collect_cycles();

$this->assertNotNull($span->getEndTimestamp());
}

public function testGetHeaders(): void
public function testGetStatusCode(): void
{
$this->mockedResponse
->expects($this->once())
->method('getHeaders')
->with(true);
$response = new TraceableResponse($this->client, new MockResponse(), null);

$this->response->getHeaders(true);
$this->assertSame(200, $response->getStatusCode());
}

public function testGetStatusCode(): void
public function testGetHeaders(): void
{
$this->mockedResponse
->expects($this->once())
->method('getStatusCode');
$expectedHeaders = ['content-length' => ['0']];
$response = new TraceableResponse($this->client, new MockResponse('', ['response_headers' => $expectedHeaders]), null);

$this->response->getStatusCode();
$this->assertSame($expectedHeaders, $response->getHeaders());
}

public function testGetContent(): void
{
$this->mockedResponse
->expects($this->once())
->method('getContent')
->with(false);
$span = new Span();
$httpClient = new MockHttpClient(new MockResponse('foobar'));
$response = new TraceableResponse($httpClient, $httpClient->request('GET', '/'), $span);

$this->response->getContent(false);
$this->assertSame('foobar', $response->getContent());
$this->assertNotNull($span->getEndTimestamp());
}

public function testToArray(): void
{
$this->mockedResponse
->expects($this->once())
->method('toArray')
->with(false);
$span = new Span();
$httpClient = new MockHttpClient(new MockResponse('{"foo":"bar"}'));
$response = new TraceableResponse($this->client, $httpClient->request('GET', '/'), $span);

$this->response->toArray(false);
$this->assertSame(['foo' => 'bar'], $response->toArray());
$this->assertNotNull($span->getEndTimestamp());
}

public function testCancel(): void
{
$this->mockedResponse
->expects($this->once())
->method('cancel');
$span = new Span();
$response = new TraceableResponse($this->client, new MockResponse(), $span);

$response->cancel();

$this->response->cancel();
$this->assertTrue($response->getInfo('canceled'));
$this->assertNotNull($span->getEndTimestamp());
}

public function testGetInfo(): void
{
$this->mockedResponse
->expects($this->once())
->method('getInfo')
->with('type');
$response = new TraceableResponse($this->client, new MockResponse(), null);

$this->response->getInfo('type');
$this->assertSame(200, $response->getInfo('http_code'));
}

public function testToStream(): void
{
if (!method_exists($this->response, 'toStream')) {
self::markTestSkipped('Response toStream method is not existent in this version of http-client');
}
$httpClient = new MockHttpClient(new MockResponse('foobar'));
$response = new TraceableResponse($this->client, $httpClient->request('GET', '/'), null);

$this->mockedResponse = $this->createMock(StreamableResponseInterface::class);
$this->mockedResponse
->expects($this->once())
->method('toStream')
->with(false);
if (!method_exists($response, 'toStream')) {
$this->markTestSkipped('The TraceableResponse::toStream() method is not supported');
}

$this->response = new TraceableResponse($this->client, $this->mockedResponse, null);
$this->response->toStream(false);
$this->assertSame('foobar', stream_get_contents($response->toStream()));
}
}

0 comments on commit 8a824d7

Please sign in to comment.