diff --git a/DependencyInjection/GoogleExtension.php b/DependencyInjection/GoogleExtension.php index e23b125..0065705 100755 --- a/DependencyInjection/GoogleExtension.php +++ b/DependencyInjection/GoogleExtension.php @@ -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 @@ -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() diff --git a/Resources/config/adwords.xml b/Resources/config/adwords.xml index c3d682e..5f5b084 100644 --- a/Resources/config/adwords.xml +++ b/Resources/config/adwords.xml @@ -3,7 +3,6 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - AntiMattr\GoogleBundle\Adwords AntiMattr\GoogleBundle\Helper\AdwordsHelper AntiMattr\GoogleBundle\Extension\AdwordsExtension diff --git a/Resources/config/analytics.xml b/Resources/config/analytics.xml index bb8eeb1..2cae196 100644 --- a/Resources/config/analytics.xml +++ b/Resources/config/analytics.xml @@ -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"> - - - - - https:// - http:// - stats.g.doubleclick.net/dc.js - - diff --git a/Resources/config/maps.xml b/Resources/config/maps.xml index 3638589..b6c29ff 100644 --- a/Resources/config/maps.xml +++ b/Resources/config/maps.xml @@ -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"> - - - - %google.maps.config% diff --git a/Tests/Configuration/ConfigurationTest.php b/Tests/Configuration/ConfigurationTest.php index 27bb387..8dc1f90 100644 --- a/Tests/Configuration/ConfigurationTest.php +++ b/Tests/Configuration/ConfigurationTest.php @@ -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 { @@ -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' => [