From eb40dc2d5d1ba9dcb7e85f3702026163da3f18d3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 3 Mar 2024 10:36:17 +0100 Subject: [PATCH] Add unit test for ShortUrlRedirectRuleService::saveRulesForShortUrl --- .../ShortUrlRedirectRuleServiceTest.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/module/Core/test/RedirectRule/ShortUrlRedirectRuleServiceTest.php b/module/Core/test/RedirectRule/ShortUrlRedirectRuleServiceTest.php index b0b6d4f2..103c6fd0 100644 --- a/module/Core/test/RedirectRule/ShortUrlRedirectRuleServiceTest.php +++ b/module/Core/test/RedirectRule/ShortUrlRedirectRuleServiceTest.php @@ -132,4 +132,40 @@ class ShortUrlRedirectRuleServiceTest extends TestCase self::assertCount(0, $result); } + + #[Test] + public function saveRulesForShortUrlDetachesAllEntitiesAndArrangesPriorities(): void + { + $shortUrl = ShortUrl::withLongUrl('https://example.com'); + $rules = [ + new ShortUrlRedirectRule($shortUrl, 8, 'https://example.com', new ArrayCollection([ + RedirectCondition::forLanguage('es-ES'), + RedirectCondition::forDevice(DeviceType::ANDROID), + ])), + new ShortUrlRedirectRule($shortUrl, 3, 'https://example.com', new ArrayCollection([ + RedirectCondition::forQueryParam('foo', 'bar'), + RedirectCondition::forQueryParam('bar', 'foo'), + ])), + new ShortUrlRedirectRule($shortUrl, 15, 'https://example.com', new ArrayCollection([ + RedirectCondition::forDevice(DeviceType::IOS), + ])), + ]; + + // Detach will be called 8 times: 3 rules + 5 conditions + $this->em->expects($this->exactly(8))->method('detach'); + $this->em->expects($this->once())->method('wrapInTransaction')->willReturnCallback( + fn (callable $callback) => $callback(), + ); + + // Persist will be called for each of the three rules. Their priorities should be consecutive starting at 1 + $cont = 0; + $this->em->expects($this->exactly(3))->method('persist')->with($this->callback( + function (ShortUrlRedirectRule $rule) use (&$cont): bool { + $cont++; + return $rule->jsonSerialize()['priority'] === $cont; + }, + )); + + $this->ruleService->saveRulesForShortUrl($shortUrl, $rules); + } }