1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
<?php
namespace Unit\Connectors;
use InvalidArgumentException; use lucatume\WPBrowser\TestCase\WPTestCase; use SolidWP\Mail\Connectors\ConnectionPool; use SolidWP\Mail\Connectors\ConnectorSMTP;
class ConnectionPoolTest extends WPTestCase {
public function testOffsetSetWithValidConnector(): void { $pool = new ConnectionPool(); $connection = $this->createValidConnection( 'valid-connection' );
$pool[] = $connection;
self::assertCount( 1, $pool ); self::assertSame( $connection, $pool->offsetGet( 0 ) ); }
public function testOffsetSetWithNullKey(): void { $pool = new ConnectionPool(); $connection = $this->createValidConnection( 'auto-key-connection' );
$pool->offsetSet( null, $connection );
self::assertCount( 1, $pool ); self::assertSame( $connection, $pool->offsetGet( 0 ) ); }
public function testOverriding(): void { $pool = new ConnectionPool(); $connection = $this->createValidConnection( 'explicit-key-connection' );
$this->expectException( InvalidArgumentException::class ); $this->expectExceptionMessage( 'The pool must have sequential integer keys.' );
$pool[] = $connection; $pool[0] = $connection; }
public function testOffsetSetWithStringKey(): void { $pool = new ConnectionPool(); $connection = $this->createValidConnection( 'string-key-connection' );
$this->expectException( InvalidArgumentException::class ); $this->expectExceptionMessage( 'The pool must have sequential integer keys.' );
$pool['invalid'] = $connection; }
public function testOffsetSetWithNonConnection(): void { $pool = new ConnectionPool();
$pool[] = 'not-a-connection';
self::assertCount( 0, $pool ); }
public function testOffsetSetWithInvalidConnection(): void { $pool = new ConnectionPool(); $pool[] = $this->createInvalidConnection();
self::assertCount( 0, $pool ); }
public function testOffsetSetPreventsDuplicateIds(): void { $pool = new ConnectionPool(); $connection1 = $this->createValidConnection( 'duplicate-id' ); $connection2 = $this->createValidConnection( 'duplicate-id' );
$pool[] = $connection1; $pool[] = $connection2;
self::assertCount( 1, $pool ); self::assertSame( $connection1, $pool[0] ); self::assertFalse( $pool->offsetExists( 1 ) ); }
public function testOffsetSetWithDifferentIds(): void { $pool = new ConnectionPool(); $connection1 = $this->createValidConnection( 'first-id' ); $connection2 = $this->createValidConnection( 'second-id' );
$pool[] = $connection1; $pool[] = $connection2;
self::assertCount( 2, $pool ); self::assertSame( $connection1, $pool[0] ); self::assertSame( $connection2, $pool[1] ); }
public function testKeyReturnsValidInteger(): void { $pool = new ConnectionPool(); $connection = $this->createValidConnection( 'key-test' );
$pool[] = $connection;
self::assertSame( 0, $pool->key() ); }
public function testHasNextWithSingleItem(): void { $pool = new ConnectionPool(); $connection = $this->createValidConnection( 'single-item' );
$pool[] = $connection; $pool->rewind();
self::assertFalse( $pool->hasNext() ); }
public function testHasNextWithMultipleItems(): void { $pool = new ConnectionPool(); $connection1 = $this->createValidConnection( 'first' ); $connection2 = $this->createValidConnection( 'second' );
$pool[] = $connection1; $pool[] = $connection2; $pool->rewind();
self::assertTrue( $pool->hasNext() );
$pool->next(); self::assertFalse( $pool->hasNext() ); }
public function testHasNextWithEmptyPool(): void { $pool = new ConnectionPool();
self::assertFalse( $pool->hasNext() ); }
public function testIteratorFunctionality(): void { $pool = new ConnectionPool(); $connection1 = $this->createValidConnection( 'iter-first' ); $connection2 = $this->createValidConnection( 'iter-second' ); $connection3 = $this->createValidConnection( 'iter-third' );
$pool[] = $connection1; $pool[] = $connection2; $pool[] = $connection3;
$iterations = 0; $connections = [];
foreach ( $pool as $key => $connection ) { ++$iterations; $connections[ $key ] = $connection; }
self::assertSame( 3, $iterations ); self::assertCount( 3, $connections ); self::assertSame( $connection1, $connections[0] ); self::assertSame( $connection2, $connections[1] ); self::assertSame( $connection3, $connections[2] ); }
public function testCountFunctionality(): void { $pool = new ConnectionPool();
self::assertCount( 0, $pool );
$connection1 = $this->createValidConnection( 'count-first' ); $pool[] = $connection1;
self::assertCount( 1, $pool );
$connection2 = $this->createValidConnection( 'count-second' ); $pool[] = $connection2;
self::assertCount( 2, $pool ); }
public function testArrayAccessFunctionalityWithExplicitKey(): void { $pool = new ConnectionPool(); $connection = $this->createValidConnection( 'array-access-connection' );
$this->expectException( InvalidArgumentException::class ); $this->expectExceptionMessage( 'Overriding connections is not allowed.' );
$pool[0] = $connection; }
public function testArrayAccessFunctionalityWithImplicitKey(): void { $pool = new ConnectionPool(); $connection = $this->createValidConnection( 'array-access-connection' );
$pool[] = $connection;
self::assertTrue( isset( $pool[0] ) ); self::assertSame( $connection, $pool[0] );
unset( $pool[0] ); self::assertFalse( isset( $pool[0] ) ); self::assertCount( 0, $pool ); }
private function createValidConnection( string $id = 'test-id' ): ConnectorSMTP { $data = [ 'id' => $id, 'name' => 'Test Provider', 'description' => 'A test SMTP provider', 'from_email' => '[email protected]', 'from_name' => 'Test Sender', 'smtp_host' => 'smtp.example.com', 'smtp_port' => '587', 'smtp_auth' => 'yes', 'smtp_username' => 'user', 'smtp_password' => 'pass', 'smtp_secure' => 'tls', 'is_active' => true, ];
return new ConnectorSMTP( $data ); }
private function createInvalidConnection(): ConnectorSMTP { // Create a connection with missing required fields to make validation fail $data = [ 'id' => 'invalid-id', ];
return new ConnectorSMTP( $data ); } }
|