Skip to content

Commit

Permalink
Merge pull request #62 from peter-gribanov/database_edition
Browse files Browse the repository at this point in the history
Add paid editions of the database to a "edition" configuration option
  • Loading branch information
peter-gribanov authored Sep 10, 2020
2 parents 90cc376 + 1a4c0dd commit 21a70b5
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 66 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ gpslab_geoip:
# Your personal licence key
license: 'XXXXXXXXXXXXXXXX'

# Database edition ID GeoLite2-ASN, GeoLite2-City or GeoLite2-Country
# One of database edition IDs:
# GeoLite2-ASN
# GeoLite2-City
# GeoLite2-Country
# GeoIP2-City
# GeoIP2-Country
# GeoIP2-Anonymous-IP
# GeoIP2-Domain
# GeoIP2-ISP
edition: 'GeoLite2-City'
```
Expand Down
22 changes: 12 additions & 10 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ class Configuration implements ConfigurationInterface

private const LICENSE_DIRTY_HACK = 'YOUR-LICENSE-KEY';

private const DATABASE_EDITION_IDS = [
'GeoLite2-ASN',
'GeoLite2-City',
'GeoLite2-Country',
'GeoIP2-City',
'GeoIP2-Country',
'GeoIP2-Anonymous-IP',
'GeoIP2-Domain',
'GeoIP2-ISP',
];

/**
* @var string
*/
Expand Down Expand Up @@ -100,16 +111,7 @@ private function getDatabaseNode(): ArrayNodeDefinition

$database_node->children()->scalarNode('license');

$database_node->children()->enumNode('edition')->values([
'GeoLite2-ASN',
'GeoLite2-City',
'GeoLite2-Country',
'GeoIP2-City',
'GeoIP2-Country',
'GeoIP2-Anonymous-IP',
'GeoIP2-Domain',
'GeoIP2-ISP',
]);
$database_node->children()->enumNode('edition')->values(self::DATABASE_EDITION_IDS);

return $root_node;
}
Expand Down
134 changes: 79 additions & 55 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@ class ConfigurationTest extends TestCase

private const PATH = '%s/%s.mmdb';

private const DATABASE_EDITION_IDS = [
'GeoLite2-ASN',
'GeoLite2-City',
'GeoLite2-Country',
'GeoIP2-City',
'GeoIP2-Country',
'GeoIP2-Anonymous-IP',
'GeoIP2-Domain',
'GeoIP2-ISP',
];

/**
* @return array[]
* @return mixed[]
*/
public function getBadConfigs(): array
public function getBadConfigs(): iterable
{
$configurations = [];
// undefined edition
Expand All @@ -34,7 +45,15 @@ public function getBadConfigs(): array
'license' => 'LICENSE',
],
];
// permissible values: "GeoLite2-ASN", "GeoLite2-City", "GeoLite2-Country"
// available values:
// GeoLite2-ASN
// GeoLite2-City
// GeoLite2-Country
// GeoIP2-City
// GeoIP2-Country
// GeoIP2-Anonymous-IP
// GeoIP2-Domain
// GeoIP2-ISP
$configurations[] = [
'gpslab_geoip' => [
'license' => 'LICENSE',
Expand Down Expand Up @@ -133,14 +152,11 @@ public function getBadConfigs(): array
];
}

$return = [];
foreach ($configurations as $configuration) {
foreach (['/tmp/var/cache', null] as $cache_dir) {
$return[] = [$cache_dir, $configuration];
yield [$cache_dir, $configuration];
}
}

return $return;
}

/**
Expand All @@ -161,34 +177,36 @@ public function testBadConfigs(?string $cache_dir, array $configs): void
}

/**
* @return array[]
* @return mixed[]
*/
public function getConfigs(): array
public function getConfigs(): iterable
{
$return = [];
foreach (['/tmp/var/cache', null] as $cache_dir) {
$real_cache_dir = $cache_dir ?: sys_get_temp_dir();

$return[] = [$cache_dir, [], [
yield [$cache_dir, [], [
'locales' => ['en'],
'default_database' => 'default',
'databases' => [],
]];
$return[] = [$cache_dir, [

yield [$cache_dir, [
'gpslab_geoip' => null,
], [
'locales' => ['en'],
'default_database' => 'default',
'databases' => [],
]];
$return[] = [$cache_dir, [

yield [$cache_dir, [
'gpslab_geoip' => [],
], [
'locales' => ['en'],
'default_database' => 'default',
'databases' => [],
]];
$return[] = [$cache_dir, [

yield [$cache_dir, [
'gpslab_geoip' => [
'databases' => null,
],
Expand All @@ -197,7 +215,8 @@ public function getConfigs(): array
'locales' => ['en'],
'default_database' => 'default',
]];
$return[] = [$cache_dir, [

yield [$cache_dir, [
'gpslab_geoip' => [
'databases' => [],
],
Expand All @@ -206,49 +225,54 @@ public function getConfigs(): array
'locales' => ['en'],
'default_database' => 'default',
]];
$return[] = [$cache_dir, [
'gpslab_geoip' => [
'license' => 'LICENSE',
'edition' => 'GeoLite2-City',
],
], [
'default_database' => 'default',
'databases' => [
'default' => [

foreach (self::DATABASE_EDITION_IDS as $database_edition_id) {
yield [$cache_dir, [
'gpslab_geoip' => [
'license' => 'LICENSE',
'edition' => 'GeoLite2-City',
'url' => sprintf(self::URL, 'GeoLite2-City', 'LICENSE'),
'path' => sprintf(self::PATH, $real_cache_dir, 'GeoLite2-City'),
'locales' => ['en'],
'edition' => $database_edition_id,
],
],
'locales' => ['en'],
]];
$return[] = [$cache_dir, [
'gpslab_geoip' => [
'license' => 'LICENSE',
], [
'default_database' => 'default',
'databases' => [
'default' => [
'edition' => 'GeoLite2-City',
'locales' => ['ru'],
'license' => 'LICENSE',
'edition' => $database_edition_id,
'url' => sprintf(self::URL, $database_edition_id, 'LICENSE'),
'path' => sprintf(self::PATH, $real_cache_dir, $database_edition_id),
'locales' => ['en'],
],
],
],
], [
'license' => 'LICENSE',
'databases' => [
'default' => [
'edition' => 'GeoLite2-City',
'locales' => ['ru'],
'locales' => ['en'],
]];

yield [$cache_dir, [
'gpslab_geoip' => [
'license' => 'LICENSE',
'url' => sprintf(self::URL, 'GeoLite2-City', 'LICENSE'),
'path' => sprintf(self::PATH, $real_cache_dir, 'GeoLite2-City'),
'databases' => [
'default' => [
'edition' => $database_edition_id,
'locales' => ['ru'],
],
],
],
],
'default_database' => 'default',
'locales' => ['en'],
]];
$return[] = [$cache_dir, [
], [
'license' => 'LICENSE',
'databases' => [
'default' => [
'edition' => $database_edition_id,
'locales' => ['ru'],
'license' => 'LICENSE',
'url' => sprintf(self::URL, $database_edition_id, 'LICENSE'),
'path' => sprintf(self::PATH, $real_cache_dir, $database_edition_id),
],
],
'default_database' => 'default',
'locales' => ['en'],
]];
}

yield [$cache_dir, [
'gpslab_geoip' => [
'default_database' => 'foo',
'databases' => [
Expand Down Expand Up @@ -295,7 +319,8 @@ public function getConfigs(): array
],
'locales' => ['en'],
]];
$return[] = [$cache_dir, [

yield [$cache_dir, [
'gpslab_geoip' => [
'license' => 'LICENSE_1',
'locales' => ['ru', 'en'],
Expand Down Expand Up @@ -334,7 +359,7 @@ public function getConfigs(): array

// test dirty hack for Symfony Flex
// https://github.com/symfony/recipes-contrib/pull/837
$return[] = [$cache_dir, [
yield [$cache_dir, [
'gpslab_geoip' => [
'license' => 'YOUR-LICENSE-KEY',
],
Expand All @@ -343,7 +368,8 @@ public function getConfigs(): array
'databases' => [],
'locales' => ['en'],
]];
$return[] = [$cache_dir, [

yield [$cache_dir, [
'gpslab_geoip' => [
'databases' => [
'default' => [
Expand All @@ -357,8 +383,6 @@ public function getConfigs(): array
'locales' => ['en'],
]];
}

return $return;
}

/**
Expand Down

0 comments on commit 21a70b5

Please sign in to comment.