14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \Mailer \Exception \TransportException ;
16
16
use Symfony \Component \Mailer \Transport \FailoverTransport ;
17
+ use Symfony \Component \Mailer \Transport \RoundRobinTransport ;
17
18
use Symfony \Component \Mailer \Transport \TransportInterface ;
18
19
use Symfony \Component \Mime \RawMessage ;
19
20
@@ -36,8 +37,11 @@ public function testSendFirstWork()
36
37
$ t2 ->expects ($ this ->never ())->method ('send ' );
37
38
$ t = new FailoverTransport ([$ t1 , $ t2 ]);
38
39
$ t ->send (new RawMessage ('' ));
40
+ $ this ->assertTransports ($ t , 1 , []);
39
41
$ t ->send (new RawMessage ('' ));
42
+ $ this ->assertTransports ($ t , 1 , []);
40
43
$ t ->send (new RawMessage ('' ));
44
+ $ this ->assertTransports ($ t , 1 , []);
41
45
}
42
46
43
47
public function testSendAllDead ()
@@ -50,6 +54,7 @@ public function testSendAllDead()
50
54
$ this ->expectException (TransportException::class);
51
55
$ this ->expectExceptionMessage ('All transports failed. ' );
52
56
$ t ->send (new RawMessage ('' ));
57
+ $ this ->assertTransports ($ t , 0 , [$ t1 , $ t2 ]);
53
58
}
54
59
55
60
public function testSendOneDead ()
@@ -60,51 +65,38 @@ public function testSendOneDead()
60
65
$ t2 ->expects ($ this ->exactly (3 ))->method ('send ' );
61
66
$ t = new FailoverTransport ([$ t1 , $ t2 ]);
62
67
$ t ->send (new RawMessage ('' ));
68
+ $ this ->assertTransports ($ t , 0 , [$ t1 ]);
63
69
$ t ->send (new RawMessage ('' ));
70
+ $ this ->assertTransports ($ t , 0 , [$ t1 ]);
64
71
$ t ->send (new RawMessage ('' ));
65
- }
66
-
67
- public function testSendOneDeadAndRecoveryNotWithinRetryPeriod ()
68
- {
69
- $ t1 = $ this ->createMock (TransportInterface::class);
70
- $ t1 ->expects ($ this ->at (0 ))->method ('send ' )->will ($ this ->throwException (new TransportException ()));
71
- $ t1 ->expects ($ this ->once ())->method ('send ' );
72
- $ t2 = $ this ->createMock (TransportInterface::class);
73
- $ t2 ->expects ($ this ->exactly (5 ))->method ('send ' );
74
- $ t = new FailoverTransport ([$ t1 , $ t2 ], 40 );
75
- $ t ->send (new RawMessage ('' ));
76
- sleep (4 );
77
- $ t ->send (new RawMessage ('' ));
78
- sleep (4 );
79
- $ t ->send (new RawMessage ('' ));
80
- sleep (4 );
81
- $ t ->send (new RawMessage ('' ));
82
- sleep (4 );
83
- $ t ->send (new RawMessage ('' ));
72
+ $ this ->assertTransports ($ t , 0 , [$ t1 ]);
84
73
}
85
74
86
75
public function testSendOneDeadAndRecoveryWithinRetryPeriod ()
87
76
{
88
77
$ t1 = $ this ->createMock (TransportInterface::class);
89
78
$ t1 ->expects ($ this ->at (0 ))->method ('send ' )->will ($ this ->throwException (new TransportException ()));
90
79
$ t1 ->expects ($ this ->at (1 ))->method ('send ' );
91
- $ t1 ->expects ($ this ->exactly (3 ))->method ('send ' );
92
80
$ t2 = $ this ->createMock (TransportInterface::class);
93
81
$ t2 ->expects ($ this ->at (0 ))->method ('send ' );
94
82
$ t2 ->expects ($ this ->at (1 ))->method ('send ' );
95
83
$ t2 ->expects ($ this ->at (2 ))->method ('send ' );
96
84
$ t2 ->expects ($ this ->at (3 ))->method ('send ' )->will ($ this ->throwException (new TransportException ()));
97
- $ t2 ->expects ($ this ->exactly (4 ))->method ('send ' );
98
85
$ t = new FailoverTransport ([$ t1 , $ t2 ], 6 );
99
86
$ t ->send (new RawMessage ('' )); // t1>fail - t2>sent
87
+ $ this ->assertTransports ($ t , 0 , [$ t1 ]);
100
88
sleep (4 );
101
89
$ t ->send (new RawMessage ('' )); // t2>sent
90
+ $ this ->assertTransports ($ t , 0 , [$ t1 ]);
102
91
sleep (4 );
103
92
$ t ->send (new RawMessage ('' )); // t2>sent
93
+ $ this ->assertTransports ($ t , 0 , [$ t1 ]);
104
94
sleep (4 );
105
95
$ t ->send (new RawMessage ('' )); // t2>fail - t1>sent
96
+ $ this ->assertTransports ($ t , 1 , [$ t2 ]);
106
97
sleep (4 );
107
98
$ t ->send (new RawMessage ('' )); // t1>sent
99
+ $ this ->assertTransports ($ t , 1 , [$ t2 ]);
108
100
}
109
101
110
102
public function testSendAllDeadWithinRetryPeriod ()
@@ -143,4 +135,15 @@ public function testSendOneDeadButRecover()
143
135
sleep (1 );
144
136
$ t ->send (new RawMessage ('' ));
145
137
}
138
+
139
+ private function assertTransports (RoundRobinTransport $ transport , int $ cursor , array $ deadTransports )
140
+ {
141
+ $ p = new \ReflectionProperty (RoundRobinTransport::class, 'cursor ' );
142
+ $ p ->setAccessible (true );
143
+ $ this ->assertSame ($ cursor , $ p ->getValue ($ transport ));
144
+
145
+ $ p = new \ReflectionProperty (RoundRobinTransport::class, 'deadTransports ' );
146
+ $ p ->setAccessible (true );
147
+ $ this ->assertSame ($ deadTransports , iterator_to_array ($ p ->getValue ($ transport )));
148
+ }
146
149
}
0 commit comments