Skip to content

Commit fac2ec2

Browse files
committed
fixes.
1 parent 137b967 commit fac2ec2

File tree

3 files changed

+72
-56
lines changed

3 files changed

+72
-56
lines changed

Diff for: src/Optimizely/Utils/CustomAttributeConditionEvaluator.php

+43-32
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ class CustomAttributeConditionEvaluator
2828

2929
const EXACT_MATCH_TYPE = 'exact';
3030
const EXISTS_MATCH_TYPE = 'exists';
31-
const GREATER_THAN_MATCH_TYPE = 'gt';
3231
const GREATER_THAN_EQUAL_TO_MATCH_TYPE = 'ge';
33-
const LESS_THAN_MATCH_TYPE = 'lt';
32+
const GREATER_THAN_MATCH_TYPE = 'gt';
3433
const LESS_THAN_EQUAL_TO_MATCH_TYPE = 'le';
35-
const SUBSTRING_MATCH_TYPE = 'substring';
34+
const LESS_THAN_MATCH_TYPE = 'lt';
3635
const SEMVER_EQ = 'semver_eq';
37-
const SEMVER_GT = 'semver_gt';
3836
const SEMVER_GE = 'semver_ge';
39-
const SEMVER_LT = 'semver_lt';
37+
const SEMVER_GT = 'semver_gt';
4038
const SEMVER_LE = 'semver_le';
39+
const SEMVER_LT = 'semver_lt';
40+
const SUBSTRING_MATCH_TYPE = 'substring';
4141

4242
/**
4343
* @var UserAttributes
@@ -78,9 +78,20 @@ protected function setNullForMissingKeys(array $leafCondition)
7878
*/
7979
protected function getMatchTypes()
8080
{
81-
return array(self::EXACT_MATCH_TYPE, self::EXISTS_MATCH_TYPE, self::GREATER_THAN_MATCH_TYPE, self::GREATER_THAN_EQUAL_TO_MATCH_TYPE,
82-
self::LESS_THAN_MATCH_TYPE, self::LESS_THAN_EQUAL_TO_MATCH_TYPE, self::SUBSTRING_MATCH_TYPE, self::SEMVER_EQ,
83-
self::SEMVER_GT, self::SEMVER_GE, self::SEMVER_LT, self::SEMVER_LE);
81+
return array(
82+
self::EXACT_MATCH_TYPE,
83+
self::EXISTS_MATCH_TYPE,
84+
self::GREATER_THAN_EQUAL_TO_MATCH_TYPE,
85+
self::GREATER_THAN_MATCH_TYPE,
86+
self::LESS_THAN_EQUAL_TO_MATCH_TYPE,
87+
self::LESS_THAN_MATCH_TYPE,
88+
self::SEMVER_EQ,
89+
self::SEMVER_GE,
90+
self::SEMVER_GT,
91+
self::SEMVER_LE,
92+
self::SEMVER_LT,
93+
self::SUBSTRING_MATCH_TYPE,
94+
);
8495
}
8596

