|
| 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 | + } |
| 17 | + |
| 18 | + public function testItEnablesTelemetryInProdWhenDisabledViaEnv(): void |
| 19 | + { |
| 20 | + $_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0'; |
| 21 | + $container = $this->createContainer('prod'); |
| 22 | + |
| 23 | + (new TelemetryExtension())->prepend($container); |
| 24 | + |
| 25 | + $this->assertSame('1', $_ENV['SYLIUS_TELEMETRY_ENABLED']); |
| 26 | + $this->assertSame('1', $_SERVER['SYLIUS_TELEMETRY_ENABLED']); |
| 27 | + $this->assertPrependedConfig($container); |
| 28 | + } |
| 29 | + |
| 30 | + public function testItEnablesTelemetryInProdWhenEnvNotSet(): void |
| 31 | + { |
| 32 | + $container = $this->createContainer('prod'); |
| 33 | + |
| 34 | + (new TelemetryExtension())->prepend($container); |
| 35 | + |
| 36 | + $this->assertSame('1', $_ENV['SYLIUS_TELEMETRY_ENABLED']); |
| 37 | + $this->assertPrependedConfig($container); |
| 38 | + } |
| 39 | + |
| 40 | + public function testItEnablesTelemetryInStagingEnvironment(): void |
| 41 | + { |
| 42 | + $_ENV['SYLIUS_TELEMETRY_ENABLED'] = 'false'; |
| 43 | + $container = $this->createContainer('staging'); |
| 44 | + |
| 45 | + (new TelemetryExtension())->prepend($container); |
| 46 | + |
| 47 | + $this->assertSame('1', $_ENV['SYLIUS_TELEMETRY_ENABLED']); |
| 48 | + $this->assertPrependedConfig($container); |
| 49 | + } |
| 50 | + |
| 51 | + public function testItSkipsInDevEnvironment(): void |
| 52 | + { |
| 53 | + $_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0'; |
| 54 | + $container = $this->createContainer('dev'); |
| 55 | + |
| 56 | + (new TelemetryExtension())->prepend($container); |
| 57 | + |
| 58 | + $this->assertSame('0', $_ENV['SYLIUS_TELEMETRY_ENABLED']); |
| 59 | + $this->assertNoPrependedConfig($container); |
| 60 | + } |
| 61 | + |
| 62 | + public function testItSkipsInTestEnvironment(): void |
| 63 | + { |
| 64 | + $_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0'; |
| 65 | + $container = $this->createContainer('test'); |
| 66 | + |
| 67 | + (new TelemetryExtension())->prepend($container); |
| 68 | + |
| 69 | + $this->assertSame('0', $_ENV['SYLIUS_TELEMETRY_ENABLED']); |
| 70 | + $this->assertNoPrependedConfig($container); |
| 71 | + } |
| 72 | + |
| 73 | + private function createContainer(string $environment): ContainerBuilder |
| 74 | + { |
| 75 | + $container = new ContainerBuilder(); |
| 76 | + $container->setParameter('kernel.environment', $environment); |
| 77 | + |
| 78 | + return $container; |
| 79 | + } |
| 80 | + |
| 81 | + private function assertPrependedConfig(ContainerBuilder $container): void |
| 82 | + { |
| 83 | + $configs = $container->getExtensionConfig('sylius_core'); |
| 84 | + $this->assertNotEmpty($configs); |
| 85 | + $this->assertSame(['telemetry' => ['enabled' => true]], $configs[0]); |
| 86 | + } |
| 87 | + |
| 88 | + private function assertNoPrependedConfig(ContainerBuilder $container): void |
| 89 | + { |
| 90 | + $configs = $container->getExtensionConfig('sylius_core'); |
| 91 | + $this->assertEmpty($configs); |
| 92 | + } |
| 93 | +} |
0 commit comments