Skip to content

Commit

Permalink
Merge pull request #47 from jcchavezs/adds_curl_integration
Browse files Browse the repository at this point in the history
Adds cURL integration test.
  • Loading branch information
jcchavezs authored Jan 10, 2018
2 parents 44d2a4d + 2f22a05 commit 9605b62
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require-dev": {
"phpunit/phpunit": "~5.7.19",
"squizlabs/php_codesniffer": "3.*",
"guzzlehttp/psr7": "^1.4"
"guzzlehttp/psr7": "^1.4",
"jcchavezs/httptest": "0.1-beta2"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 4 additions & 0 deletions src/Zipkin/Reporters/Http/CurlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

final class CurlFactory implements ClientFactory
{
/**
* @return CurlFactory
* @throws \BadFunctionCallException if the curl extension is not installed.
*/
public static function create()
{
if (!function_exists('curl_init')) {
Expand Down
95 changes: 95 additions & 0 deletions tests/Integration/Reporters/Http/CurlFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace ZipkinTests\Integration\Reporters\Http;

use Exception;
use HttpTest\HttpTestServer;
use PHPUnit_Framework_TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Zipkin\Reporters\Http\CurlFactory;

final class CurlFactoryTest extends PHPUnit_Framework_TestCase
{
public function testHttpReportingSuccess()
{
$t = $this;

$server = HttpTestServer::create(
function (RequestInterface $request, ResponseInterface &$response) use ($t) {
$t->assertEquals('POST', $request->getMethod());
$t->assertEquals('application/json', $request->getHeader('Content-Type')[0]);
$response = $response->withStatus(202);
}
);

$pid = pcntl_fork();

if ($pid === -1) {
$this->fail('Error forking thread.');
} elseif ($pid) {
$server->start();
} else {
$server->waitForReady();

try {
$curlClient = CurlFactory::create()->build([
'endpoint_url' => $server->getUrl(),
]);

$curlClient(json_encode([]));
} catch (Exception $e) {
$this->fail($e->getMessage());
} finally {
$server->stop();
}
}
}

public function testHttpReportingFailsDueToInvalidStatusCode()
{
$server = HttpTestServer::create(
function (RequestInterface $request, ResponseInterface &$response) {
$response = $response->withStatus(404);
}
);

$pid = pcntl_fork();

if ($pid === -1) {
$this->fail('Error forking thread.');
} elseif ($pid) {
$server->start();
} else {
$server->waitForReady();

try {
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Reporting of spans failed');

$curlClient = CurlFactory::create()->build([
'endpoint_url' => $server->getUrl(),
]);

$curlClient('');

$this->fail('Runtime exception expected');
} finally {
$server->stop();
}
}
}

public function testHttpReportingFailsDueToUnreachableUrl()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Reporting of spans failed');

$curlClient = CurlFactory::create()->build([
'endpoint_url' => 'invalid_url',
]);

$curlClient('');
}
}

0 comments on commit 9605b62

Please sign in to comment.