Skip to content

Commit 9a98bcc

Browse files
committed
Add TelemetryPass — force-enable telemetry for official plugins
Use PrependExtensionInterface to override both ENV and config before SyliusCoreExtension loads. This ensures telemetry services are always registered in prod when the bundle is installed, even if the user set SYLIUS_TELEMETRY_ENABLED=0. Dev/test environments are skipped. Includes unit tests (TelemetryExtension) and integration tests (real Sylius kernel via sylius/test-application).
1 parent 5049c05 commit 9a98bcc

File tree

6 files changed

+243
-3
lines changed

6 files changed

+243
-3
lines changed

composer.json

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,38 @@
99
"symfony/http-kernel": "^6.4 || ^7.0"
1010
},
1111
"require-dev": {
12-
"phpunit/phpunit": "^10.5 || ^11.0"
12+
"phpunit/phpunit": "^10.5 || ^11.0",
13+
"sylius/sylius": "^2.0",
14+
"sylius/test-application": "^2.0.0@alpha",
15+
"symfony/browser-kit": "^6.4 || ^7.1",
16+
"symfony/debug-bundle": "^6.4 || ^7.1",
17+
"symfony/dotenv": "^6.4 || ^7.1",
18+
"symfony/runtime": "^6.4 || ^7.1",
19+
"symfony/web-profiler-bundle": "^6.4 || ^7.1"
1320
},
1421
"autoload": {
1522
"psr-4": {
1623
"Sylius\\TelemetryBundle\\": "src/"
1724
}
1825
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Sylius\\TelemetryBundle\\Tests\\": "tests/"
29+
}
30+
},
31+
"config": {
32+
"allow-plugins": {
33+
"symfony/flex": true,
34+
"symfony/runtime": true,
35+
"php-http/discovery": false
36+
}
37+
},
1938
"extra": {
2039
"branch-alias": {
2140
"dev-main": "1.0-dev"
22-
}
23-
}
41+
},
42+
"public-dir": "vendor/sylius/test-application/public"
43+
},
44+
"prefer-stable": true,
45+
"minimum-stability": "alpha"
2446
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\TelemetryBundle\DependencyInjection;
6+
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
9+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10+
11+
final class TelemetryExtension extends Extension implements PrependExtensionInterface
12+
{
13+
public function prepend(ContainerBuilder $container): void
14+
{
15+
if ($this->isDevOrTestEnv($container)) {
16+
return;
17+
}
18+
19+
$_ENV['SYLIUS_TELEMETRY_ENABLED'] = '1';
20+
$_SERVER['SYLIUS_TELEMETRY_ENABLED'] = '1';
21+
putenv('SYLIUS_TELEMETRY_ENABLED=1');
22+
23+
$container->prependExtensionConfig('sylius_core', [
24+
'telemetry' => [
25+
'enabled' => true,
26+
],
27+
]);
28+
}
29+
30+
public function load(array $configs, ContainerBuilder $container): void
31+
{
32+
}
33+
34+
private function isDevOrTestEnv(ContainerBuilder $container): bool
35+
{
36+
$env = $container->getParameter('kernel.environment');
37+
38+
return str_starts_with($env, 'dev') || str_starts_with($env, 'test');
39+
}
40+
}

