Skip to content

Commit

Permalink
Merge pull request #66 from toin0u/hotfix-loggable
Browse files Browse the repository at this point in the history
Hotfix loggable with multiple results
  • Loading branch information
willdurand committed Mar 15, 2014
2 parents 753c6b1 + 53a1aa7 commit b28fab6
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 30 deletions.
17 changes: 6 additions & 11 deletions Geocoder/LoggableGeocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public function geocode($value)
}

$startTime = microtime(true);
$result = parent::geocode($value);
$results = parent::geocode($value);
$duration = (microtime(true) - $startTime) * 1000;

$this->logger->logRequest(
sprintf("[Geocoding] %s", $value),
$duration,
$this->getProviderClass(),
json_encode($result->toArray())
$results
);

return $result;
return $results;
}

/**
Expand All @@ -62,19 +62,14 @@ public function reverse($latitude, $longitude)
}

$startTime = microtime(true);
$result = parent::reverse($latitude, $longitude);
$results = parent::reverse($latitude, $longitude);
$duration = (microtime(true) - $startTime) * 1000;

$value = sprintf("[Reverse geocoding] latitude: %s, longitude: %s", $latitude, $longitude);

$this->logger->logRequest(
$value,
$duration,
$this->getProviderClass(),
json_encode($result->toArray())
);
$this->logger->logRequest($value, $duration, $this->getProviderClass(), $results);

return $result;
return $results;
}

protected function getProviderClass()
Expand Down
44 changes: 26 additions & 18 deletions Logger/GeocoderLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
*/
class GeocoderLogger
{
/**
* @var LoggerInterface
*/
protected $logger;

/**
* @var array
*/
protected $requests = array();

/**
*
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger = null)
Expand All @@ -31,28 +36,31 @@ public function __construct(LoggerInterface $logger = null)
}

/**
*
* @param string $value value to geocode
* @param float $duration
* @param string $providerClass Geocoder provider class
* @param mixed $result
* @param string $value value to geocode
* @param float $duration geocoding duration
* @param string $providerClass Geocoder provider class name
* @param \SplObjectStorage|Geocoded $results
*/
public function logRequest($value, $duration, $providerClass, $result)
public function logRequest($value, $duration, $providerClass, $results)
{
if (null !== $this->logger) {
$this->requests[] = array(
'value' => $value,
'duration' => $duration,
'providerClass' => $providerClass,
'result' => $result
);
$this->logger->info(sprintf("%s %0.2f ms (%s)", $value, $duration, $providerClass));
}

$this->logger->info(sprintf("%s %0.2f ms (%s)",
$value,
$duration,
$providerClass
));
if ($results instanceof \SplObjectStorage) {
foreach ($results as $result) {
$data[] = $result->toArray();
}
} else {
$data = $results->toArray();
}

$this->requests[] = array(
'value' => $value,
'duration' => $duration,
'providerClass' => $providerClass,
'result' => json_encode($data),
);
}

/**
Expand Down
124 changes: 124 additions & 0 deletions Tests/Logger/GeocoderLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/**
* This file is part of the BazingaGeocoderBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Bazinga\Bundle\GeocoderBundle\Tests\Logger;

use Bazinga\Bundle\GeocoderBundle\Logger\GeocoderLogger;
use Geocoder\Result\Geocoded;

/**
* @author Antoine Corcy <[email protected]>
*/
class GeocoderLoggerTest extends \PHPUnit_Framework_TestCase
{
protected $geocoderLogger;
protected $result;
protected $results;

public function setUp()
{
$logger = $this->getMock('Symfony\\Component\\HttpKernel\\Log\\LoggerInterface');
$logger
->expects($this->any())
->method('info')
->will($this->returnValue(null))
;

$this->geocoderLogger = new GeocoderLogger($logger);

$this->result = new Geocoded;
$this->result->fromArray(array(
'latitude' => 1,
'longitude' => 2,
));

$this->results = new \SplObjectStorage;
$this->results->attach($this->result);

$otherResult = new Geocoded;
$otherResult->fromArray(array(
'latitude' => 3,
'longitude' => 4,
));

$this->results->attach($otherResult);
}

public function testLogSingleResult()
{
$this->geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->result);

$this->assertTrue(is_array($requests = $this->geocoderLogger->getRequests()));
$this->assertCount(1, $requests);
$this->assertTrue(is_array($request = $requests[0]));
$this->assertSame($request['value'], 'copenhagen');
$this->assertSame($request['duration'], 0.123);
$this->assertSame($request['providerClass'], 'FooProvider');
$this->assertSame($request['result'], '{"latitude":1,"longitude":2,"bounds":null,"streetNumber":null,"streetName":null,"zipcode":null,"city":null,"cityDistrict":null,"county":null,"countyCode":null,"region":null,"regionCode":null,"country":null,"countryCode":null,"timezone":null}');
}

public function testLog2RequestsWithSingleResult()
{
$this->geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->result);
$this->geocoderLogger->logRequest('paris', 0.456, 'BarProvider', $this->result);

$this->assertTrue(is_array($requests = $this->geocoderLogger->getRequests()));
$this->assertCount(2, $requests);
$this->assertTrue(is_array($request = $requests[0]));
$this->assertSame($request['value'], 'copenhagen');
$this->assertTrue(is_array($request = $requests[1]));
$this->assertSame($request['value'], 'paris');
}

