Skip to content

Commit 189ce45

Browse files
committed
fix(segments): use django id if present
1 parent 52388a6 commit 189ce45

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ vendor
22
package-lock.json
33
composer.lock
44
/node_modules/
5-
.env
5+
.env
6+
.php-cs-fixer.cache

.php-cs-fixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
[
1616
'@PSR12' => true,
1717
'single_quote' => true,
18-
'no_trailing_comma_in_singleline_array' => true,
18+
'no_trailing_comma_in_singleline' => true,
1919
'array_indentation' => true,
2020
'phpdoc_indent' => true,
2121
]

src/Engine/Identities/IdentityModel.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class IdentityModel
1919
public IdentityFeaturesList $identity_features;
2020
public IdentityTraitList $identity_traits;
2121
public string $identity_uuid;
22-
public int $djangoId;
22+
public ?int $django_id = null;
2323

2424
protected array $keys = [
2525
'identity_features' => 'Flagsmith\Engine\Utils\Collections\IdentityFeaturesList',
@@ -76,19 +76,19 @@ public function withIdentifier(string $identifier): self
7676
* Get the django ID.
7777
* @return int
7878
*/
79-
public function getDjangoId(): int
79+
public function getDjangoId(): ?int
8080
{
8181
return $this->django_id;
8282
}
8383

8484
/**
8585
* Build with Django ID.
86-
* @param int $djangoId
86+
* @param int $django_id
8787
* @return IdentityModel
8888
*/
89-
public function withDjangoId(int $djangoId): self
89+
public function withDjangoId(int $django_id): self
9090
{
91-
return $this->with('django_id', $djangoId);
91+
return $this->with('django_id', $django_id);
9292
}
9393

9494
/**

src/Engine/Segments/SegmentEvaluator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static function evaluateIdentityInSegment(
4545
array $overrideTraits = null
4646
): bool {
4747
$rulesCount = count($segment->getRules());
48+
$identityId = ($identity->getDjangoId() != null) ? $identity->getDjangoId() : $identity->compositeKey();
4849

4950
if (empty($rulesCount)) {
5051
return false;
@@ -55,7 +56,7 @@ public static function evaluateIdentityInSegment(
5556
$overrideTraits ?? $identity->getIdentityTraits()->getArrayCopy(),
5657
$rule,
5758
$segment->getId(),
58-
$identity->compositeKey()
59+
$identityId
5960
);
6061

6162
if (!$matchesRule) {

tests/Engine/Unit/Segments/SegmentEvaluatorTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Flagsmith\Engine\Utils\Collections\SegmentConditionModelList;
1616
use Flagsmith\Engine\Utils\Collections\SegmentRuleModelList;
1717
use Flagsmith\Engine\Utils\Hashing;
18+
use Flagsmith\Models\Segment;
1819
use FlagsmithTest\Engine\Fixtures;
1920

2021
class SegmentEvaluatorTest extends TestCase
@@ -188,7 +189,6 @@ public function segmentSplitValues()
188189
*/
189190
public function testIdentityInSegmentPercentageSplit($segmentSplitValue, $identityHashedPercentage, $expectedResult)
190191
{
191-
$this->assertTrue(true);
192192
$percentageSplitCondition = (new SegmentConditionModel())
193193
->withOperator(SegmentConditions::PERCENTAGE_SPLIT)
194194
->withValue("{$segmentSplitValue}");
@@ -216,4 +216,46 @@ public function testIdentityInSegmentPercentageSplit($segmentSplitValue, $identi
216216

217217
$this->assertEquals($result, $expectedResult);
218218
}
219+
220+
public function testIdentityInSegmentPercentageSplitUsesDjangoIdIfPresent()
221+
{
222+
$percentageSplitCondition = (new SegmentConditionModel())
223+
->withOperator(SegmentConditions::PERCENTAGE_SPLIT)
224+
->withValue('10');
225+
226+
$segmentRule = (new SegmentRuleModel())
227+
->withType(SegmentRules::ALL_RULE)
228+
->withConditions(
229+
new SegmentConditionModelList([$percentageSplitCondition])
230+
);
231+
232+
$segmentModel = (new SegmentModel())
233+
->withId(1)
234+
->withName('splitty')
235+
->withRules(
236+
new SegmentRuleModelList([$segmentRule])
237+
);
238+
239+
$identityModel = (new IdentityModel())
240+
->withIdentifier('identifier_1')
241+
->withEnvironmentApiKey(Fixtures::environment()->getApiKey())
242+
->withCreatedDate(new \DateTime('now'))
243+
->withDjangoId(1);
244+
245+
$hashingStub = $this->createMock(Hashing::class);
246+
$hashingStub
247+
->method('getHashedPercentageForObjectIds')
248+
->will($this->returnValue(1));
249+
250+
$hashingStub
251+
->expects($this->once())
252+
->method('getHashedPercentageForObjectIds')
253+
->with($this->identicalTo(array($segmentModel->getId(), $identityModel->getDjangoId())));
254+
255+
SegmentEvaluator::setHashObject($hashingStub);
256+
257+
$result = SegmentEvaluator::evaluateIdentityInSegment($identityModel, $segmentModel);
258+
259+
$this->assertEquals($result, true);
260+
}
219261
}

0 commit comments

Comments
 (0)