Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support both snake_case and camelCase versions of impression data #227

Closed
wants to merge 13 commits into from
7 changes: 4 additions & 3 deletions src/DTO/DefaultProxyFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* value: string
* }
* },
* impression_data: bool
* impressionData: bool
gastonfournier marked this conversation as resolved.
Show resolved Hide resolved
* } $response
*/
public function __construct(array $response)
Expand All @@ -37,12 +37,12 @@ public function __construct(array $response)
// tries to new this up manually and isn't interesting to tests

// @codeCoverageIgnoreStart
assert(isset($response['name'], $response['enabled'], $response['variant'], $response['impression_data']));
assert(isset($response['name'], $response['enabled'], $response['variant'], $response['impressionData']));
gastonfournier marked this conversation as resolved.
Show resolved Hide resolved
// @codeCoverageIgnoreEnd

$this->name = $response['name'];
$this->enabled = $response['enabled'];
$this->impressionData = $response['impression_data'];
$this->impressionData = $response['impressionData'] ?? $response['impression_data'];

$payload = null;

Expand Down Expand Up @@ -88,6 +88,7 @@ public function jsonSerialize(): array
'enabled' => $this->enabled,
'variant' => $this->variant,
'impression_data' => $this->impressionData,
'impressionData' => $this->impressionData, // maybe we don't need this
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure what are we using this serialization for... maybe caching?

];
}
}
6 changes: 3 additions & 3 deletions src/Repository/DefaultUnleashProxyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ private function addQuery(string $url, string $query): string
* value: string
* }
* },
* impression_data: bool
* impressionData: bool
* }|null
*/
private function validateResponse(array $response): ?array
{
if (!isset($response['name'], $response['enabled'], $response['variant'], $response['impression_data'])) {
if (!isset($response['name'], $response['enabled'], $response['variant'], $response['impressionData'])) {
gastonfournier marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

if (!is_string($response['name']) || !is_bool($response['enabled']) || !is_bool($response['impression_data']) || !is_array($response['variant'])) {
if (!is_string($response['name']) || !is_bool($response['enabled']) || !is_bool($response['impressionData']) || !is_array($response['variant'])) {
gastonfournier marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Bootstrap/FileBootstrapProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testUnreadableFile()
$file = $this->createTemporaryFile();
chmod($file, 0222);
$instance = new FileBootstrapProvider($file);
$this->expectException(RuntimeException::class);
$this->expectException(JsonException::class);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RikudouSage I couldn't check your comments as I'm struggling with this test that fails locally. I'm building this Dockerfile (docker build . -t php-with-composer) to reduce noise and make it reproducible:

FROM php:8.3.10-fpm

# Install system dependencies and clean cache
RUN apt-get update && apt-get install -y \
 git \
 curl \
 libpng-dev \
 libonig-dev \
 libxml2-dev \
 libcurl4-openssl-dev libssl-dev \ 
 zip \
 unzip

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Copy Composer from the official Composer image
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

COPY . /usr/src/unleash-client-php

WORKDIR /usr/src/unleash-client-php

RUN rm -f composer.lock

RUN composer install

CMD ["sh"]

And running the tests as: docker run -it --rm --name my-running-script php-with-composer composer phpunit and the test fails with the following error:

$ docker run -it --rm --name my-running-script php-with-composer composer phpunit
> phpunit
PHPUnit 9.6.21 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.10
Configuration: /usr/src/unleash-client-php/phpunit.xml

.........F.....................................................  63 / 233 ( 27%)
............................................................... 126 / 233 ( 54%)
............................................................... 189 / 233 ( 81%)
............................................                    233 / 233 (100%)

Time: 00:25.133, Memory: 20.00 MB

There was 1 failure:

1) Unleash\Client\Tests\Bootstrap\FileBootstrapProviderTest::testUnreadableFile
Failed asserting that exception of type "JsonException" matches expected exception "RuntimeException". Message was: "Syntax error" at
/usr/src/unleash-client-php/src/Bootstrap/FileBootstrapProvider.php:46
/usr/src/unleash-client-php/tests/Bootstrap/FileBootstrapProviderTest.php:79
.

FAILURES!
Tests: 233, Assertions: 895, Failures: 1.
Script phpunit handling the phpunit event returned with error code 1

Do you know what can it be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI does prefer RuntimeException...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't :/ But JsonException generally means that there was a syntax error in some JSON, any chance you're providing a non-JSON string somewhere where JSON is expected?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, figured it out, I assume you run the container under its default user, root? The test creates a file and makes it non-readable using chmod, but root has access to everything regardless of read/write permissions, thus the file is readable but because it's empty it fails with a JsonException.

If I run it as the current user, it works:

docker run --rm -it -u $(id -u):$(id -g) php-with-composer vendor/bin/phpunit

$instance->getBootstrap();
}
}
6 changes: 3 additions & 3 deletions tests/DTO/DefaultProxyFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class DefaultProxyFeatureTest extends TestCase
{
public function testGetProperties()
{
$instance = new DefaultProxyFeature(['name' => 'test', 'enabled' => true, 'impression_data' => true, 'variant' => ['name' => 'someVariant', 'enabled' => true, 'payload' => ['type' => 'string', 'value' => 'test']]]);
$instance = new DefaultProxyFeature(['name' => 'test', 'enabled' => true, 'impressionData' => true, 'variant' => ['name' => 'someVariant', 'enabled' => true, 'payload' => ['type' => 'string', 'value' => 'test']]]);
RikudouSage marked this conversation as resolved.
Show resolved Hide resolved
self::assertIsString($instance->getName());
self::assertIsBool($instance->isEnabled());
self::assertIsBool($instance->hasImpressionData());
Expand All @@ -24,8 +24,8 @@ public function testGetProperties()

public function testToJson()
{
$instance = new DefaultProxyFeature(['name' => 'test', 'enabled' => true, 'impression_data' => true, 'variant' => ['name' => 'someVariant', 'enabled' => true, 'payload' => ['type' => 'string', 'value' => 'test']]]);
$instance = new DefaultProxyFeature(['name' => 'test', 'enabled' => true, 'impressionData' => true, 'variant' => ['name' => 'someVariant', 'enabled' => true, 'payload' => ['type' => 'string', 'value' => 'test']]]);
$json = json_encode($instance);
self::assertEquals('{"name":"test","enabled":true,"variant":{"name":"someVariant","enabled":true,"payload":{"type":"string","value":"test"}},"impression_data":true}', $json);
self::assertEquals('{"name":"test","enabled":true,"variant":{"name":"someVariant","enabled":true,"payload":{"type":"string","value":"test"}},"impression_data":true,"impressionData":true}', $json);
}
}
16 changes: 8 additions & 8 deletions tests/DefaultProxyUnleashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testBasicResolveFeature()
'name' => 'some-variant',
'enabled' => true,
],
'impression_data' => false,
'impressionData' => false,
]);
$unleash = $builder->build();

