Skip to content

Commit 22df444

Browse files
authored
Added tests, bugfixes and profiler (#131)
* Added tests and fixed some bugs * More tests and bugfixes * Updated deps * Removed stuff not used * Allow us to test on symfony 4.0 * Typo * Use PHPunit 6 * Working on the profiler page * Fixed profiler * Minor * Using older PHPUnit version * added a word * Text fix * Reduce the number of travis build
1 parent 392de1c commit 22df444

File tree

21 files changed

+399
-374
lines changed

21 files changed

+399
-374
lines changed

.travis.yml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@ php:
1111
- 7.1
1212

1313
env:
14+
matrix:
1415
- SYMFONY_VERSION=3.3.*
15-
- SYMFONY_VERSION=dev-master
1616

1717
matrix:
18-
allow_failures:
19-
- env: SYMFONY_VERSION=dev-master
18+
fast_finish: true
19+
allow_failures:
20+
- php: 7.1
21+
env: SYMFONY_VERSION=4.0.*
22+
include:
23+
- php: 7.0
24+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_VERSION=3.3.* TEST_COMMAND="phpunit --coverage-text --coverage-clover=build/coverage.xml"
25+
- php: 7.1
26+
env: SYMFONY_VERSION=4.0.*
2027

2128
before_script:
22-
- composer require symfony/framework-bundle:${SYMFONY_VERSION} --no-update
23-
# This command must be run with `--prefer-source`, otherwise the `Doctrine\Tests\OrmTestCase` class
24-
# won't be found.
29+
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
30+
# This command must be run with `--prefer-source`, otherwise the `Doctrine\Tests\OrmTestCase` class won't be found.
2531
- composer update --prefer-source
2632

2733
script:
28-
- phpunit --coverage-text --coverage-clover=build/coverage.xml
34+
- phpunit

DataCollector/GeocoderDataCollector.php

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*
88
* @license MIT License
99
*/
10+
1011
namespace Bazinga\Bundle\GeocoderBundle\DataCollector;
1112

12-
use Bazinga\Bundle\GeocoderBundle\Logger\GeocoderLogger;
1313
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpFoundation\Response;
@@ -20,63 +20,85 @@
2020
class GeocoderDataCollector extends DataCollector
2121
{
2222
/**
23-
* @var GeocoderLogger
23+
* @var ProfilingProvider[]
2424
*/
25-
private $logger;
25+
private $instances = [];
2626

27-
/**
28-
* @param GeocoderLogger $logger
29-
*/
30-
public function __construct(GeocoderLogger $logger)
27+
28+
public function __construct()
3129
{
32-
$this->logger = $logger;
30+
$this->data['queries'] = [];
31+
$this->data['providers'] = [];
3332
}
3433

3534
/**
3635
* {@inheritdoc}
3736
*/
3837
public function collect(Request $request, Response $response, \Exception $exception = null)
3938
{
40-
$this->data = array(
41-
'requests' => $this->logger->getRequests(),
42-
);
39+
foreach ($this->instances as $instance) {
40+
foreach ($instance->getQueries() as $query) {
41+
$query['query'] = $this->cloneVar($query['query']);
42+
$query['result'] = $this->cloneVar($query['result']);
43+
$this->data['queries'][] = $query;
44+
}
45+
}
4346
}
4447

4548
/**
4649
* Returns an array of collected requests.
4750
*
4851
* @return array
4952
*/
50-
public function getRequests()
53+
public function getQueries(): array
5154
{
52-
return $this->data['requests'];
53-
}
54-
55-
/**
56-
* Returns the number of collected requests.
57-
*
58-
* @return int
59-
*/
60-
public function getRequestsCount()
61-
{
62-
return count($this->data['requests']);
55+
return $this->data['queries'];
6356
}
6457

6558
/**
6659
* Returns the execution time of all collected requests in seconds.
6760
*
6861
* @return float
6962
*/
70-
public function getTime()
63+
public function getTotalDuration()
7164
{
7265
$time = 0;
73-
foreach ($this->data['requests'] as $command) {
66+
foreach ($this->data['queries'] as $command) {
7467
$time += $command['duration'];
7568
}
7669

7770
return $time;
7871
}
7972

73+
/**
74+
* @return array
75+
*/
76+
public function getProviders(): array
77+
{
78+
return $this->data['providers'];
79+
}
80+
81+
/**
82+
* @param string $provider
83+
*
84+
* @return array
85+
*/
86+
public function getProviderQueries($provider):array
87+
{
88+
return array_filter($this->data['queries'], function ($data) use ($provider) {
89+
return $data['providerName'] === $provider;
90+
});
91+
}
92+
93+
/**
94+
* @param ProfilingProvider $instance
95+
*/
96+
public function addInstance(ProfilingProvider $instance)
97+
{
98+
$this->instances[] = $instance;
99+
$this->data['providers'][] = $instance->getName();
100+
}
101+
80102
/**
81103
* {@inheritdoc}
82104
*/

DataCollector/ProfilingProvider.php

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Bazinga\Bundle\GeocoderBundle\DataCollector;
44

5-
use Bazinga\Bundle\GeocoderBundle\Logger\GeocoderLogger;
65
use Geocoder\Collection;
6+
use Geocoder\Exception\LogicException;
7+
use Geocoder\Location;
78
use Geocoder\Provider\Provider;
89
use Geocoder\Query\GeocodeQuery;
910
use Geocoder\Query\ReverseQuery;
@@ -19,56 +20,76 @@ class ProfilingProvider implements Provider
1920
private $realProvider;
2021

2122
/**
22-
* @var GeocoderLogger
23+
* @var array
2324
*/
24-
private $logger;
25+
private $queries = [];
2526

2627
/**
2728
* @param Provider $realProvider
28-
* @param GeocoderLogger $logger
2929
*/
30-
public function __construct(Provider $realProvider, GeocoderLogger $logger)
30+
public function __construct(Provider $realProvider)
3131
{
3232
$this->realProvider = $realProvider;
33-
$this->logger = $logger;
3433
}
3534

3635
public function geocodeQuery(GeocodeQuery $query): Collection
3736
{
3837
$startTime = microtime(true);
3938
try {
40-
$results = $this->realProvider->geocodeQuery($query);
39+
$result = $this->realProvider->geocodeQuery($query);
4140
} finally {
4241
$duration = (microtime(true) - $startTime) * 1000;
4342

44-
$this->logger->logRequest(
45-
sprintf('[Geocoding] %s', $query),
46-
$duration,
47-
$this->getName(),
48-
$results
49-
);
43+
$this->logQuery($query, $duration, $result);
5044
}
5145

52-
return $results;
46+
return $result;
5347
}
5448

5549
public function reverseQuery(ReverseQuery $query): Collection
5650
{
5751
$startTime = microtime(true);
5852
try {
59-
$results = $this->realProvider->reverseQuery($query);
53+
$result = $this->realProvider->reverseQuery($query);
6054
} finally {
6155
$duration = (microtime(true) - $startTime) * 1000;
6256

63-
$this->logger->logRequest(
64-
sprintf('[Geocoding] %s', $query),
65-
$duration,
66-
$this->getName(),
67-
$results
68-
);
57+
$this->logQuery($query, $duration, $result);
6958
}
7059

71-
return $results;
60+
return $result;
61+
}
62+
63+
/**
64+
* @param GeocodeQuery|ReverseQuery $query
65+
* @param float $duration geocoding duration
66+
* @param Collection $result
67+
*/
68+
private function logQuery($query, float $duration, Collection $result = null)
69+
{
70+
if ($query instanceof GeocodeQuery) {
71+
$queryString = $query->getText();
72+
} elseif ($query instanceof ReverseQuery) {
73+
$queryString = sprintf('(%s, %s)', $query->getCoordinates()->getLongitude(), $query->getCoordinates()->getLongitude());
74+
} else {
75+
throw new LogicException('First parameter to ProfilingProvider::logQuery must be a query');
76+
}
77+
78+
$this->queries[] = array(
79+
'query' => $query,
80+
'queryString' => $queryString,
81+
'duration' => $duration,
82+
'providerName' => $this->getName(),
83+
'result' => $result,
84+
);
85+
}
86+
87+
/**
88+
* @return array
89+
*/
90+
public function getQueries(): array
91+
{
92+
return $this->queries;
7293
}
7394

7495
public function __call($method, $args)

DependencyInjection/BazingaGeocoderExtension.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Bazinga\Bundle\GeocoderBundle\DependencyInjection;
1111

1212
use Bazinga\Bundle\GeocoderBundle\ProviderFactory\ProviderFactoryInterface;
13+
use Geocoder\Provider\Cache\ProviderCache;
1314
use Geocoder\Provider\Provider;
1415
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1516
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
@@ -34,7 +35,7 @@ public function load(array $configs, ContainerBuilder $container)
3435
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3536
$loader->load('services.yml');
3637

37-
if ($config['profiling']) {
38+
if (true === $config['profiling']['enabled']) {
3839
$loader->load('profiling.yml');
3940
}
4041
$this->loadProviders($container, $config);
@@ -88,15 +89,16 @@ private function loadProviders(ContainerBuilder $container, array $config)
8889
*/
8990
private function configureCache(ContainerBuilder $container, string $serviceId, array $providerConfig)
9091
{
91-
if (
92-
(null !== $providerConfig['cache'] || null !== $providerConfig['cache_lifetime']) &&
93-
!class_exists(ProviderCache::class)
94-
) {
95-
throw new \LogicException('You must install "geocoder-php/chain-provider" to use cache.');
92+
if (null === $providerConfig['cache'] && null === $providerConfig['cache_lifetime']) {
93+
return;
9694
}
9795

98-
if (null !== $cacheServiceId = $providerConfig['cache']) {
99-
if (!$container->has('app.cache\'')) {
96+
if (!class_exists(ProviderCache::class)) {
97+
throw new \LogicException('You must install "geocoder-php/cache-provider" to use cache.');
98+
}
99+
100+
if (null === $cacheServiceId = $providerConfig['cache']) {
101+
if (!$container->has('app.cache')) {
100102
throw new \LogicException('You need to specify a service for cache.');
101103
}
102104
$cacheServiceId = 'app.cache';

DependencyInjection/Compiler/ProfilerPass.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Bazinga\Bundle\GeocoderBundle\DataCollector\GeocoderDataCollector;
1313
use Bazinga\Bundle\GeocoderBundle\DataCollector\ProfilingProvider;
14-
use Bazinga\Bundle\GeocoderBundle\Logger\GeocoderLogger;
1514
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1615
use Symfony\Component\DependencyInjection\ContainerBuilder;
1716
use Symfony\Component\DependencyInjection\Reference;
@@ -32,13 +31,15 @@ public function process(ContainerBuilder $container)
3231
return;
3332
}
3433

34+
$dataCollector = $container->getDefinition(GeocoderDataCollector::class);
35+
3536
foreach ($container->findTaggedServiceIds('bazinga_geocoder.provider') as $providerId => $attributes) {
3637
$container->register($providerId.'.debug', ProfilingProvider::class)
3738
->setDecoratedService($providerId)
3839
->setArguments([
3940
new Reference($providerId.'.debug.inner'),
40-
new Reference(GeocoderLogger::class)
4141
]);
42+
$dataCollector->addMethodCall('addInstance', [new Reference($providerId)]);
4243
}
4344
}
4445
}

0 commit comments

Comments
 (0)