diff --git a/lib/MC/Google/Visualization.php b/lib/MC/Google/Visualization.php index d434e43..639310f 100644 --- a/lib/MC/Google/Visualization.php +++ b/lib/MC/Google/Visualization.php @@ -144,9 +144,13 @@ public function setDefaultFormat($type, $format) /** * Handle the entire request, pulling the query from the $_GET variables, and printing the results directly. * + * @param bool $echo print response and set header + * * @throws Visualization_Error + * + * @return string the javascript response */ - public function handleRequest() + public function handleRequest($echo = true) { $query = $_GET['tq']; $params = ['version' => $this->version, 'responseHandler' => 'google.visualization.Query.setResponse']; @@ -166,8 +170,14 @@ public function handleRequest() $params['responseHandler'] = $_GET['responseHandler']; } - @header('Content-Type: text/javascript; charset=utf-8'); - $this->handleQuery($query, $params); + $response = $this->handleQuery($query, $params); + + if ($echo) { + header('Content-Type: text/javascript; charset=utf-8'); + echo $response; + } + + return $response; } /** @@ -175,10 +185,13 @@ public function handleRequest() * * @param string $query the visualization query to parse and execute * @param array $params all extra params sent along with the query - must include at least "reqId" key + * + * @return string the javascript response */ public function handleQuery($query, $params) { $reqid = null; + $response = ''; try { if (!($this->db instanceof PDO)) { @@ -194,26 +207,28 @@ public function handleQuery($query, $params) $stmt = $this->db->query($sql); //If we got here, there's no errors - echo $this->getSuccessInit($meta); + $response .= $this->getSuccessInit($meta); $first = true; foreach ($stmt as $row) { if (!$first) { - echo ','; + $response .= ','; } - echo $this->getRowValues($row, $meta); + $response .= $this->getRowValues($row, $meta); $first = false; } - echo $this->getSuccessClose(); + $response .= $this->getSuccessClose(); } catch (Visualization_Error $e) { - echo $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], $e->type, $e->summary); + $response .= $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], $e->type, $e->summary); } catch (PDOException $e) { - echo $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], 'invalid_query', 'Invalid Query - PDO exception'); + $response .= $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], 'invalid_query', 'Invalid Query - PDO exception'); } catch (ParseError $e) { - echo $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], 'invalid_query', 'Invalid Query - Parse Error'); + $response .= $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], 'invalid_query', 'Invalid Query - Parse Error'); } catch (Exception $e) { - echo $this->handleError($reqid, $e->getMessage(), $params['responseHandler']); + $response .= $this->handleError($reqid, $e->getMessage(), $params['responseHandler']); } + + return $response; } /** diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index c9de125..bfccc35 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -50,9 +50,7 @@ public function testQueryComplete() $_GET['tq'] = 'select country, year, birth_control, infant_mort where birth_control!=0 AND infant_mort!=0 group by country, year label country "Country", year "Year", birth_control "Birth Control Penetration", gdp_us "Per-capita GDP (US Dollars)", savings_rate "Savings Rate", investment_rate "Investment Rate", infant_mort "Infant Mortality", life_expect "Life Expectancy" format year "%d"'; $_GET['tqx'] = 'reqId:1'; - ob_start(); - $vis->handleRequest(); - $output = ob_get_clean(); + $output = $vis->handleRequest(false); //file_put_contents(__DIR__.'/result1.js', $output); static::assertStringEqualsFile(__DIR__.'/result1.js', $output); @@ -75,9 +73,7 @@ public function testQuerySimple() $_GET['tq'] = 'select id, name from countries order by name label id "ID", name "Name"'; $_GET['tqx'] = 'reqId:2'; - ob_start(); - $vis->handleRequest(); - $output = ob_get_clean(); + $output = $vis->handleRequest(false); //file_put_contents(__DIR__.'/result2.js', $output); static::assertStringEqualsFile(__DIR__.'/result2.js', $output); @@ -110,9 +106,7 @@ public function testQueryJoins() $_GET['tq'] = 'select avg(life_male), avg(life_female), avg(life_both) from countries label life_male "Life Expectancy (Male)", life_female "Life Expectancy (Female)", life_both "Life Expectancy (Combined)" format life_male "%.2f years", life_female "%.2f years", life_both "%.2f years"'; $_GET['tqx'] = 'reqId:3'; - ob_start(); - $vis->handleRequest(); - $output = ob_get_clean(); + $output = $vis->handleRequest(false); //file_put_contents(__DIR__.'/result3.js', $output); static::assertStringEqualsFile(__DIR__.'/result3.js', $output);