Expand Down Expand Up @@ -89,7 +89,7 @@ public function testBasicResolveVariant()
],
'enabled' => true,
],
'impression_data' => false,
'impressionData' => false,
]);
$unleash = $builder->build();
$variant = $unleash->getVariant('test');
Expand All @@ -107,7 +107,7 @@ public function testVariantWithoutPayload()
'name' => 'some-variant',
'enabled' => true,
],
'impression_data' => false,
'impressionData' => false,
]);
$unleash = $builder->build();
$variant = $unleash->getVariant('test');
Expand Down Expand Up @@ -138,7 +138,7 @@ public function testVariantWithNullPayload()
'payload' => null,
'enabled' => true,
],
'impression_data' => false,
'impressionData' => false,
]);
$unleash = $builder->build();
$variant = $unleash->getVariant('test');
Expand All @@ -159,7 +159,7 @@ public function testCachingIsRespected()
'name' => 'some-variant',
'enabled' => true,
],
'impression_data' => false,
'impressionData' => false,
]);

$builder = new TestBuilder();
Expand Down Expand Up @@ -236,15 +236,15 @@ public function testPoisonedResponsesReturnFalse()
'name' => 'some-variant',
'enabled' => true,
],
'impression_data' => false,
'impressionData' => false,
],
[
'name' => 'test',
'enabled' => true,
'variant' => [
'poisoned' => 'variant',
],
'impression_data' => false,
'impressionData' => false,
],
[
'name' => 'test',
Expand All @@ -257,7 +257,7 @@ public function testPoisonedResponsesReturnFalse()
'value' => 'stuff',
],
],
'impression_data' => false,
'impressionData' => false,
],
];

Expand Down
4 changes: 2 additions & 2 deletions tests/Repository/DefaultUnleashProxyRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function test200ResponseResolvesCorrectly()
],
'enabled' => true,
],
'impression_data' => false,
'impressionData' => false,
])
),
]);
Expand Down Expand Up @@ -112,7 +112,7 @@ public function testCacheTtlIsRespected()
],
'enabled' => true,
],
'impression_data' => false,
'impressionData' => false,
]);

$mock = new MockHandler([
Expand Down
Loading