Skip to content

Commit 0a82df2

Browse files
authored
Merge pull request #255 from qdequippe/master
Add option to use Faker for FakeIpPlugin
2 parents 2850133 + f14b5dc commit 0a82df2

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

DependencyInjection/BazingaGeocoderExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Bazinga\GeocoderBundle\Plugin\ProfilingPlugin;
1919
use Bazinga\GeocoderBundle\ProviderFactory\PluginProviderFactory;
2020
use Bazinga\GeocoderBundle\ProviderFactory\ProviderFactoryInterface;
21+
use Faker\Generator;
2122
use Geocoder\Dumper\Dumper;
2223
use Geocoder\Plugin\Plugin\CachePlugin;
2324
use Geocoder\Plugin\Plugin\LimitPlugin;
@@ -55,6 +56,11 @@ public function load(array $configs, ContainerBuilder $container)
5556
if ($config['fake_ip']['enabled']) {
5657
$definition = $container->getDefinition(FakeIpPlugin::class);
5758
$definition->replaceArgument(1, $config['fake_ip']['ip']);
59+
$definition->replaceArgument(2, $config['fake_ip']['use_faker']);
60+
61+
if ($config['fake_ip']['use_faker'] && !class_exists(Generator::class)) {
62+
throw new \LogicException('To enable this option, you must install fzaninotto/faker package.');
63+
}
5864
} else {
5965
$container->removeDefinition(FakeIpPlugin::class);
6066
}

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function getConfigTreeBuilder()
8787
->canBeEnabled()
8888
->children()
8989
->scalarNode('ip')->defaultNull()->end()
90+
->booleanNode('use_faker')->defaultFalse()->end()
9091
->end()
9192
->end();
9293

Plugin/FakeIpPlugin.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Bazinga\GeocoderBundle\Plugin;
1414

15+
use Faker\Generator;
16+
use Faker\Provider\Internet;
1517
use Geocoder\Plugin\Plugin;
1618
use Geocoder\Query\GeocodeQuery;
1719
use Geocoder\Query\Query;
@@ -33,10 +35,26 @@ class FakeIpPlugin implements Plugin
3335
*/
3436
private $replacement;
3537

36-
public function __construct(string $needle, string $replacement)
38+
/**
39+
* @var bool
40+
*/
41+
private $useFaker;
42+
43+
/**
44+
* @var Generator|null
45+
*/
46+
private $faker;
47+
48+
public function __construct(string $needle, string $replacement = null, bool $useFaker = false)
3749
{
3850
$this->needle = $needle;
3951
$this->replacement = $replacement;
52+
$this->useFaker = $useFaker;
53+
54+
if ($useFaker) {
55+
$this->faker = new Generator();
56+
$this->faker->addProvider(new Internet($this->faker));
57+
}
4058
}
4159

4260
/**
@@ -48,7 +66,13 @@ public function handleQuery(Query $query, callable $next, callable $first)
4866
return $next($query);
4967
}
5068

51-
$text = str_replace($this->needle, $this->replacement, $query->getText(), $count);
69+
$replacement = $this->replacement;
70+
71+
if (null !== $this->faker) {
72+
$replacement = $this->faker->ipv4;
73+
}
74+
75+
$text = str_replace($this->needle, $replacement, $query->getText(), $count);
5276
if ($count > 0) {
5377
$query = $query->withText($text);
5478
}

Resources/doc/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ bazinga_geocoder:
207207

208208
If set, the parameter will replace all instances of "127.0.0.1" in your queries and replace them with the given one.
209209

210+
You can also let [Faker](https://github.com/fzaninotto/Faker) generate fake ip for you.
211+
212+
```yaml
213+
bazinga_geocoder:
214+
fake_ip:
215+
use_faker: true # default false
216+
```
210217

211218
### Cache Results
212219

Tests/Plugin/FakeIpPluginTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the BazingaGeocoderBundle package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Bazinga\GeocoderBundle\Tests\Plugin;
14+
15+
use Bazinga\GeocoderBundle\Plugin\FakeIpPlugin;
16+
use Geocoder\Query\GeocodeQuery;
17+
use Geocoder\Query\Query;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* @author Quentin Dequippe <[email protected]
22+
*/
23+
class FakeIpPluginTest extends TestCase
24+
{
25+
public function testSimpleHandleQuery()
26+
{
27+
$fakeIpPlugin = new FakeIpPlugin('127.0.0.1', '123.123.123.123');
28+
$query = GeocodeQuery::create('127.0.0.1');
29+
30+
/** @var Query $query */
31+
$query = $fakeIpPlugin->handleQuery($query, function (Query $query) { return $query; }, function () {});
32+
33+
$this->assertSame($query->getText(), '123.123.123.123');
34+
}
35+
36+
public function testHandleQueryUsingFaker()
37+
{
38+
$fakeIpPlugin = new FakeIpPlugin('127.0.0.1', '192.168.1.1', true);
39+
$query = GeocodeQuery::create('127.0.0.1');
40+
41+
/** @var Query $query */
42+
$query = $fakeIpPlugin->handleQuery($query, function (Query $query) { return $query; }, function () {});
43+
44+
$this->assertNotSame($query->getText(), '192.168.1.1');
45+
}
46+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"require-dev": {
2323
"doctrine/cache": "~1.3",
2424
"doctrine/orm": "~2.3",
25+
"fzaninotto/faker": "^1.8",
2526
"geocoder-php/algolia-places-provider": "^0.1.1",
2627
"geocoder-php/arcgis-online-provider": "^4.0",
2728
"geocoder-php/bing-maps-provider": "^4.0",

0 commit comments

Comments
 (0)