@@ -51,7 +51,7 @@ public function testImportSubscribersWithNonCsvFileReturnsBadRequestStatus(): vo
51
51
{
52
52
$ filePath = $ this ->tempDir . '/test.txt ' ;
53
53
file_put_contents ($ filePath , 'This is not a CSV file ' );
54
-
54
+
55
55
$ file = new UploadedFile (
56
56
$ filePath ,
57
57
'test.txt ' ,
@@ -77,7 +77,7 @@ public function testImportSubscribersWithValidCsvFile(): void
77
77
$ filePath = $ this ->tempDir . '/subscribers.csv ' ;
78
78
$ csvContent =
"email,name \n[email protected] ,Test User \n[email protected] ,Test User 2 " ;
79
79
file_put_contents ($ filePath , $ csvContent );
80
-
80
+
81
81
$ file = new UploadedFile (
82
82
$ filePath ,
83
83
'subscribers.csv ' ,
@@ -95,7 +95,7 @@ public function testImportSubscribersWithValidCsvFile(): void
95
95
96
96
$ response = self ::getClient ()->getResponse ();
97
97
self ::assertSame (Response::HTTP_OK , $ response ->getStatusCode ());
98
-
98
+
99
99
$ responseContent = $ this ->getDecodedJsonResponseContent ();
100
100
self ::assertArrayHasKey ('imported ' , $ responseContent );
101
101
self ::assertArrayHasKey ('skipped ' , $ responseContent );
@@ -107,7 +107,7 @@ public function testImportSubscribersWithOptions(): void
107
107
$ filePath = $ this ->tempDir . '/subscribers.csv ' ;
108
108
$ csvContent =
"email,name \n[email protected] ,Test User " ;
109
109
file_put_contents ($ filePath , $ csvContent );
110
-
110
+
111
111
$ file = new UploadedFile (
112
112
$ filePath ,
113
113
'subscribers.csv ' ,
@@ -128,7 +128,7 @@ public function testImportSubscribersWithOptions(): void
128
128
129
129
$ response = self ::getClient ()->getResponse ();
130
130
self ::assertSame (Response::HTTP_OK , $ response ->getStatusCode ());
131
-
131
+
132
132
$ responseContent = $ this ->getDecodedJsonResponseContent ();
133
133
self ::assertArrayHasKey ('imported ' , $ responseContent );
134
134
self ::assertArrayHasKey ('skipped ' , $ responseContent );
@@ -141,4 +141,124 @@ public function testGetMethodIsNotAllowed(): void
141
141
142
142
$ this ->assertHttpMethodNotAllowed ();
143
143
}
144
+
145
+ public function testImportSubscribersWithListId (): void
146
+ {
147
+ $ filePath = $ this ->tempDir . '/subscribers.csv ' ;
148
+ $ csvContent =
"email,name \n[email protected] ,Test User " ;
149
+ file_put_contents ($ filePath , $ csvContent );
150
+
151
+ $ file = new UploadedFile (
152
+ $ filePath ,
153
+ 'subscribers.csv ' ,
154
+ 'text/csv ' ,
155
+ null ,
156
+ true
157
+ );
158
+
159
+ $ this ->authenticatedJsonRequest (
160
+ 'POST ' ,
161
+ '/api/v2/subscribers/import ' ,
162
+ [
163
+ 'list_id ' => '1 '
164
+ ],
165
+ ['file ' => $ file ]
166
+ );
167
+
168
+ $ response = self ::getClient ()->getResponse ();
169
+ self ::assertSame (Response::HTTP_OK , $ response ->getStatusCode ());
170
+
171
+ $ responseContent = $ this ->getDecodedJsonResponseContent ();
172
+ self ::assertArrayHasKey ('imported ' , $ responseContent );
173
+ self ::assertArrayHasKey ('skipped ' , $ responseContent );
174
+ self ::assertArrayHasKey ('errors ' , $ responseContent );
175
+ }
176
+
177
+ public function testImportSubscribersWithUpdateExisting (): void
178
+ {
179
+ $ filePath = $ this ->tempDir . '/subscribers.csv ' ;
180
+ $ csvContent =
"email,name \n[email protected] ,Test User " ;
181
+ file_put_contents ($ filePath , $ csvContent );
182
+
183
+ $ file = new UploadedFile (
184
+ $ filePath ,
185
+ 'subscribers.csv ' ,
186
+ 'text/csv ' ,
187
+ null ,
188
+ true
189
+ );
190
+
191
+ $ this ->authenticatedJsonRequest (
192
+ 'POST ' ,
193
+ '/api/v2/subscribers/import ' ,
194
+ [
195
+ 'update_existing ' => 'true '
196
+ ],
197
+ ['file ' => $ file ]
198
+ );
199
+
200
+ $ response = self ::getClient ()->getResponse ();
201
+ self ::assertSame (Response::HTTP_OK , $ response ->getStatusCode ());
202
+
203
+ $ responseContent = $ this ->getDecodedJsonResponseContent ();
204
+ self ::assertArrayHasKey ('imported ' , $ responseContent );
205
+ self ::assertArrayHasKey ('skipped ' , $ responseContent );
206
+ self ::assertArrayHasKey ('errors ' , $ responseContent );
207
+ }
208
+
209
+ public function testImportSubscribersWithSkipInvalidEmails (): void
210
+ {
211
+ $ filePath = $ this ->tempDir . '/subscribers.csv ' ;
212
+ $ csvContent = "email,name \ninvalid-email,Test User " ;
213
+ file_put_contents ($ filePath , $ csvContent );
214
+
215
+ $ file = new UploadedFile (
216
+ $ filePath ,
217
+ 'subscribers.csv ' ,
218
+ 'text/csv ' ,
219
+ null ,
220
+ true
221
+ );
222
+
223
+ $ this ->authenticatedJsonRequest (
224
+ 'POST ' ,
225
+ '/api/v2/subscribers/import ' ,
226
+ [
227
+ 'skip_invalid_emails ' => 'true '
228
+ ],
229
+ ['file ' => $ file ]
230
+ );
231
+
232
+ $ response = self ::getClient ()->getResponse ();
233
+ self ::assertSame (Response::HTTP_OK , $ response ->getStatusCode ());
234
+
235
+ $ responseContent = $ this ->getDecodedJsonResponseContent ();
236
+ self ::assertArrayHasKey ('imported ' , $ responseContent );
237
+ self ::assertArrayHasKey ('skipped ' , $ responseContent );
238
+ self ::assertArrayHasKey ('errors ' , $ responseContent );
239
+ self ::assertEquals (0 , $ responseContent ['imported ' ]);
240
+ self ::assertEquals (1 , $ responseContent ['skipped ' ]);
241
+ self ::assertEquals ([], $ responseContent ['errors ' ]);
242
+
243
+ $ this ->authenticatedJsonRequest (
244
+ 'POST ' ,
245
+ '/api/v2/subscribers/import ' ,
246
+ [
247
+ 'skip_invalid_emails ' => 'false ' ,
248
+ 'update_existing ' => 'true '
249
+ ],
250
+ ['file ' => $ file ],
251
+ );
252
+
253
+ $ response = self ::getClient ()->getResponse ();
254
+ self ::assertSame (Response::HTTP_OK , $ response ->getStatusCode ());
255
+
256
+ $ responseContent = $ this ->getDecodedJsonResponseContent ();
257
+ self ::assertArrayHasKey ('imported ' , $ responseContent );
258
+ self ::assertArrayHasKey ('skipped ' , $ responseContent );
259
+ self ::assertArrayHasKey ('errors ' , $ responseContent );
260
+ self ::assertEquals (1 , $ responseContent ['imported ' ]);
261
+ self ::assertEquals (0 , $ responseContent ['skipped ' ]);
262
+ self ::assertEquals ([], $ responseContent ['errors ' ]);
263
+ }
144
264
}
0 commit comments