@@ -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 }
0 commit comments