Skip to content

Commit

Permalink
Allow invalid constructor to the ConfigurationBasedPropertyProvider. …
Browse files Browse the repository at this point in the history
…This will make it easier to define values in .env files (where the value _can_ be omitted)
  • Loading branch information
loevgaard committed Apr 17, 2023
1 parent 07cc41a commit 466879f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Property/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public function __construct(
public readonly string $apiSecret,
public readonly string $measurementId,
) {
Assert::true(str_starts_with($measurementId, 'G-'));
Assert::true(
str_starts_with($measurementId, 'G-'),
sprintf('The measurement id does not start with "G-". The given input was: "%s"', $measurementId),
);
}
}
14 changes: 12 additions & 2 deletions src/Provider/ConfigurationBasedPropertyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@

final class ConfigurationBasedPropertyProvider implements PropertyProviderInterface
{
/** @var list<array{api_secret: string, measurement_id: string}> */
private readonly array $properties;

/**
* @param list<array{api_secret: string, measurement_id: string}> $properties
*/
public function __construct(private readonly array $properties)
public function __construct(array $properties)
{
// this will filter all properties where the api secret _or_ the measurement id is empty
$this->properties = array_values(array_filter($properties, static function (array $property): bool {
return '' !== $property['api_secret'] && '' !== $property['measurement_id'];
}));
}

public function getProperties(): array
{
return array_map(static fn (array $property): Property => new Property($property['api_secret'], $property['measurement_id']), $this->properties);
return array_map(
static fn (array $property): Property => new Property($property['api_secret'], $property['measurement_id']),
$this->properties,
);
}
}
33 changes: 33 additions & 0 deletions tests/Unit/Provider/ConfigurationBasedPropertyProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Setono\GoogleAnalyticsBundle\Tests\Unit\Provider;

use PHPUnit\Framework\TestCase;
use Setono\GoogleAnalyticsBundle\Property\Property;
use Setono\GoogleAnalyticsBundle\Provider\ConfigurationBasedPropertyProvider;

/**
* @covers \Setono\GoogleAnalyticsBundle\Provider\ConfigurationBasedPropertyProvider
*/
final class ConfigurationBasedPropertyProviderTest extends TestCase
{
/**
* @test
*/
public function it_returns_properties(): void
{
$properties = [
['api_secret' => '', 'measurement_id' => ''],
['api_secret' => '', 'measurement_id' => 'G-1234'],
['api_secret' => 's3cr3t', 'measurement_id' => ''],
['api_secret' => 's3cr3t', 'measurement_id' => 'G-1234'],
];
$provider = new ConfigurationBasedPropertyProvider($properties);

self::assertEquals([
new Property('s3cr3t', 'G-1234'),
], $provider->getProperties());
}
}

0 comments on commit 466879f

Please sign in to comment.