Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"require": {
"php": ">=5.3",
"guzzle/guzzle": ">=3.0",
"ext-curl": "*",
"satooshi/php-coveralls": "dev-master",
"symfony/console": ">=2.0"
},
Expand Down
39 changes: 23 additions & 16 deletions src/CodeClimate/Bundle/TestReporterBundle/ApiClient.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<?php
namespace CodeClimate\Bundle\TestReporterBundle;

use Guzzle\Http\Client;
use Guzzle\Http\Exception\ClientErrorResponseException;

class ApiClient
{
protected $client;
protected $apiHost;

public function __construct()
{
$this->client = new Client();
$this->apiHost = "https://codeclimate.com";

if (isset($_SERVER["CODECLIMATE_API_HOST"])) {
Expand All @@ -22,18 +17,30 @@ public function __construct()

public function send($json)
{
$request = $this->client->createRequest('POST', $this->apiHost."/test_reports");
$response = false;

$request->setHeader("User-Agent", "Code Climate (PHP Test Reporter v".Version::VERSION.")");
$request->setHeader("Content-Type", "application/json");
$request->setBody($json);

try {
$response = $this->client->send($request);
} catch (ClientErrorResponseException $e) {
$response = $e->getResponse();
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_URL => $this->apiHost.'/test_reports',
CURLOPT_USERAGENT => 'Code Climate (PHP Test Reporter v'.Version::VERSION.')',
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => (string)$json,
));

$response = new \stdClass;
if ($raw_response = curl_exec($ch)) {
list($response->headers, $response->body) = explode("\r\n\r\n", $raw_response, 2);
$response->headers = explode("\r\n", $response->headers);
list(, $response->code, $response->message) = explode(' ', $response->headers[0], 3);
}
else {
$response->code = -curl_errno($ch);
$response->message = curl_error($ch);
$response->headers = array();
$response->body = NULL;
}
curl_close($ch);

return $response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
} else {
$client = new ApiClient();
$response = $client->send($json);
switch ($response->code) {
case 200:
$output->writeln("Test coverage data sent.");
break;

if ($response) {
$code = $response->getStatusCode();
case 401:
$output->writeln("Invalid CODECLIMATE_REPO_TOKEN.");
$ret = 1;
break;

switch ($code) {
case 200:
$output->writeln("Test coverage data sent");
break;
case 401:
$output->writeln("An invalid CODECLIMATE_REPO_TOKEN repo token was specified.");
$ret = 1;
break;
default:
$output->writeln("Status code: ".$code);
$ret = 1;
break;
}
} else {
$output->writeln("Unknown error posting Test coverage data.");
$ret = 1;
default:
$output->writeln("Unexpected response: ".$response->code." ".$response->message);
$output->writeln($response->body);
$ret = 1;
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing

PHP Parse error: syntax error, unexpected 'public' (T_PUBLIC) in /home/travis/build/codeclimate/php-test-reporter/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php on line 86

on Travis.

I think you're missing the closing brace for the switch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops. Sorry.

}
}

Expand Down