Skip to content

Commit

Permalink
Rashid/bucket variation bug (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
msohailhussain authored and mikeproeng37 committed Jun 1, 2018
1 parent c0123f6 commit ad1e98a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/Optimizely/Bucketer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017, Optimizely
* Copyright 2016-2018, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -127,21 +127,21 @@ private function findBucket($bucketingId, $userId, $parentId, $trafficAllocation
public function bucket(ProjectConfig $config, Experiment $experiment, $bucketingId, $userId)
{
if (is_null($experiment->getKey())) {
return new Variation();
return null;
}

// Determine if experiment is in a mutually exclusive group.
if ($experiment->isInMutexGroup()) {
$group = $config->getGroup($experiment->getGroupId());

if (is_null($group->getId())) {
return new Variation();
return null;
}

$userExperimentId = $this->findBucket($bucketingId, $userId, $group->getId(), $group->getTrafficAllocation());
if (empty($userExperimentId)) {
$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no experiment.', $userId));
return new Variation();
return null;
}

if ($userExperimentId != $experiment->getId()) {
Expand All @@ -154,7 +154,7 @@ public function bucket(ProjectConfig $config, Experiment $experiment, $bucketing
$experiment->getGroupId()
)
);
return new Variation();
return null;
}

$this->_logger->log(
Expand Down Expand Up @@ -186,6 +186,6 @@ public function bucket(ProjectConfig $config, Experiment $experiment, $bucketing
}

$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no variation.', $userId));
return new Variation();
return null;
}
}
26 changes: 9 additions & 17 deletions tests/BucketerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017, Optimizely
* Copyright 2016-2018, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -107,8 +107,7 @@ public function testBucketValidExperimentNotInGroup()
->method('log')
->with(Logger::INFO, 'User "testUserId" is in no variation.');

$this->assertEquals(
new Variation(),
$this->assertNull(
$bucketer->bucket(
$this->config,
$this->config->getExperimentFromKey('test_experiment'),
Expand Down Expand Up @@ -167,8 +166,7 @@ public function testBucketValidExperimentNotInGroup()
->method('log')
->with(Logger::INFO, 'User "testUserId" is in no variation.');

$this->assertEquals(
new Variation(),
$this->assertNull(
$bucketer->bucket(
$this->config,
$this->config->getExperimentFromKey('test_experiment'),
Expand Down Expand Up @@ -271,8 +269,7 @@ public function testBucketValidExperimentInGroup()
->method('log')
->with(Logger::INFO, 'User "testUserId" is not in experiment group_experiment_1 of group 7722400015.');

$this->assertEquals(
new Variation(),
$this->assertNull(
$bucketer->bucket(
$this->config,
$this->config->getExperimentFromKey('group_experiment_1'),
Expand All @@ -290,8 +287,7 @@ public function testBucketValidExperimentInGroup()
->method('log')
->with(Logger::INFO, 'User "testUserId" is in no experiment.');

$this->assertEquals(
new Variation(),
$this->assertNull(
$bucketer->bucket(
$this->config,
$this->config->getExperimentFromKey('group_experiment_1'),
Expand All @@ -308,8 +304,7 @@ public function testBucketValidExperimentInGroup()
$this->loggerMock->expects($this->at(1))
->method('log')
->with(Logger::INFO, 'User "testUserId" is in no experiment.');
$this->assertEquals(
new Variation(),
$this->assertNull(
$bucketer->bucket(
$this->config,
$this->config->getExperimentFromKey('group_experiment_1'),
Expand All @@ -325,8 +320,7 @@ public function testBucketInvalidExperiment()
$this->loggerMock->expects($this->never())
->method('log');

$this->assertEquals(
new Variation(),
$this->assertNull(
$bucketer->bucket($this->config, new Experiment(), $this->testBucketingIdControl, $this->testUserId)
);
}
Expand Down Expand Up @@ -356,8 +350,7 @@ public function testBucketVariationInvalidExperimentsWithBucketingId()
$bucketer = new TestBucketer($this->loggerMock);
$bucketer->setBucketValues([1000, 3000, 7000, 9000]);

$this->assertEquals(
new Variation(),
$this->assertNull(
$bucketer->bucket(
$this->config,
$this->config->getExperimentFromKey('invalid_experiment'),
Expand Down Expand Up @@ -430,8 +423,7 @@ public function testBucketWithRolloutRule()
)
);

$this->assertEquals(
new Variation(),
$this->assertNull(
$bucketer->bucket(
$this->config,
$rollout_rule,
Expand Down

0 comments on commit ad1e98a

Please sign in to comment.