Skip to content

Commit

Permalink
simplified extension + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prgTW committed Jan 31, 2024
1 parent c617f60 commit 55167d3
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 56 deletions.
53 changes: 11 additions & 42 deletions DependencyInjection/GoogleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,9 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration;
$config = $this->processConfiguration($configuration, $configs);

$modules = [
'analytics' => [],
'maps' => [],
];

foreach ($configs as $singleConfig) {
foreach (array_keys($modules) as $module) {
if (array_key_exists($module, $singleConfig)) {
$modules[$module][] = isset($singleConfig[$module]) ? $singleConfig[$module] : [];
}
}
}

$this->adwordsLoad($config['adwords'], $container);
$this->analyticsLoad($modules['analytics'], $container);
$this->mapsLoad($modules['maps'], $container);
$this->analyticsLoad($config['analytics'], $container);
$this->mapsLoad($config['maps'], $container);
}

private function adwordsLoad(array $config, ContainerBuilder $container): void
Expand All @@ -43,43 +30,25 @@ private function adwordsLoad(array $config, ContainerBuilder $container): void
$container->setParameter('google.adwords.conversions', $config['conversions']);
}

private function analyticsLoad(array $configs, ContainerBuilder $container): void
private function analyticsLoad(array $config, ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('analytics.xml');

$trackers = [];
foreach ($configs as $config) {
$trackers = array_merge($trackers, isset($config['trackers']) ? $config['trackers'] : []);
if (isset($config['dashboard'])) {
$container->setParameter('google.analytics.dashboard', $config['dashboard']);
}
if (isset($config['whitelist'])) {
$container->setParameter('google.analytics.whitelist', $config['whitelist']);
}
if (isset($config['js_source_https'])) {
$container->setParameter('google.analytics.js_source_https', $config['js_source_https']);
}
if (isset($config['js_source_http'])) {
$container->setParameter('google.analytics.js_source_http', $config['js_source_http']);
}
if (isset($config['js_source_endpoint'])) {
$container->setParameter('google.analytics.js_source_endpoint', $config['js_source_endpoint']);
}
}
$container->setParameter('google.analytics.trackers', $trackers);
$container->setParameter('google.analytics.dashboard', $config['dashboard']);
$container->setParameter('google.analytics.whitelist', $config['whitelist']);
$container->setParameter('google.analytics.js_source_https', $config['js_source_https']);
$container->setParameter('google.analytics.js_source_http', $config['js_source_http']);
$container->setParameter('google.analytics.js_source_endpoint', $config['js_source_endpoint']);
$container->setParameter('google.analytics.trackers', $config['trackers']);
}

private function mapsLoad(array $configs, ContainerBuilder $container): void
private function mapsLoad(array $config, ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('maps.xml');

foreach ($configs as $config) {
if (isset($config['config'])) {
$container->setParameter('google.maps.config', $config['config']);
}
}
$container->setParameter('google.maps.config', $config['config']);
}

public function getAlias()
Expand Down
1 change: 0 additions & 1 deletion Resources/config/adwords.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="google.adwords.conversions" type="collection" />
<parameter key="google.adwords.class">AntiMattr\GoogleBundle\Adwords</parameter>
<parameter key="google.adwords.helper.class">AntiMattr\GoogleBundle\Helper\AdwordsHelper</parameter>
<parameter key="google.adwords.extension.class">AntiMattr\GoogleBundle\Extension\AdwordsExtension</parameter>
Expand Down
9 changes: 0 additions & 9 deletions Resources/config/analytics.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="google.analytics.dashboard" type="collection" />
<parameter key="google.analytics.trackers" type="collection" />
<parameter key="google.analytics.whitelist" type="collection" />
<parameter key="google.analytics.js_source_https">https://</parameter>
<parameter key="google.analytics.js_source_http">http://</parameter>
<parameter key="google.analytics.js_source_endpoint">stats.g.doubleclick.net/dc.js</parameter>
</parameters>

<services>
<service id="google.analytics" class="AntiMattr\GoogleBundle\Analytics" public="true">
<argument type="service" id="service_container" />
Expand Down
4 changes: 0 additions & 4 deletions Resources/config/maps.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="google.maps.config" type="collection" />
</parameters>

<services>
<service id="google.maps" class="AntiMattr\GoogleBundle\MapsManager" public="true">
<argument>%google.maps.config%</argument>
Expand Down
96 changes: 96 additions & 0 deletions Tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace AntiMattr\GoogleBundle\Tests\Configuration;

use AntiMattr\GoogleBundle\DependencyInjection\Configuration;
use AntiMattr\GoogleBundle\DependencyInjection\GoogleExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ConfigurationTest extends TestCase
{
Expand All @@ -19,6 +21,100 @@ public function testEmptyConfig(array $configs, array $expectedConfig): void
self::assertEquals($expectedConfig, $computedConfig);
}

public function testDefaultParametersSet(): void
{
$container = new ContainerBuilder;
(new GoogleExtension)->load([], $container);

self::assertTrue($container->hasParameter('google.adwords.conversions'));
self::assertEquals([], $container->getParameter('google.analytics.dashboard'));
self::assertEquals([], $container->getParameter('google.analytics.trackers'));
self::assertEquals([], $container->getParameter('google.analytics.whitelist'));
self::assertSame('https://', $container->getParameter('google.analytics.js_source_https'));
self::assertSame('http://', $container->getParameter('google.analytics.js_source_http'));
self::assertSame(
'stats.g.doubleclick.net/dc.js',
$container->getParameter('google.analytics.js_source_endpoint')
);
self::assertSame([], $container->getParameter('google.maps.config'));
}

public function testOverridenParametersSet(): void
{
$container = new ContainerBuilder;
$configs = [];
$configs[] = [
'adwords' => [
'conversions' => [
'conversion1' => [
'id' => 'conversion1id',
],
],
],
'maps' => [
'config' => null,
],
];
$configs[] = [
'analytics' => [
'dashboard' => [
'api_key' => 'test',
],
'whitelist' => [],
'js_source_https' => 'secure://',
'js_source_http' => 'insecure://',
'js_source_endpoint' => 'some_host',
'trackers' => [
'tracker1' => [
'accountId' => 'UA-123',
],
],
],
];
$configs[] = [
'maps' => [
'config' => [
'map1key' => [
'id' => 'map1id',
],
],
],
];

(new GoogleExtension)->load($configs, $container);

self::assertEquals(
[
'conversion1' => [
'id' => 'conversion1id',
'label' => null,
'value' => null,
'format' => null,
'color' => null,
'language' => null,
],
],
$container->getParameter('google.adwords.conversions')
);
self::assertEquals(
['api_key' => 'test'],
$container->getParameter('google.analytics.dashboard')
);
self::assertEquals(
['tracker1' => ['accountId' => 'UA-123']],
$container->getParameter('google.analytics.trackers')
);
self::assertEquals([], $container->getParameter('google.analytics.whitelist'));
self::assertSame('secure://', $container->getParameter('google.analytics.js_source_https'));
self::assertSame('insecure://', $container->getParameter('google.analytics.js_source_http'));
self::assertSame('some_host', $container->getParameter('google.analytics.js_source_endpoint'));
self::assertSame(
['map1key' => ['id' => 'map1id']],
$container->getParameter('google.maps.config')
);
}


public function provideConfigs(): iterable
{
yield 'empty' => [
Expand Down

0 comments on commit 55167d3

Please sign in to comment.