Skip to content

Commit

Permalink
add test for http-client stream method
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Jul 5, 2022
1 parent 39eaa7f commit 851f196
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Expand Up @@ -12,6 +12,7 @@ parameters:
- src/aliases.php
- src/Tracing/Doctrine/DBAL/TracingStatementForV2.php
- src/Tracing/Doctrine/DBAL/TracingDriverForV2.php
- src/Tracing/HttpClient/TraceableHttpClientForV4.php
- src/Tracing/HttpClient/TraceableHttpClientForV5.php
- tests/End2End/App
- tests/Tracing/Doctrine/DBAL/TracingDriverForV2Test.php
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Expand Up @@ -11,6 +11,7 @@
<directory name="src" />
<ignoreFiles>
<file name="src/Tracing/Doctrine/DBAL/TracingStatementForV2.php" />
<file name="src/Tracing/HttpClient/TraceableHttpClientForV4.php" />
<file name="src/Tracing/HttpClient/TraceableHttpClientForV5.php" />
<directory name="vendor" />
</ignoreFiles>
Expand Down
28 changes: 28 additions & 0 deletions tests/Tracing/HttpClient/TraceableHttpClientTest.php
Expand Up @@ -11,6 +11,7 @@
use Psr\Log\NullLogger;
use Sentry\SentryBundle\Tracing\HttpClient\AbstractTraceableResponse;
use Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClient;
use Sentry\SentryBundle\Tracing\HttpClient\TraceableResponse;
use Sentry\State\HubInterface;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
Expand Down Expand Up @@ -155,6 +156,33 @@ public function testWithOptions(): void
$this->assertSame('https://www.example.org/test-page', $response->getInfo('url'));
}

public function testStream(): void
{
if (!method_exists(MockHttpClient::class, 'stream')) {
self::markTestSkipped();
}

$transaction = new Transaction(new TransactionContext());
$transaction->initSpanRecorder();

$mockResponse = MockResponse::fromRequest('GET', 'https://www.example.com', [], new MockResponse('test_body', ['http_code' => 201]));
$decoratedHttpClient = new MockHttpClient([$mockResponse], 'https://www.example.com');
$httpClient = new TraceableHttpClient($decoratedHttpClient, $this->hub);
$response = new TraceableResponse($decoratedHttpClient, $mockResponse, $transaction);

$stream = $httpClient->stream([$response]);
$chunks = iterator_to_array($stream, false);

$this->assertCount(3, $chunks);

$spans = $transaction->getSpanRecorder()->getSpans();
$this->assertCount(1, $spans);
$this->assertNotNull($spans[0]->getEndTimestamp());

$this->assertEquals('test_body', $response->getContent(false));
$this->assertEquals(201, $response->getStatusCode());
}

private static function isHttpClientPackageInstalled(): bool
{
return interface_exists(HttpClientInterface::class);
Expand Down

0 comments on commit 851f196

Please sign in to comment.