src/TelemetryBundle.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\TelemetryBundle;
6+
7+
use Symfony\Component\HttpKernel\Bundle\Bundle;
8+
9+
final class TelemetryBundle extends Bundle
10+
{
11+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\TelemetryBundle\Tests\DependencyInjection;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sylius\TelemetryBundle\DependencyInjection\TelemetryExtension;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
11+
final class TelemetryExtensionTest extends TestCase
12+
{
13+
protected function tearDown(): void
14+
{
15+
unset($_ENV['SYLIUS_TELEMETRY_ENABLED'], $_SERVER['SYLIUS_TELEMETRY_ENABLED']);
16+
putenv('SYLIUS_TELEMETRY_ENABLED');
17+
}
18+
19+
public function testItForceEnablesTelemetryInProdWhenDisabledViaEnv(): void
20+
{
21+
$_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0';
22+
$container = $this->createContainer('prod');
23+
24+
(new TelemetryExtension())->prepend($container);
25+
26+
$this->assertSame('1', $_ENV['SYLIUS_TELEMETRY_ENABLED']);
27+
$this->assertSame('1', $_SERVER['SYLIUS_TELEMETRY_ENABLED']);
28+
$this->assertSame('1', getenv('SYLIUS_TELEMETRY_ENABLED'));
29+
$this->assertPrependedConfig($container);
30+
}
31+
32+
public function testItForceEnablesTelemetryInProdWhenEnvNotSet(): void
33+
{
34+
$container = $this->createContainer('prod');
35+
36+
(new TelemetryExtension())->prepend($container);
37+
38+
$this->assertSame('1', $_ENV['SYLIUS_TELEMETRY_ENABLED']);
39+
$this->assertPrependedConfig($container);
40+
}
41+
42+
public function testItForceEnablesTelemetryInStagingEnvironment(): void
43+
{
44+
$_ENV['SYLIUS_TELEMETRY_ENABLED'] = 'false';
45+
$container = $this->createContainer('staging');
46+
47+
(new TelemetryExtension())->prepend($container);
48+
49+
$this->assertSame('1', $_ENV['SYLIUS_TELEMETRY_ENABLED']);
50+
$this->assertPrependedConfig($container);
51+
}
52+
53+
public function testItSkipsInDevEnvironment(): void
54+
{
55+
$_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0';
56+
$container = $this->createContainer('dev');
57+
58+
(new TelemetryExtension())->prepend($container);
59+
60+
$this->assertSame('0', $_ENV['SYLIUS_TELEMETRY_ENABLED']);
61+
$this->assertNoPrependedConfig($container);
62+
}
63+
64+
public function testItSkipsInTestEnvironment(): void
65+
{
66+
$_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0';
67+
$container = $this->createContainer('test');
68+
69+
(new TelemetryExtension())->prepend($container);
70+
71+
$this->assertSame('0', $_ENV['SYLIUS_TELEMETRY_ENABLED']);
72+
$this->assertNoPrependedConfig($container);
73+
}
74+
75+
private function createContainer(string $environment): ContainerBuilder
76+
{
77+
$container = new ContainerBuilder();
78+
$container->setParameter('kernel.environment', $environment);
79+
80+
return $container;
81+
}
82+
83+
private function assertPrependedConfig(ContainerBuilder $container): void
84+
{
85+
$configs = $container->getExtensionConfig('sylius_core');
86+
$this->assertNotEmpty($configs);
87+
$this->assertSame(['telemetry' => ['enabled' => true]], $configs[0]);
88+
}
89+
90+
private function assertNoPrependedConfig(ContainerBuilder $container): void
91+
{
92+
$configs = $container->getExtensionConfig('sylius_core');
93+
$this->assertEmpty($configs);
94+
}
95+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\TelemetryBundle\Tests\Integration;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sylius\TestApplication\Kernel;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
11+
final class TelemetryEnforcementTest extends TestCase
12+
{
13+
protected function setUp(): void
14+
{
15+
$_SERVER['SYLIUS_TEST_APP_BUNDLES_PATH'] = 'tests/TestApplication/config/bundles.php';
16+
}
17+
18+
protected function tearDown(): void
19+
{
20+
unset(
21+
$_ENV['SYLIUS_TELEMETRY_ENABLED'],
22+
$_SERVER['SYLIUS_TELEMETRY_ENABLED'],
23+
$_SERVER['SYLIUS_TEST_APP_BUNDLES_PATH'],
24+
);
25+
putenv('SYLIUS_TELEMETRY_ENABLED');
26+
}
27+
28+
public function testTelemetryServicesAreRegisteredInProdEvenWhenDisabledViaEnv(): void
29+
{
30+
$_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0';
31+
$_SERVER['SYLIUS_TELEMETRY_ENABLED'] = '0';
32+
putenv('SYLIUS_TELEMETRY_ENABLED=0');
33+
34+
$container = $this->compileContainer('prod');
35+
36+
$this->assertTrue($container->getParameter('sylius_core.telemetry.enabled'));
37+
$this->assertTrue($container->has('sylius.telemetry.sender'));
38+
$this->assertTrue($container->has('sylius.telemetry.send_manager'));
39+
}
40+
41+
public function testTelemetryServicesAreNotRegisteredInTestEnvWhenDisabledViaEnv(): void
42+
{
43+
$_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0';
44+
$_SERVER['SYLIUS_TELEMETRY_ENABLED'] = '0';
45+
putenv('SYLIUS_TELEMETRY_ENABLED=0');
46+
47+
$container = $this->compileContainer('test');
48+
49+
$this->assertFalse($container->has('sylius.telemetry.sender'));
50+
}
51+
52+
private function compileContainer(string $env): ContainerBuilder
53+
{
54+
$kernel = new Kernel($env, true);
55+
56+
$initBundles = new \ReflectionMethod($kernel, 'initializeBundles');
57+
$initBundles->invoke($kernel);
58+
59+
$buildContainer = new \ReflectionMethod($kernel, 'buildContainer');
60+
61+
/** @var ContainerBuilder $container */
62+
$container = $buildContainer->invoke($kernel);
63+
$container->compile();
64+
65+
return $container;
66+
}
67+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
Sylius\TelemetryBundle\TelemetryBundle::class => ['all' => true],
5+
];

0 commit comments

Comments
 (0)