|
15 | 15 | use Flagsmith\Engine\Utils\Collections\SegmentConditionModelList; |
16 | 16 | use Flagsmith\Engine\Utils\Collections\SegmentRuleModelList; |
17 | 17 | use Flagsmith\Engine\Utils\Hashing; |
| 18 | +use Flagsmith\Models\Segment; |
18 | 19 | use FlagsmithTest\Engine\Fixtures; |
19 | 20 |
|
20 | 21 | class SegmentEvaluatorTest extends TestCase |
@@ -188,7 +189,6 @@ public function segmentSplitValues() |
188 | 189 | */ |
189 | 190 | public function testIdentityInSegmentPercentageSplit($segmentSplitValue, $identityHashedPercentage, $expectedResult) |
190 | 191 | { |
191 | | - $this->assertTrue(true); |
192 | 192 | $percentageSplitCondition = (new SegmentConditionModel()) |
193 | 193 | ->withOperator(SegmentConditions::PERCENTAGE_SPLIT) |
194 | 194 | ->withValue("{$segmentSplitValue}"); |
@@ -216,4 +216,46 @@ public function testIdentityInSegmentPercentageSplit($segmentSplitValue, $identi |
216 | 216 |
|
217 | 217 | $this->assertEquals($result, $expectedResult); |
218 | 218 | } |
| 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 | + } |
219 | 261 | } |
0 commit comments