Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions packages/Php-sdk/src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ public function runExperience(string $experienceKey, ?BucketingAttributes $attri
}

$visitorProperties = $this->getVisitorProperties($attributes?->getVisitorProperties());
$forwardedData = $attributes ? get_object_vars($attributes) : [];
$forwardedData['visitorProperties'] = $visitorProperties;
$forwardedData['environment'] = $forwardedData['environment'] ?? $this->environment;
$result = $this->experienceManager->selectVariation(
$this->visitorId,
$experienceKey,
new BucketingAttributes([
'visitorProperties' => $visitorProperties,
'locationProperties' => $attributes?->getLocationProperties(),
'updateVisitorProperties' => $attributes?->getUpdateVisitorProperties(),
'environment' => $attributes?->getEnvironment() ?? $this->environment,
])
new BucketingAttributes($forwardedData)
Comment thread
abbaseya marked this conversation as resolved.
);

if ($result === null
Expand Down Expand Up @@ -153,15 +151,13 @@ public function runExperiences(?BucketingAttributes $attributes = null): array
}

$visitorProperties = $this->getVisitorProperties($attributes?->getVisitorProperties());
$forwardedData = $attributes ? get_object_vars($attributes) : [];
$forwardedData['visitorProperties'] = $visitorProperties;
$forwardedData['environment'] = $forwardedData['environment'] ?? $this->environment;

$bucketedVariations = $this->experienceManager->selectVariations(
$this->visitorId,
new BucketingAttributes([
'visitorProperties' => $visitorProperties,
'locationProperties' => $attributes?->getLocationProperties(),
'updateVisitorProperties' => $attributes?->getUpdateVisitorProperties(),
'environment' => $attributes?->getEnvironment() ?? $this->environment,
])
new BucketingAttributes($forwardedData)
Comment thread
abbaseya marked this conversation as resolved.
);

$dtos = [];
Expand Down
82 changes: 82 additions & 0 deletions packages/Php-sdk/tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,88 @@ public function testGetVariationsAcrossAllExperiences(): void
}
}

public function testRunExperienceForwardsAllBucketingAttributes(): void
{
$realManager = $this->experienceManager;
$captured = null;

$spy = $this->createMock(\ConvertSdk\Interfaces\ExperienceManagerInterface::class);
$spy->method('selectVariation')->willReturnCallback(
function (string $visitorId, string $experienceKey, BucketingAttributes $attributes) use ($realManager, &$captured) {
$captured = $attributes;
return $realManager->selectVariation($visitorId, $experienceKey, $attributes);
}
);

$context = new Context(
$this->config,
$this->visitorId,
$this->eventManager,
$spy,
$this->featureManager,
$this->dataManager,
$this->segmentsManager,
$this->apiManager,
);

$context->runExperience('test-experience-ab-fullstack-2', new BucketingAttributes([
'locationProperties' => ['url' => 'https://convert.com/'],
'visitorProperties' => ['varName3' => 'something'],
'enableTracking' => false,
'forceVariationId' => '100299461',
'ignoreLocationProperties' => true,
'updateVisitorProperties' => true,
]));

$this->assertNotNull($captured);
$this->assertFalse($captured->enableTracking);
$this->assertSame('100299461', $captured->forceVariationId);
$this->assertTrue($captured->ignoreLocationProperties);
$this->assertTrue($captured->updateVisitorProperties);
$this->assertSame(['url' => 'https://convert.com/'], $captured->locationProperties);
}

public function testRunExperiencesForwardsAllBucketingAttributes(): void
{
$realManager = $this->experienceManager;
$captured = null;

$spy = $this->createMock(\ConvertSdk\Interfaces\ExperienceManagerInterface::class);
$spy->method('selectVariations')->willReturnCallback(
function (string $visitorId, BucketingAttributes $attributes) use ($realManager, &$captured) {
$captured = $attributes;
return $realManager->selectVariations($visitorId, $attributes);
}
);

$context = new Context(
$this->config,
$this->visitorId,
$this->eventManager,
$spy,
$this->featureManager,
$this->dataManager,
$this->segmentsManager,
$this->apiManager,
);

$context->runExperiences(new BucketingAttributes([
'locationProperties' => ['url' => 'https://convert.com/'],
'visitorProperties' => ['varName3' => 'something'],
'enableTracking' => false,
'forceVariationId' => '100299461',
'ignoreLocationProperties' => true,
'updateVisitorProperties' => true,
]));

$this->assertNotNull($captured);
$this->assertFalse($captured->enableTracking);
$this->assertSame('100299461', $captured->forceVariationId);
$this->assertTrue($captured->ignoreLocationProperties);
$this->assertTrue($captured->updateVisitorProperties);
$this->assertSame(['url' => 'https://convert.com/'], $captured->locationProperties);
}

public function testGetSingleFeatureWithStatus(): void
{
$featureKey = 'feature-2';
Expand Down
Loading