Skip to content

Commit 20d4034

Browse files
Merge pull request #22 from optimizely/aliabbasrizvi/improve_test_coverage
More unit tests
2 parents 94e6a99 + 8e04299 commit 20d4034

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

tests/OptimizelyTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,40 @@ public function testActivateInvalidAttributes()
275275
$this->assertNull($optlyObject->activate('test_experiment', 'test_user', 42));
276276
}
277277

278+
public function testActivateUserInNoVariation()
279+
{
280+
$userAttributes = [
281+
'device_type' => 'iPhone',
282+
'company' => 'Optimizely',
283+
'location' => 'San Francisco'
284+
];
285+
286+
$this->eventBuilderMock->expects($this->never())
287+
->method('createImpressionEvent');
288+
289+
$this->loggerMock->expects($this->exactly(3))
290+
->method('log');
291+
$this->loggerMock->expects($this->at(0))
292+
->method('log')
293+
->with(Logger::DEBUG, 'Assigned bucket 8495 to user "not_in_variation_user".');
294+
$this->loggerMock->expects($this->at(1))
295+
->method('log')
296+
->with(Logger::INFO,
297+
'User "not_in_variation_user" is in no variation.');
298+
$this->loggerMock->expects($this->at(2))
299+
->method('log')
300+
->with(Logger::INFO, 'Not activating user "not_in_variation_user".');
301+
302+
$optlyObject = new Optimizely($this->datafile, new ValidEventDispatcher(), $this->loggerMock);
303+
304+
$eventBuilder = new \ReflectionProperty(Optimizely::class, '_eventBuilder');
305+
$eventBuilder->setAccessible(true);
306+
$eventBuilder->setValue($optlyObject, $this->eventBuilderMock);
307+
308+
// Call activate
309+
$this->assertNull($optlyObject->activate('test_experiment', 'not_in_variation_user', $userAttributes));
310+
}
311+
278312
public function testActivateNoAudienceNoAttributes()
279313
{
280314
$this->eventBuilderMock->expects($this->once())

tests/UtilsTests/ConditionEvaluatorTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ConditionEvaluatorTest extends \PHPUnit_Framework_TestCase
2929
public function setUp()
3030
{
3131
$decoder = new ConditionDecoder();
32-
$conditions = "[\"and\", [\"or\", [\"or\", {\"name\": \"device_type\", \"type\": \"custom_attribute\", \"value\": \"iPhone\"}]], [\"or\", [\"or\", {\"name\": \"location\", \"type\": \"custom_attribute\", \"value\": \"San Francisco\"}]]]";
32+
$conditions = "[\"and\", [\"or\", [\"or\", {\"name\": \"device_type\", \"type\": \"custom_attribute\", \"value\": \"iPhone\"}]], [\"or\", [\"or\", {\"name\": \"location\", \"type\": \"custom_attribute\", \"value\": \"San Francisco\"}]], [\"or\", [\"not\", [\"or\", {\"name\": \"browser\", \"type\": \"custom_attribute\", \"value\": \"Firefox\"}]]]]";
3333
$decoder->deserializeAudienceConditions($conditions);
3434

3535
$this->conditionsList = $decoder->getConditionsList();
@@ -41,7 +41,7 @@ public function testEvaluateConditionsMatch()
4141
$userAttributes = [
4242
'device_type' => 'iPhone',
4343
'location' => 'San Francisco',
44-
'browser' => 'chrome'
44+
'browser' => 'Chrome'
4545
];
4646

4747
$this->assertTrue($this->conditionEvaluator->evaluate($this->conditionsList, $userAttributes));
@@ -51,8 +51,9 @@ public function testEvaluateConditionsMatch()
5151
public function testEvaluateConditionsDoNotMatch()
5252
{
5353
$userAttributes = [
54+
'device_type' => 'iPhone',
5455
'location' => 'San Francisco',
55-
'browser' => 'chrome'
56+
'browser' => 'Firefox'
5657
];
5758

5859
$this->assertFalse($this->conditionEvaluator->evaluate($this->conditionsList, $userAttributes));

0 commit comments

Comments
 (0)