Skip to content

Commit eda7759

Browse files
committed
Throw exception if use_faker is enabled without faker library
Add unit test for FakeIpPlugin
1 parent c54e5c8 commit eda7759

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

DependencyInjection/BazingaGeocoderExtension.php

Lines changed: 5 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\Plugin\Plugin\CachePlugin;
2223
use Geocoder\Plugin\Plugin\LimitPlugin;
2324
use Geocoder\Plugin\Plugin\LocalePlugin;
@@ -55,6 +56,10 @@ public function load(array $configs, ContainerBuilder $container)
5556
$definition = $container->getDefinition(FakeIpPlugin::class);
5657
$definition->replaceArgument(1, $config['fake_ip']['ip']);
5758
$definition->replaceArgument(2, $config['fake_ip']['use_faker']);
59+
60+
if ($config['fake_ip']['use_faker'] && !class_exists(Generator::class)) {
61+
throw new \LogicException('To enable this option, you must install fzaninotto/faker package.');
62+
}
5863
} else {
5964
$container->removeDefinition(FakeIpPlugin::class);
6065
}

Plugin/FakeIpPlugin.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
namespace Bazinga\GeocoderBundle\Plugin;
1414

15-
use Faker\Factory;
15+
use Faker\Generator;
16+
use Faker\Provider\Internet;
1617
use Geocoder\Plugin\Plugin;
1718
use Geocoder\Query\GeocodeQuery;
1819
use Geocoder\Query\Query;
@@ -39,11 +40,21 @@ class FakeIpPlugin implements Plugin
3940
*/
4041
private $useFaker;
4142

43+
/**
44+
* @var Generator|null
45+
*/
46+
private $faker;
47+
4248
public function __construct(string $needle, string $replacement = null, bool $useFaker = false)
4349
{
4450
$this->needle = $needle;
4551
$this->replacement = $replacement;
4652
$this->useFaker = $useFaker;
53+
54+
if ($useFaker) {
55+
$this->faker = new Generator();
56+
$this->faker->addProvider(new Internet($this->faker));
57+
}
4758
}
4859

4960
/**
@@ -57,9 +68,8 @@ public function handleQuery(Query $query, callable $next, callable $first)
5768

5869
$replacement = $this->replacement;
5970

60-
if ($this->useFaker) {
61-
$faker = Factory::create();
62-
$replacement = $faker->ipv4;
71+
if (null !== $this->faker) {
72+
$replacement = $this->faker->ipv4;
6373
}
6474

6575
$text = str_replace($this->needle, $replacement, $query->getText(), $count);

Tests/Plugin/FakeIpPluginTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* @author Quentin Dequippe <[email protected]
21+
*/
22+
class FakeIpPluginTest extends TestCase
23+
{
24+
public function testSimpleHandleQuery()
25+
{
26+
$fakeIpPlugin = new FakeIpPlugin('127.0.0.1', '123.123.123.123');
27+
$query = GeocodeQuery::create('127.0.0.1');
28+
29+
$fakeIpPlugin->handleQuery($query, function () {}, function () {});
30+
31+
$this->assertSame($query->getText(), '123.123.123.123');
32+
}
33+
34+
public function testHandleQueryUsingFaker()
35+
{
36+
$fakeIpPlugin = new FakeIpPlugin('127.0.0.1', '192.168.1.1', true);
37+
$query = GeocodeQuery::create('127.0.0.1');
38+
39+
$fakeIpPlugin->handleQuery($query, function () {}, function () {});
40+
41+
$this->assertNotSame($query->getText(), '192.168.1.1');
42+
}
43+
}

0 commit comments

Comments
 (0)