Skip to content

Commit 8a1dd1f

Browse files
committed
Fix tests
1 parent 65253d5 commit 8a1dd1f

File tree

5 files changed

+82
-83
lines changed

5 files changed

+82
-83
lines changed

tests/Unit/Domain/Messaging/Service/Manager/BounceManagerTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public function testUpdateChangesFieldsAndSaves(): void
125125

126126
public function testLinkUserMessageBounceFlushesAndSetsFields(): void
127127
{
128-
$bounce = new Bounce();
129-
$this->setId($bounce, 77);
128+
$bounce = $this->createMock(Bounce::class);
129+
$bounce->method('getId')->willReturn(77);
130130

131131
$this->entityManager->expects($this->once())->method('flush');
132132

@@ -202,10 +202,4 @@ public function testAnnounceDeletionModeLogsCorrectMessage(): void
202202
$this->manager->announceDeletionMode(true);
203203
$this->manager->announceDeletionMode(false);
204204
}
205-
206-
private function setId(object $entity, int $id): void
207-
{
208-
$ref = new \ReflectionProperty($entity, 'id');
209-
$ref->setValue($entity, $id);
210-
}
211205
}

tests/Unit/Domain/Messaging/Service/Manager/BounceRuleManagerTest.php

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,32 @@ protected function setUp(): void
2323
{
2424
$this->regexRepository = $this->createMock(BounceRegexRepository::class);
2525
$this->relationRepository = $this->createMock(BounceRegexBounceRepository::class);
26-
$this->manager = new BounceRuleManager($this->regexRepository, $this->relationRepository);
26+
$this->manager = new BounceRuleManager(
27+
repository: $this->regexRepository,
28+
bounceRelationRepository: $this->relationRepository,
29+
);
2730
}
2831

