Skip to content

Commit 77cb7a3

Browse files
committed
Add TelemetryExtension with functional test for env override
1 parent 5049c05 commit 77cb7a3

File tree

15 files changed

+485
-28
lines changed

15 files changed

+485
-28
lines changed

.github/workflows/build.yaml

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,27 @@ on:
66
workflow_dispatch: ~
77

88
jobs:
9-
unit-tests:
9+
tests:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
php: ["8.2", "8.3", "8.4"]
14+
php: ["8.2", "8.3", "8.4", "8.5"]
15+
symfony: ["7.4"]
1516
name: "PHPUnit (PHP ${{ matrix.php }})"
1617
steps:
1718
- uses: actions/checkout@v4
1819

19-
- uses: shivammathur/setup-php@v2
20+
- name: Build test application
21+
uses: SyliusLabs/BuildTestAppAction@v4
2022
with:
21-
php-version: "${{ matrix.php }}"
22-
extensions: intl
23-
coverage: none
24-
25-
- name: Get Composer cache directory
26-
id: composer-cache
27-
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
28-
29-
- name: Cache Composer
30-
uses: actions/cache@v4
31-
with:
32-
path: ${{ steps.composer-cache.outputs.dir }}
33-
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
34-
restore-keys: |
35-
${{ runner.os }}-php-${{ matrix.php }}-composer-
36-
37-
- name: Install PHP dependencies
38-
run: composer install --no-interaction --no-scripts
23+
build_type: "plugin"
24+
cache_key: "${{ github.run_id }}-${{ runner.os }}-${{ hashFiles('composer.json') }}-php-${{ matrix.php }}-symfony-${{ matrix.symfony }}-"
25+
cache_restore_key: "${{ github.run_id }}-${{ runner.os }}-${{ hashFiles('composer.json') }}-php-${{ matrix.php }}-symfony-${{ matrix.symfony }}-"
26+
e2e: "yes"
27+
php_version: "${{ matrix.php }}"
28+
symfony_version: "${{ matrix.symfony }}"
29+
database: "mysql:8.4"
3930

4031
- name: Run PHPUnit
41-
env:
42-
SYLIUS_TEST_APP_BUNDLES_PATH: "tests/TestApplication/config/bundles.php"
43-
DATABASE_URL: "sqlite:///tmp/telemetry_test.db"
4432
run: vendor/bin/phpunit

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/vendor
22
composer.lock
3+
/var
4+
.phpunit.result.cache
5+
tests/TestApplication/.env.local

composer.json

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,42 @@
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",
20+
"doctrine/cache": "^2.2"
1321
},
1422
"autoload": {
1523
"psr-4": {
1624
"Sylius\\TelemetryBundle\\": "src/"
1725
}
1826
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Sylius\\TelemetryBundle\\Tests\\": "tests/"
30+
}
31+
},
32+
"config": {
33+
"allow-plugins": {
34+
"symfony/flex": true,
35+
"symfony/runtime": true,
36+
"php-http/discovery": false
37+
},
38+
"audit": {
39+
"ignore": ["PKSA-rkkf-636k-qjb3", "PKSA-wws7-mr54-jsny", "PKSA-4g5g-4rkv-myqs"]
40+
}
41+
},
1942
"extra": {
2043
"branch-alias": {
2144
"dev-main": "1.0-dev"
22-
}
23-
}
45+
},
46+
"public-dir": "vendor/sylius/test-application/public"
47+
},
48+
"prefer-stable": true,
49+
"minimum-stability": "alpha"
2450
}

phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4-
bootstrap="vendor/autoload.php"
4+
bootstrap="vendor/sylius/test-application/config/bootstrap.php"
55
colors="true"
66
failOnEmptyTestSuite="false"
77
>
8+
<php>
9+
<server name="KERNEL_CLASS" value="Sylius\TestApplication\Kernel" />
10+
</php>
811
<testsuites>
912
<testsuite name="TelemetryBundle">
1013
<directory>tests</directory>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
22+
$container->prependExtensionConfig('sylius_core', [
23+
'telemetry' => [
24+
'enabled' => true,
25+
],
26+
]);
27+
}
28+
29+
public function load(array $configs, ContainerBuilder $container): void
30+
{
31+
}
32+
33+
private function isDevOrTestEnv(ContainerBuilder $container): bool
34+
{
35+
$env = $container->getParameter('kernel.environment');
36+
37+
return str_starts_with($env, 'dev') || str_starts_with($env, 'test');
38+
}
39+
}

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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\TelemetryBundle\Tests\Double;
6+
7+
use Sylius\Component\Core\Telemetry\Cache\TelemetryCacheInterface;
8+
9+
final class AlwaysSendTelemetryCache implements TelemetryCacheInterface
10+
{
11+
public function shouldSendTelemetry(): bool
12+
{
13+
return true;
14+
}
15+
16+
public function getCachedTelemetryData(): ?array
17+
{
18+
return ['installation_id' => 'test-installation'];
19+
}
20+
21+
public function storeSuccess(string $installationId): void
22+
{
23+
}
24+
25+
public function storeFailure(string $installationId, array $telemetryData): void
26+
{
27+
}
28+
29+
public function clear(): void
30+
{
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\TelemetryBundle\Tests\Double;
6+
7+
use Sylius\Component\Core\Telemetry\Sender\TelemetrySenderInterface;
8+
9+
final class InMemoryTelemetrySender implements TelemetrySenderInterface
10+
{
11+
/** @var list<array<string, mixed>> */
12+
private array $calls = [];
13+
14+
public function send(array $telemetryData): bool
15+
{
16+
$this->calls[] = $telemetryData;
17+
18+
return true;
19+
}
20+
21+
public function wasCalled(): bool
22+
{
23+
return $this->calls !== [];
24+
}
25+
26+
public function getCallCount(): int
27+
{
28+
return count($this->calls);
29+
}
30+
31+
/** @return list<array<string, mixed>> */
32+
public function getCalls(): array
33+
{
34+
return $this->calls;
35+
}
36+
37+
public function reset(): void
38+
{
39+
$this->calls = [];
40+
}
41+
}

0 commit comments

Comments
 (0)