public function testLogMultipleResults()
{
$this->geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->results);

$this->assertTrue(is_array($requests = $this->geocoderLogger->getRequests()));
$this->assertCount(1, $requests);
$this->assertTrue(is_array($request = $requests[0]));
$this->assertSame($request['value'], 'copenhagen');
$this->assertSame($request['duration'], 0.123);
$this->assertSame($request['providerClass'], 'FooProvider');
$this->assertSame($request['result'], '[{"latitude":1,"longitude":2,"bounds":null,"streetNumber":null,"streetName":null,"zipcode":null,"city":null,"cityDistrict":null,"county":null,"countyCode":null,"region":null,"regionCode":null,"country":null,"countryCode":null,"timezone":null},{"latitude":3,"longitude":4,"bounds":null,"streetNumber":null,"streetName":null,"zipcode":null,"city":null,"cityDistrict":null,"county":null,"countyCode":null,"region":null,"regionCode":null,"country":null,"countryCode":null,"timezone":null}]');
$this->assertCount(2, json_decode($request['result']));
}

public function testLog2RequetsWithMultipleResults()
{
$this->geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->results);
$this->geocoderLogger->logRequest('paris', 0.456, 'BarProvider', $this->results);

$this->assertTrue(is_array($requests = $this->geocoderLogger->getRequests()));
$this->assertCount(2, $requests);

$this->assertTrue(is_array($request = $requests[0]));
$this->assertSame($request['value'], 'copenhagen');
$this->assertCount(2, json_decode($request['result']));

$this->assertTrue(is_array($request = $requests[1]));
$this->assertSame($request['value'], 'paris');
$this->assertCount(2, json_decode($request['result']));
}

public function testLogRequestsWithoutLogger()
{
$geocoderLogger = new GeocoderLogger;
$geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->result);

$this->assertTrue(is_array($requests = $geocoderLogger->getRequests()));
$this->assertCount(1, $requests);
$this->assertTrue(is_array($request = $requests[0]));
$this->assertSame($request['value'], 'copenhagen');
$this->assertSame($request['duration'], 0.123);
$this->assertSame($request['providerClass'], 'FooProvider');
$this->assertSame($request['result'], '{"latitude":1,"longitude":2,"bounds":null,"streetNumber":null,"streetName":null,"zipcode":null,"city":null,"cityDistrict":null,"county":null,"countyCode":null,"region":null,"regionCode":null,"country":null,"countryCode":null,"timezone":null}');
}
}
2 changes: 1 addition & 1 deletion Tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
if (!$loader = @require_once __DIR__ . '/../vendor/autoload.php') {
die("You must set up the project dependencies, run the following commands:
wget http://getcomposer.org/composer.phar
php composer.phar install --dev
php composer.phar install --dev --prefer-source
");
}

Expand Down

0 comments on commit b28fab6

Please sign in to comment.