2932
public function testLoadActiveRulesMapsRowsAndSkipsInvalid(): void
3033
{
31-
$valid = new BounceRegex(regex: 'user unknown', regexHash: md5('user unknown'), action: 'delete');
32-
// invalids: no regex, no action, no id
33-
$noRegex = new BounceRegex(regex: '', regexHash: md5(''), action: 'delete');
34-
$noAction = new BounceRegex(regex: 'pattern', regexHash: md5('pattern'), action: '');
35-
$noId = new BounceRegex(regex: 'has no id', regexHash: md5('has no id'), action: 'keep');
36-
37-
// Simulate id assignment for only some of them
38-
$this->setId($valid, 1);
39-
$this->setId($noRegex, 2);
40-
$this->setId($noAction, 3);
41-
// $noId intentionally left without id
34+
$valid = $this->createMock(BounceRegex::class);
35+
$valid->method('getId')->willReturn(1);
36+
$valid->method('getAction')->willReturn('delete');
37+
$valid->method('getRegex')->willReturn('user unknown');
38+
$valid->method('getRegexHash')->willReturn(md5('user unknown'));
39+
40+
$noRegex = $this->createMock(BounceRegex::class);
41+
$noRegex->method('getId')->willReturn(2);
42+
43+
$noAction = $this->createMock(BounceRegex::class);
44+
$noAction->method('getId')->willReturn(3);
45+
$noAction->method('getRegex')->willReturn('pattern');
46+
$noAction->method('getRegexHash')->willReturn(md5('pattern'));
47+
48+
$noId = $this->createMock(BounceRegex::class);
49+
$noId->method('getRegex')->willReturn('has no id');
50+
$noId->method('getRegexHash')->willReturn(md5('has no id'));
51+
$noId->method('getAction')->willReturn('keep');
4252

4353
$this->regexRepository->expects($this->once())
4454
->method('fetchActiveOrdered')
@@ -51,33 +61,46 @@ public function testLoadActiveRulesMapsRowsAndSkipsInvalid(): void
5161

5262
public function testLoadAllRulesDelegatesToRepository(): void
5363
{
54-
$r1 = new BounceRegex(regex: 'a', regexHash: md5('a'), action: 'keep');
55-
$r2 = new BounceRegex(regex: 'b', regexHash: md5('b'), action: 'delete');
56-
$this->setId($r1, 10);
57-
$this->setId($r2, 11);
64+
$rule1 = $this->createMock(BounceRegex::class);
65+
$rule1->method('getId')->willReturn(10);
66+
$rule1->method('getAction')->willReturn('keep');
67+
$rule1->method('getRegex')->willReturn('a');
68+
$rule1->method('getRegexHash')->willReturn(md5('a'));
69+
70+
$rule2 = $this->createMock(BounceRegex::class);
71+
$rule2->method('getId')->willReturn(11);
72+
$rule2->method('getAction')->willReturn('delete');
73+
$rule2->method('getRegex')->willReturn('b');
74+
$rule2->method('getRegexHash')->willReturn(md5('b'));
5875

5976
$this->regexRepository->expects($this->once())
6077
->method('fetchAllOrdered')
61-
->willReturn([$r1, $r2]);
78+
->willReturn([$rule1, $rule2]);
6279

6380
$result = $this->manager->loadAllRules();
64-
$this->assertSame(['a' => $r1, 'b' => $r2], $result);
81+
$this->assertSame(['a' => $rule1, 'b' => $rule2], $result);
6582
}
6683

6784
public function testMatchBounceRulesMatchesQuotedAndRawAndHandlesInvalidPatterns(): void
6885
{
69-
$valid = new BounceRegex(regex: 'user unknown', regexHash: md5('user unknown'), action: 'delete');
70-
$this->setId($valid, 1);
71-
// invalid regex pattern that would break preg_match if not handled (unbalanced bracket)
72-
$invalid = new BounceRegex(regex: '([a-z', regexHash: md5('([a-z'), action: 'keep');
73-
$this->setId($invalid, 2);
86+
$valid = $this->createMock(BounceRegex::class);
87+
$valid->method('getId')->willReturn(1);
88+
$valid->method('getAction')->willReturn('delete');
89+
$valid->method('getRegex')->willReturn('user unknown');
90+
$valid->method('getRegexHash')->willReturn(md5('user unknown'));
91+
92+
$invalid = $this->createMock(BounceRegex::class);
93+
$invalid->method('getId')->willReturn(2);
94+
$invalid->method('getAction')->willReturn('keep');
95+
$invalid->method('getRegex')->willReturn('([a-z');
96+
$invalid->method('getRegexHash')->willReturn(md5('([a-z'));
7497

7598
$rules = ['user unknown' => $valid, '([a-z' => $invalid];
7699

77100
$matched = $this->manager->matchBounceRules('Delivery failed: user unknown at example', $rules);
78101
$this->assertSame($valid, $matched);
79102

80-
// Ensure invalid pattern does not throw and simply not match
103+
// Ensure an invalid pattern does not throw and simply not match
81104
$matchedInvalid = $this->manager->matchBounceRules('something else', ['([a-z' => $invalid]);
82105
$this->assertNull($matchedInvalid);
83106
}

tests/Unit/Domain/Messaging/Service/Manager/TemplateImageManagerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ protected function setUp(): void
2424
$this->entityManager = $this->createMock(EntityManagerInterface::class);
2525

2626
$this->manager = new TemplateImageManager(
27-
$this->templateImageRepository,
28-
$this->entityManager
27+
templateImageRepository: $this->templateImageRepository,
28+
entityManager: $this->entityManager
2929
);
3030
}
3131

tests/Unit/Domain/Messaging/Service/Processor/AdvancedBounceRulesProcessorTest.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpList\Core\Domain\Messaging\Model\Bounce;
88
use PhpList\Core\Domain\Messaging\Model\BounceRegex;
9+
use PhpList\Core\Domain\Messaging\Model\UserMessageBounce;
910
use PhpList\Core\Domain\Messaging\Service\BounceActionResolver;
1011
use PhpList\Core\Domain\Messaging\Service\Manager\BounceManager;
1112
use PhpList\Core\Domain\Messaging\Service\Manager\BounceRuleManager;
@@ -51,16 +52,16 @@ public function testNoActiveRules(): void
5152

