Skip to content

Commit

Permalink
Merge branch 'master' into jctong/improve_build_time
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeproeng37 committed Jan 15, 2019
2 parents 22fa48b + 513039d commit 8c606b4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/Optimizely/ProjectConfig.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016, 2018, Optimizely
* Copyright 2016, 2018-2019 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 @@ -455,7 +455,7 @@ public function getEvent($eventKey)
* @param $audienceId string ID of the audience.
*
* @return Audience Entity corresponding to the ID.
* Dummy entity is returned if ID is invalid.
* Null is returned if ID is invalid.
*/
public function getAudience($audienceId)
{
Expand All @@ -465,7 +465,8 @@ public function getAudience($audienceId)

$this->_logger->log(Logger::ERROR, sprintf('Audience ID "%s" is not in datafile.', $audienceId));
$this->_errorHandler->handleError(new InvalidAudienceException('Provided audience is not in datafile.'));
return new Audience();

return null;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Optimizely/Utils/Validator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2018, Optimizely
* Copyright 2016-2019, 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 @@ -160,7 +160,12 @@ public static function isUserInExperiment($config, $experiment, $userAttributes)

$evaluateAudience = function($audienceId) use ($config, $evaluateCustomAttr) {
$conditionTreeEvaluator = new ConditionTreeEvaluator();

$audience = $config->getAudience($audienceId);
if ($audience === null) {
return null;
}

return $conditionTreeEvaluator->evaluate($audience->getConditionsList(), $evaluateCustomAttr);
};

Expand Down
4 changes: 2 additions & 2 deletions tests/ProjectConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2018, Optimizely
* Copyright 2016-2019, 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 @@ -546,7 +546,7 @@ public function testGetAudienceInvalidKey()
->method('handleError')
->with(new InvalidAudienceException('Provided audience is not in datafile.'));

$this->assertEquals(new Audience(), $this->config->getAudience('invalid_id'));
$this->assertNull($this->config->getAudience('invalid_id'));
}

public function testGetAttributeValidKey()
Expand Down
21 changes: 20 additions & 1 deletion tests/UtilsTests/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2018, Optimizely
* Copyright 2016-2019, 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 @@ -346,6 +346,25 @@ public function testIsUserInExperimentWithAudienceConditionsSetToAudienceIdStrin
);
}

public function testIsUserInExperimentWithUnknownAudienceId()
{
$config = new ProjectConfig(DATAFILE, $this->loggerMock, new NoOpErrorHandler());
$experiment = $config->getExperimentFromKey('test_experiment');

// Both audience Ids and audience conditions exist. Audience Ids is ignored.
$experiment->setAudienceIds([]);
$experiment->setAudienceConditions(["or", "unknown_audience_id", "7718080042"]);

// User qualifies for audience with ID "7718080042".
$this->assertTrue(
Validator::isUserInExperiment(
$config,
$experiment,
['device_type' => 'iPhone', 'location' => 'San Francisco']
)
);
}

// test that isUserInExperiment evaluates simple audience.
public function testIsUserInExperimentWithSimpleAudience()
{
Expand Down

0 comments on commit 8c606b4

Please sign in to comment.