Skip to content

Commit 451dc1c

Browse files
oakbanimikeproeng37
authored andcommitted
Parse revenue value (#91)
1 parent 330e496 commit 451dc1c

File tree

4 files changed

+108
-30
lines changed

4 files changed

+108
-30
lines changed

src/Optimizely/Event/Builder/EventBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private function getConversionParams($config, $eventKey, $experimentVariationMap
203203
];
204204

205205
if (!is_null($eventTags)) {
206-
$revenue = EventTagUtils::getRevenueValue($eventTags);
206+
$revenue = EventTagUtils::getRevenueValue($eventTags, $this->_logger);
207207
if (!is_null($revenue)) {
208208
$singleSnapshot[EVENTS][0][EventTagUtils::REVENUE_EVENT_METRIC_NAME] = $revenue;
209209
}

src/Optimizely/Utils/EventTagUtils.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace Optimizely\Utils;
1919

2020
use Optimizely\Logger\LoggerInterface;
21+
use Optimizely\Logger\NoOpLogger;
2122
use Monolog\Logger;
2223

2324
class EventTagUtils
@@ -31,30 +32,45 @@ class EventTagUtils
3132

3233
/**
3334
* Grab the revenue value from the event tags. "revenue" is a reserved keyword.
34-
* The revenue value must be an integer, not a numeric string.
35+
* The value will be parsed to an integer if possible.
36+
* Example:
37+
* 4.0 or "4.0" will be parsed to int(4).
38+
* 4.1 will not be parsed and the method will return null.
3539
*
3640
* @param $eventTags array Representing metadata associated with the event.
3741
* @return integer Revenue value as an integer number or null if revenue can't be retrieved from the event tags
3842
*/
39-
public static function getRevenueValue($eventTags)
43+
public static function getRevenueValue($eventTags, $logger)
4044
{
4145
if (!$eventTags) {
46+
$logger->log(Logger::DEBUG, "Event tags is undefined.");
4247
return null;
4348
}
4449
if (!is_array($eventTags)) {
50+
$logger->log(Logger::DEBUG, "Event tags is not a dictionary.");
4551
return null;
4652
}
4753

4854
if (!isset($eventTags[self::REVENUE_EVENT_METRIC_NAME])) {
55+
$logger->log(Logger::DEBUG, "The revenue key is not defined in the event tags or is null.");
56+
return null;
57+
}
58+
59+
$rawValue = $eventTags[self::REVENUE_EVENT_METRIC_NAME];
60+
61+
if (!is_numeric($rawValue)) {
62+
$logger->log(Logger::DEBUG, "Revenue value is not an integer or float, or is not a numeric string.");
4963
return null;
5064
}
5165

52-
$raw_value = $eventTags[self::REVENUE_EVENT_METRIC_NAME];
53-
if (!is_int($raw_value)) {
66+
if ($rawValue != intval($rawValue)) {
67+
$logger->log(Logger::DEBUG, "Revenue value couldn't be parsed as an integer.");
5468
return null;
5569
}
5670

57-
return $raw_value;
71+
$rawValue = intval($rawValue);
72+
$logger->log(Logger::INFO, "The revenue value {$rawValue} will be sent to results.");
73+
return $rawValue;
5874
}
5975

6076
/**

tests/EventTests/EventBuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2017, Optimizely
3+
* Copyright 2016-2018, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -518,7 +518,7 @@ public function testCreateConversionEventNoAttributesWithInvalidValue()
518518
'uuid'=> $this->uuid,
519519
'key'=> 'purchase',
520520
'tags' => [
521-
'revenue' => '42',
521+
'revenue' => '42.5',
522522
'non-revenue' => 'definitely',
523523
'value' => 'invalid value'
524524
]
@@ -539,7 +539,7 @@ public function testCreateConversionEventNoAttributesWithInvalidValue()
539539
$this->testUserId,
540540
null,
541541
array(
542-
'revenue' => '42',
542+
'revenue' => '42.5',
543543
'non-revenue' => 'definitely',
544544
'value' => 'invalid value'
545545
)

tests/UtilsTests/EventTagUtilsTest.php

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,99 @@ protected function setUp()
3737
->getMock();
3838
}
3939

40-
public function testGetRevenueValueInvalidArgs()
40+
public function testGetRevenueValueWithUndefinedTags()
4141
{
42-
$this->assertNull(EventTagUtils::getRevenueValue(null));
43-
$this->assertNull(EventTagUtils::getRevenueValue(0.5));
44-
$this->assertNull(EventTagUtils::getRevenueValue(65536));
45-
$this->assertNull(EventTagUtils::getRevenueValue(9223372036854775807));
46-
$this->assertNull(EventTagUtils::getRevenueValue('65536'));
47-
$this->assertNull(EventTagUtils::getRevenueValue(true));
48-
$this->assertNull(EventTagUtils::getRevenueValue(false));
42+
$this->loggerMock->expects($this->exactly(3))
43+
->method('log')
44+
->with(Logger::DEBUG, 'Event tags is undefined.');
45+
46+
$this->assertNull(EventTagUtils::getRevenueValue(null, $this->loggerMock));
47+
$this->assertNull(EventTagUtils::getRevenueValue(array(), $this->loggerMock));
48+
$this->assertNull(EventTagUtils::getRevenueValue(false, $this->loggerMock));
49+
}
50+
51+
public function testGetRevenueValueWithNonDictionaryTags()
52+
{
53+
$this->loggerMock->expects($this->exactly(5))
54+
->method('log')
55+
->with(Logger::DEBUG, 'Event tags is not a dictionary.');
56+
57+
$this->assertNull(EventTagUtils::getRevenueValue(0.5, $this->loggerMock));
58+
$this->assertNull(EventTagUtils::getRevenueValue(65536, $this->loggerMock));
59+
$this->assertNull(EventTagUtils::getRevenueValue(9223372036854775807, $this->loggerMock));
60+
$this->assertNull(EventTagUtils::getRevenueValue('65536', $this->loggerMock));
61+
$this->assertNull(EventTagUtils::getRevenueValue(true, $this->loggerMock));
4962
}
63+
64+
5065
public function testGetRevenueValueNoRevenueTag()
5166
{
52-
$this->assertNull(EventTagUtils::getRevenueValue(array()));
53-
$this->assertNull(EventTagUtils::getRevenueValue(array('non-revenue' => 42)));
67+
$this->loggerMock->expects($this->exactly(2))
68+
->method('log')
69+
->with(Logger::DEBUG, "The revenue key is not defined in the event tags or is null.");
70+
71+
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => null), $this->loggerMock));
72+
$this->assertNull(EventTagUtils::getRevenueValue(array('non-revenue' => 42), $this->loggerMock));
73+
}
74+
75+
public function testGetRevenueValueWithNonNumericRevenueTag()
76+
{
77+
$this->loggerMock->expects($this->exactly(4))
78+
->method('log')
79+
->with(Logger::DEBUG, "Revenue value is not an integer or float, or is not a numeric string.");
80+
81+
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => 'optimizely'), $this->loggerMock));
82+
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => true), $this->loggerMock));
83+
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => false), $this->loggerMock));
84+
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => array(1, 2, 3)), $this->loggerMock));
5485
}
5586

56-
public function testGetRevenueValueWithInvalidRevenueTag()
87+
public function testGetRevenueValueWithNonParsableRevenueTag()
5788
{
58-
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => null)));
59-
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => 0.5)));
60-
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => '65536')));
61-
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => true)));
62-
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => false)));
63-
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => array(1, 2, 3))));
89+
$this->loggerMock->expects($this->exactly(2))
90+
->method('log')
91+
->with(Logger::DEBUG, "Revenue value couldn't be parsed as an integer.");
92+
93+
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => 0.5), $this->loggerMock));
94+
$this->assertNull(EventTagUtils::getRevenueValue(array('revenue' => "42.5"), $this->loggerMock));
6495
}
6596

66-
public function testGetRevenueValueWithRevenueTag()
97+
public function testGetRevenueValueWithValidRevenueTag()
6798
{
68-
$this->assertSame(65536, EventTagUtils::getRevenueValue(array('revenue' => 65536)));
69-
$this->assertSame(9223372036854775807, EventTagUtils::getRevenueValue(array('revenue' => 9223372036854775807)));
70-
$this->assertSame(0, EventTagUtils::getRevenueValue(array('revenue' => 0)));
99+
$this->loggerMock->expects($this->at(0))
100+
->method('log')
101+
->with(Logger::INFO, "The revenue value 65536 will be sent to results.");
102+
$this->assertSame(65536, EventTagUtils::getRevenueValue(array('revenue' => 65536), $this->loggerMock));
103+
104+
$this->loggerMock->expects($this->at(0))
105+
->method('log')
106+
->with(Logger::INFO, "The revenue value 65536 will be sent to results.");
107+
$this->assertSame(65536, EventTagUtils::getRevenueValue(array('revenue' => "65536"), $this->loggerMock));
108+
109+
$this->loggerMock->expects($this->at(0))
110+
->method('log')
111+
->with(Logger::INFO, "The revenue value 65536 will be sent to results.");
112+
$this->assertSame(65536, EventTagUtils::getRevenueValue(array('revenue' => 65536.0), $this->loggerMock));
113+
114+
$this->loggerMock->expects($this->at(0))
115+
->method('log')
116+
->with(Logger::INFO, "The revenue value 65536 will be sent to results.");
117+
$this->assertSame(65536, EventTagUtils::getRevenueValue(array('revenue' => "65536.0"), $this->loggerMock));
118+
119+
$this->loggerMock->expects($this->at(0))
120+
->method('log')
121+
->with(Logger::INFO, "The revenue value 9223372036854775807 will be sent to results.");
122+
$this->assertSame(9223372036854775807, EventTagUtils::getRevenueValue(array('revenue' => 9223372036854775807), $this->loggerMock));
123+
124+
$this->loggerMock->expects($this->at(0))
125+
->method('log')
126+
->with(Logger::INFO, "The revenue value 0 will be sent to results.");
127+
$this->assertSame(0, EventTagUtils::getRevenueValue(array('revenue' => 0), $this->loggerMock));
128+
129+
$this->loggerMock->expects($this->at(0))
130+
->method('log')
131+
->with(Logger::INFO, "The revenue value 0 will be sent to results.");
132+
$this->assertSame(0, EventTagUtils::getRevenueValue(array('revenue' => 0.0), $this->loggerMock));
71133
}
72134

73135
public function testGetNumericValueWithUndefinedTags()

0 commit comments

Comments
 (0)