|
| 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 TelemetryEnablerTest extends TestCase |
| 12 | +{ |
| 13 | + private ?string $previousConfigsToImport = null; |
| 14 | + |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + $_SERVER['SYLIUS_TEST_APP_BUNDLES_PATH'] = 'tests/TestApplication/config/bundles.php'; |
| 18 | + |
| 19 | + $this->previousConfigsToImport = $_SERVER['SYLIUS_TEST_APP_CONFIGS_TO_IMPORT'] ?? null; |
| 20 | + unset($_SERVER['SYLIUS_TEST_APP_CONFIGS_TO_IMPORT']); |
| 21 | + } |
| 22 | + |
| 23 | + protected function tearDown(): void |
| 24 | + { |
| 25 | + if ($this->previousConfigsToImport !== null) { |
| 26 | + $_SERVER['SYLIUS_TEST_APP_CONFIGS_TO_IMPORT'] = $this->previousConfigsToImport; |
| 27 | + } |
| 28 | + |
| 29 | + unset( |
| 30 | + $_ENV['SYLIUS_TELEMETRY_ENABLED'], |
| 31 | + $_SERVER['SYLIUS_TELEMETRY_ENABLED'], |
| 32 | + $_SERVER['SYLIUS_TEST_APP_BUNDLES_PATH'], |
| 33 | + ); |
| 34 | + putenv('SYLIUS_TELEMETRY_ENABLED'); |
| 35 | + } |
| 36 | + |
| 37 | + /** @group prod */ |
| 38 | + public function testProdWithTelemetryDisabledStillHasServicesRegistered(): void |
| 39 | + { |
| 40 | + $_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0'; |
| 41 | + $_SERVER['SYLIUS_TELEMETRY_ENABLED'] = '0'; |
| 42 | + putenv('SYLIUS_TELEMETRY_ENABLED=0'); |
| 43 | + |
| 44 | + $container = $this->compileContainer('prod'); |
| 45 | + |
| 46 | + $this->assertTrue($container->getParameter('sylius_core.telemetry.enabled')); |
| 47 | + $this->assertTrue($container->has('sylius.telemetry.sender')); |
| 48 | + $this->assertTrue($container->has('sylius.telemetry.send_manager')); |
| 49 | + } |
| 50 | + |
| 51 | + /** @group prod */ |
| 52 | + public function testProdWithTelemetryEnabledHasServicesRegistered(): void |
| 53 | + { |
| 54 | + $_ENV['SYLIUS_TELEMETRY_ENABLED'] = '1'; |
| 55 | + $_SERVER['SYLIUS_TELEMETRY_ENABLED'] = '1'; |
| 56 | + putenv('SYLIUS_TELEMETRY_ENABLED=1'); |
| 57 | + |
| 58 | + $container = $this->compileContainer('prod'); |
| 59 | + |
| 60 | + $this->assertTrue($container->getParameter('sylius_core.telemetry.enabled')); |
| 61 | + $this->assertTrue($container->has('sylius.telemetry.sender')); |
| 62 | + $this->assertTrue($container->has('sylius.telemetry.send_manager')); |
| 63 | + } |
| 64 | + |
| 65 | + /** @group dev */ |
| 66 | + public function testDevWithTelemetryDisabledDoesNotHaveServicesRegistered(): void |
| 67 | + { |
| 68 | + $_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0'; |
| 69 | + $_SERVER['SYLIUS_TELEMETRY_ENABLED'] = '0'; |
| 70 | + putenv('SYLIUS_TELEMETRY_ENABLED=0'); |
| 71 | + |
| 72 | + $container = $this->compileContainer('dev'); |
| 73 | + |
| 74 | + $this->assertFalse($container->has('sylius.telemetry.sender')); |
| 75 | + } |
| 76 | + |
| 77 | + /** @group dev */ |
| 78 | + public function testTestEnvWithTelemetryDisabledDoesNotHaveServicesRegistered(): void |
| 79 | + { |
| 80 | + $_ENV['SYLIUS_TELEMETRY_ENABLED'] = '0'; |
| 81 | + $_SERVER['SYLIUS_TELEMETRY_ENABLED'] = '0'; |
| 82 | + putenv('SYLIUS_TELEMETRY_ENABLED=0'); |
| 83 | + |
| 84 | + $container = $this->compileContainer('test'); |
| 85 | + |
| 86 | + $this->assertFalse($container->has('sylius.telemetry.sender')); |
| 87 | + } |
| 88 | + |
| 89 | + private function compileContainer(string $env): ContainerBuilder |
| 90 | + { |
| 91 | + $kernel = new Kernel($env, true); |
| 92 | + |
| 93 | + $initBundles = new \ReflectionMethod($kernel, 'initializeBundles'); |
| 94 | + $initBundles->invoke($kernel); |
| 95 | + |
| 96 | + $buildContainer = new \ReflectionMethod($kernel, 'buildContainer'); |
| 97 | + |
| 98 | + /** @var ContainerBuilder $container */ |
| 99 | + $container = $buildContainer->invoke($kernel); |
| 100 | + $container->compile(); |
| 101 | + |
| 102 | + return $container; |
| 103 | + } |
| 104 | +} |
0 commit comments