8697
/**
@@ -95,16 +106,16 @@ protected function getEvaluatorByMatchType($matchType)
95106
$evaluatorsByMatchType = array();
96107
$evaluatorsByMatchType[self::EXACT_MATCH_TYPE] = 'exactEvaluator';
97108
$evaluatorsByMatchType[self::EXISTS_MATCH_TYPE] = 'existsEvaluator';
98-
$evaluatorsByMatchType[self::GREATER_THAN_MATCH_TYPE] = 'greaterThanEvaluator';
99109
$evaluatorsByMatchType[self::GREATER_THAN_EQUAL_TO_MATCH_TYPE] = 'greaterThanEqualToEvaluator';
100-
$evaluatorsByMatchType[self::LESS_THAN_MATCH_TYPE] = 'lessThanEvaluator';
110+
$evaluatorsByMatchType[self::GREATER_THAN_MATCH_TYPE] = 'greaterThanEvaluator';
101111
$evaluatorsByMatchType[self::LESS_THAN_EQUAL_TO_MATCH_TYPE] = 'lessThanEqualToEvaluator';
102-
$evaluatorsByMatchType[self::SUBSTRING_MATCH_TYPE] = 'substringEvaluator';
112+
$evaluatorsByMatchType[self::LESS_THAN_MATCH_TYPE] = 'lessThanEvaluator';
103113
$evaluatorsByMatchType[self::SEMVER_EQ] = 'semverEqualEvaluator';
104-
$evaluatorsByMatchType[self::SEMVER_GT] = 'semverGreaterThanEvaluator';
105114
$evaluatorsByMatchType[self::SEMVER_GE] = 'semverGreaterThanEqualToEvaluator';
106-
$evaluatorsByMatchType[self::SEMVER_LT] = 'semverLessThanEvaluator';
115+
$evaluatorsByMatchType[self::SEMVER_GT] = 'semverGreaterThanEvaluator';
107116
$evaluatorsByMatchType[self::SEMVER_LE] = 'semverLessThanEqualToEvaluator';
117+
$evaluatorsByMatchType[self::SEMVER_LT] = 'semverLessThanEvaluator';
118+
$evaluatorsByMatchType[self::SUBSTRING_MATCH_TYPE] = 'substringEvaluator';
108119

109120
return $evaluatorsByMatchType[$matchType];
110121
}
@@ -126,13 +137,13 @@ protected function isValueTypeValidForExactConditions($value)
126137
}
127138

128139
/**
129-
* returns result of SemVersionConditionEvaluator::compareVersion for given target and user versions.
140+
* Returns result of SemVersionConditionEvaluator::compareVersion for given target and user versions.
130141
*
131142
* @param object $condition
132143
*
133-
* @return null|int 0 if user's semver attribute is equal to the semver condition value,
134-
* 1 if user's semver attribute is greater than the semver condition value,
135-
* -1 if user's semver attribute is less than the semver condition value,
144+
* @return null|int 0 if user's version attribute is equal to the semver condition value,
145+
* 1 if user's version attribute is greater than the semver condition value,
146+
* -1 if user's version attribute is less than the semver condition value,
136147
* null if the condition value or user attribute value has an invalid type, or
137148
* if there is a mismatch between the user attribute type and the condition
138149
* value type.
@@ -444,9 +455,9 @@ protected function substringEvaluator($condition)
444455
*
445456
* @param object $condition
446457
*
447-
* @return boolean true if user's semver attribute is equal to the semver condition value,
448-
* false if the user's semver is greater or less than the semver condition value,
449-
* null if the semver condition value or user's semver attribute is invalid.
458+
* @return boolean true if user's version attribute is equal to the semver condition value,
459+
* false if the user's version attribute is greater or less than the semver condition value,
460+
* null if the semver condition value or user's version attribute is invalid.
450461
*/
451462
protected function semverEqualEvaluator($condition)
452463
{
@@ -462,9 +473,9 @@ protected function semverEqualEvaluator($condition)
462473
*
463474
* @param object $condition
464475
*
465-
* @return boolean true if user's semver attribute is greater than the semver condition value,
466-
* false if the user's semver is less than or equal to the semver condition value,
467-
* null if the semver condition value or user's semver attribute is invalid.
476+
* @return boolean true if user's version attribute is greater than the semver condition value,
477+
* false if the user's version attribute is less than or equal to the semver condition value,
478+
* null if the semver condition value or user's version attribute is invalid.
468479
*/
469480
protected function semverGreaterThanEvaluator($condition)
470481
{
@@ -480,9 +491,9 @@ protected function semverGreaterThanEvaluator($condition)
480491
*
481492
* @param object $condition
482493
*
483-
* @return boolean true if user's semver attribute is greater than or equal to the semver condition value,
484-
* false if the user's semver is less than the semver condition value,
485-
* null if the semver condition value or user's semver attribute is invalid.
494+
* @return boolean true if user's version attribute is greater than or equal to the semver condition value,
495+
* false if the user's version attribute is less than the semver condition value,
496+
* null if the semver condition value or user's version attribute is invalid.
486497
*/
487498
protected function semverGreaterThanEqualToEvaluator($condition)
488499
{
@@ -498,9 +509,9 @@ protected function semverGreaterThanEqualToEvaluator($condition)
498509
*
499510
* @param object $condition
500511
*
501-
* @return boolean true if user's semver attribute is less than the semver condition value,
502-
* false if the user's semver is greater than or equal to the semver condition value,
503-
* null if the semver condition value or user's semver attribute is invalid.
512+
* @return boolean true if user's version attribute is less than the semver condition value,
513+
* false if the user's version attribute is greater than or equal to the semver condition value,
514+
* null if the semver condition value or user's version attribute is invalid.
504515
*/
505516
protected function semverLessThanEvaluator($condition)
506517
{
@@ -516,9 +527,9 @@ protected function semverLessThanEvaluator($condition)
516527
*
517528
* @param object $condition
518529
*
519-
* @return boolean true if user's semver attribute is less than or equal to the semver condition value,
520-
* false if the user's semver is greater than the semver condition value,
521-
* null if the semver condition value or user's semver attribute is invalid.
530+
* @return boolean true if user's version attribute is less than or equal to the semver condition value,
531+
* false if the user's version attribute is greater than the semver condition value,
532+
* null if the semver condition value or user's version attribute is invalid.
522533
*/
523534
protected function semverLessThanEqualToEvaluator($condition)
524535
{

Diff for: src/Optimizely/Utils/SemVersionConditionEvaluator.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class SemVersionConditionEvaluator
2929
/**
3030
* compares targeted version with the provided user version.
3131
*
32-
* @param object $targetedVersion
33-
* @param object $userVersion
32+
* @param string $targetedVersion
33+
* @param string $userVersion
3434
* @param object $logger
3535
*
3636
* @return null|int 0 if user's semver attribute is equal to the semver condition value,
@@ -51,8 +51,8 @@ public static function compareVersion($targetedVersion, $userVersion, $logger)
5151
return null;
5252
}
5353
$userVersionPartsCount = count($userVersionParts);
54-
$isPreReleaseTargetedVersion = self::isPreRelease($targetedVersion);
55-
$isPreReleaseUserVersion = self::isPreRelease($userVersion);
54+
$isPreReleaseTargetedVersion = self::isPreReleaseVersion($targetedVersion);
55+
$isPreReleaseUserVersion = self::isPreReleaseVersion($userVersion);
5656

5757
// Up to the precision of targetedVersion, expect version to match exactly.
5858
for ($i = 0; $i < count($targetedVersionParts); $i++) {
@@ -85,6 +85,8 @@ public static function compareVersion($targetedVersion, $userVersion, $logger)
8585
return -1;
8686
}
8787
}
88+
// checking if user's version attribute is a prerelease and targeted version isn't
89+
// since pre-release versions have a lower precedence than the associated normal version
8890
if (!$isPreReleaseTargetedVersion && $isPreReleaseUserVersion) {
8991
return -1;
9092
}
@@ -112,9 +114,9 @@ private static function splitSemanticVersion($version, $logger)
112114
$targetSuffix = array();
113115

114116
$separator = null;
115-
if (self::isPreRelease($version)) {
117+
if (self::isPreReleaseVersion($version)) {
116118
$separator = self::PRE_RELEASE_SEPARATOR;
117-
} elseif (self::isBuild($version)) {
119+
} elseif (self::isBuildVersion($version)) {
118120
$separator = self::BUILD_SEPARATOR;
119121
}
120122

@@ -171,7 +173,7 @@ private static function splitSemanticVersion($version, $logger)
171173
*
172174
* @return bool true if given version is a prerelease.
173175
*/
174-
private static function isPreRelease($version)
176+
private static function isPreReleaseVersion($version)
175177
{
176178
//check if string contains prerelease seperator before build separator
177179
$preReleasePos = strpos($version, self::PRE_RELEASE_SEPARATOR);
@@ -193,7 +195,7 @@ private static function isPreRelease($version)
193195
*
194196
* @return bool true if given version is a build.
195197
*/
196-
private static function isBuild($version)
198+
private static function isBuildVersion($version)
197199
{
198200
// checks if string contains build seperator before prerelease separator
199201
$buildPos = strpos($version, self::BUILD_SEPARATOR);

Diff for: tests/UtilsTests/CustomAttributeConditionEvaluatorTest.php

+19-16
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ public function testSemVerEQMatcherReturnsTrueWhenAttributeValueIsEqualToConditi
18441844
}
18451845
}
18461846

1847-
public function testSemVerEQMatcherReturnsFalseOrFalseWhenAttributeValueIsNotEqualToConditionValueMajorOnly()
1847+
public function testSemVerEQMatcherReturnsFalseWhenAttributeValueIsNotEqualToConditionValueMajorOnly()
18481848
{
18491849
$semverEqCondition = [
18501850
'name' => 'semversion_eq',
@@ -1949,25 +1949,28 @@ public function testDifferentAttributeComplex()
19491949

19501950
public function testInvalidTargetVersionTypes()
19511951
{
1952-
$semverLeCondition = [
1953-
'name' => 'semversion_le',
1952+
$semverCondition = [
1953+
'name' => 'semversion',
19541954
'type' => 'custom_attribute',
1955-
'match' => 'semver_le',
19561955
];
19571956

19581957
$invalidTypeValues = [true, ["abc"], 10, ""];
1959-
1960-
foreach ($invalidTypeValues as $val) {
1961-
$semverLeCondition['value'] = $val;
1962-
$customAttrConditionEvaluator = new CustomAttributeConditionEvaluator(
1963-
['semversion_le' => "3"],
1964-
$this->loggerMock
1965-
);
1966-
$this->assertNull(
1967-
$customAttrConditionEvaluator->evaluate(
1968-
$semverLeCondition
1969-
)
1970-
);
1958+
$semverMatchTypes = ["semver_eq", "semver_gt", "semver_ge", "semver_lt", "semver_le"];
1959+
1960+
for ($i = 0; $i < count($semverMatchTypes); $i++) {
1961+
foreach ($invalidTypeValues as $val) {
1962+
$semverCondition['match'] = $semverMatchTypes[$i];
1963+
$semverCondition['value'] = $val;
1964+
$customAttrConditionEvaluator = new CustomAttributeConditionEvaluator(
1965+
['semversion' => "3"],
1966+
$this->loggerMock
1967+
);
1968+
$this->assertNull(
1969+
$customAttrConditionEvaluator->evaluate(
1970+
$semverCondition
1971+
)
1972+
);
1973+
}
19711974
}
19721975
}
19731976

0 commit comments

Comments
 (0)