5253
public function testProcessingWithMatchesAndNonMatches(): void
5354
{
54-
$rule1 = new BounceRegex(action: 'blacklist', count: 0);
55-
$rule2 = new BounceRegex(action: 'notify', count: 0);
56-
// Manually set IDs via reflection since BounceRegex::id is private and generated by ORM in production
57-
$setId = function (object $obj, int $id): void {
58-
$ref = new \ReflectionClass($obj);
59-
$prop = $ref->getProperty('id');
60-
$prop->setValue($obj, $id);
61-
};
62-
$setId($rule1, 10);
63-
$setId($rule2, 20);
55+
$rule1 = $this->createMock(BounceRegex::class);
56+
$rule1->method('getId')->willReturn(10);
57+
$rule1->method('getAction')->willReturn('blacklist');
58+
$rule1->method('getCount')->willReturn(0);
59+
60+
$rule2 = $this->createMock(BounceRegex::class);
61+
$rule2->method('getId')->willReturn(20);
62+
$rule2->method('getAction')->willReturn('notify');
63+
$rule2->method('getCount')->willReturn(0);
64+
6465
$rules = [$rule1, $rule2];
6566
$this->ruleManager->method('loadActiveRules')->willReturn($rules);
6667

@@ -78,33 +79,17 @@ public function testProcessingWithMatchesAndNonMatches(): void
7879
$bounce3->method('getHeader')->willReturn('H3');
7980
$bounce3->method('getData')->willReturn('D3');
8081

81-
$umb1 = new class {
82-
public function getId()
83-
{
84-
return 1;
85-
} public function getUserId()
86-
{
87-
return 111;
88-
}
89-
};
90-
$umb2 = new class {
91-
public function getId()
92-
{
93-
return 2;
94-
} public function getUserId()
95-
{
96-
return 0;
97-
}
98-
};
99-
$umb3 = new class {
100-
public function getId()
101-
{
102-
return 3;
103-
} public function getUserId()
104-
{
105-
return 222;
106-
}
107-
};
82+
$umb1 = $this->createMock(UserMessageBounce::class);
83+
$umb1->method('getId')->willReturn(1);
84+
$umb1->method('getUserId')->willReturn(111);
85+
86+
$umb2 = $this->createMock(UserMessageBounce::class);
87+
$umb2->method('getId')->willReturn(2);
88+
$umb2->method('getUserId')->willReturn(0);
89+
90+
$umb3 = $this->createMock(UserMessageBounce::class);
91+
$umb3->method('getId')->willReturn(3);
92+
$umb3->method('getUserId')->willReturn(222);
10893

10994
$this->bounceManager->method('fetchUserMessageBounceBatch')->willReturnOnConsecutiveCalls(
11095
[ ['umb' => $umb1, 'bounce' => $bounce1], ['umb' => $umb2, 'bounce' => $bounce2] ],

tests/Unit/Domain/Messaging/Service/Processor/BounceDataProcessorTest.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpList\Core\Domain\Messaging\Model\Bounce;
99
use PhpList\Core\Domain\Messaging\Repository\MessageRepository;
1010
use PhpList\Core\Domain\Messaging\Service\Manager\BounceManager;
11+
use PhpList\Core\Domain\Subscription\Model\Subscriber;
1112
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;
1213
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberHistoryManager;
1314
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager;
@@ -40,12 +41,12 @@ protected function setUp(): void
4041
private function makeProcessor(): BounceDataProcessor
4142
{
4243
return new BounceDataProcessor(
43-
$this->bounceManager,
44-
$this->subscriberRepository,
45-
$this->messageRepository,
46-
$this->logger,
47-
$this->subscriberManager,
48-
$this->historyManager,
44+
bounceManager: $this->bounceManager,
45+
subscriberRepository: $this->subscriberRepository,
46+
messageRepository: $this->messageRepository,
47+
logger: $this->logger,
48+
subscriberManager: $this->subscriberManager,
49+
subscriberHistoryManager: $this->historyManager,
4950
);
5051
}
5152

@@ -70,12 +71,8 @@ public function testSystemMessageWithUserAddsHistory(): void
7071
->method('info')
7172
->with('system message bounced, user marked unconfirmed', ['userId' => 123]);
7273

73-
$subscriber = new class {
74-
public function getId()
75-
{
76-
return 123;
77-
}
78-
};
74+
$subscriber = $this->createMock(Subscriber::class);
75+
$subscriber->method('getId')->willReturn(123);
7976
$this->subscriberManager->method('getSubscriberById')->with(123)->willReturn($subscriber);
8077
$this->historyManager
8178
->expects($this->once())

0 commit comments

Comments
 (0)