@@ -909,6 +909,39 @@ public function attachMany($files)
909
909
return $ this ;
910
910
}
911
911
912
+ /**
913
+ * Determine if the mailable has the given attachment.
914
+ *
915
+ * @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file
916
+ * @param array $options
917
+ * @return bool
918
+ */
919
+ public function hasAttachment ($ file , array $ options = [])
920
+ {
921
+ if ($ file instanceof Attachable) {
922
+ $ file = $ file ->toMailAttachment ();
923
+ }
924
+
925
+ if ($ file instanceof Attachment) {
926
+ $ parts = $ file ->attachWith (
927
+ fn ($ path ) => [$ path , ['as ' => $ file ->as , 'mime ' => $ file ->mime ]],
928
+ fn ($ data ) => $ this ->hasAttachedData ($ data (), $ file ->as , ['mime ' => $ file ->mime ])
929
+ );
930
+
931
+ if ($ parts === true ) {
932
+ return true ;
933
+ }
934
+
935
+ [$ file , $ options ] = $ parts === false
936
+ ? [null , []]
937
+ : $ parts ;
938
+ }
939
+
940
+ return collect ($ this ->attachments )->contains (
941
+ fn ($ attachment ) => $ attachment ['file ' ] === $ file && array_filter ($ attachment ['options ' ]) === array_filter ($ options )
942
+ );
943
+ }
944
+
912
945
/**
913
946
* Attach a file to the message from storage.
914
947
*
@@ -945,6 +978,38 @@ public function attachFromStorageDisk($disk, $path, $name = null, array $options
945
978
return $ this ;
946
979
}
947
980
981
+ /**
982
+ * Determine if the mailable has the given attachment from storage.
983
+ *
984
+ * @param string $path
985
+ * @param string|null $name
986
+ * @param array $options
987
+ * @return bool
988
+ */
989
+ public function hasAttachmentFromStorage ($ path , $ name = null , array $ options = [])
990
+ {
991
+ return $ this ->hasAttachmentFromStorageDisk (null , $ path , $ name , $ options );
992
+ }
993
+
994
+ /**
995
+ * Determine if the mailable has the given attachment from a specific storage disk.
996
+ *
997
+ * @param string $disk
998
+ * @param string $path
999
+ * @param string|null $name
1000
+ * @param array $options
1001
+ * @return bool
1002
+ */
1003
+ public function hasAttachmentFromStorageDisk ($ disk , $ path , $ name = null , array $ options = [])
1004
+ {
1005
+ return collect ($ this ->diskAttachments )->contains (
1006
+ fn ($ attachment ) => $ attachment ['disk ' ] === $ disk
1007
+ && $ attachment ['path ' ] === $ path
1008
+ && $ attachment ['name ' ] === ($ name ?? basename ($ path ))
1009
+ && $ attachment ['options ' ] === $ options
1010
+ );
1011
+ }
1012
+
948
1013
/**
949
1014
* Attach in-memory data as an attachment.
950
1015
*
@@ -964,6 +1029,23 @@ public function attachData($data, $name, array $options = [])
964
1029
return $ this ;
965
1030
}
966
1031
1032
+ /**
1033
+ * Determine if the mailable has the given data as an attachment.
1034
+ *
1035
+ * @param string $data
1036
+ * @param string $name
1037
+ * @param array $options
1038
+ * @return bool
1039
+ */
1040
+ public function hasAttachedData ($ data , $ name , array $ options = [])
1041
+ {
1042
+ return collect ($ this ->rawAttachments )->contains (
1043
+ fn ($ attachment ) => $ attachment ['data ' ] === $ data
1044
+ && $ attachment ['name ' ] === $ name
1045
+ && array_filter ($ attachment ['options ' ]) === array_filter ($ options )
1046
+ );
1047
+ }
1048
+
967
1049
/**
968
1050
* Add a tag header to the message when supported by the underlying transport.
969
1051
*
@@ -1093,6 +1175,86 @@ public function assertSeeInOrderInText($strings)
1093
1175
return $ this ;
1094
1176
}
1095
1177
1178
+ /**
1179
+ * Assert the mailable has the given attachment.
1180
+ *
1181
+ * @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file
1182
+ * @param array $options
1183
+ * @return $this
1184
+ */
1185
+ public function assertHasAttachment ($ file , array $ options = [])
1186
+ {
1187
+ $ this ->renderForAssertions ();
1188
+
1189
+ PHPUnit::assertTrue (
1190
+ $ this ->hasAttachment ($ file , $ options ),
1191
+ 'Did not find the expected attachment. '
1192
+ );
1193
+
1194
+ return $ this ;
1195
+ }
1196
+
1197
+ /**
1198
+ * Assert the mailable has the given data as an attachment.
1199
+ *
1200
+ * @param string $data
1201
+ * @param string $name
1202
+ * @param array $options
1203
+ * @return $this
1204
+ */
1205
+ public function assertHasAttachedData ($ data , $ name , array $ options = [])
1206
+ {
1207
+ $ this ->renderForAssertions ();
1208
+
1209
+ PHPUnit::assertTrue (
1210
+ $ this ->hasAttachedData ($ data , $ name , $ options ),
1211
+ 'Did not find the expected attachment. '
1212
+ );
1213
+
1214
+ return $ this ;
1215
+ }
1216
+
1217
+ /**
1218
+ * Assert the mailable has the given attachment from storage.
1219
+ *
1220
+ * @param string $path
1221
+ * @param string|null $name
1222
+ * @param array $options
1223
+ * @return $this
1224
+ */
1225
+ public function assertHasAttachmentFromStorage ($ path , $ name = null , array $ options = [])
1226
+ {
1227
+ $ this ->renderForAssertions ();
1228
+
1229
+ PHPUnit::assertTrue (
1230
+ $ this ->hasAttachmentFromStorage ($ path , $ name , $ options ),
1231
+ 'Did not find the expected attachment. '
1232
+ );
1233
+
1234
+ return $ this ;
1235
+ }
1236
+
1237
+ /**
1238
+ * Assert the mailable has the given attachment from a specific storage disk.
1239
+ *
1240
+ * @param string $disk
1241
+ * @param string $path
1242
+ * @param string|null $name
1243
+ * @param array $options
1244
+ * @return $this
1245
+ */
1246
+ public function assertHasAttachmentFromStorageDisk ($ disk , $ path , $ name = null , array $ options = [])
1247
+ {
1248
+ $ this ->renderForAssertions ();
1249
+
1250
+ PHPUnit::assertTrue (
1251
+ $ this ->hasAttachmentFromStorageDisk ($ disk , $ path , $ name , $ options ),
1252
+ 'Did not find the expected attachment. '
1253
+ );
1254
+
1255
+ return $ this ;
1256
+ }
1257
+
1096
1258
/**
1097
1259
* Render the HTML and plain-text version of the mailable into views for assertions.
1098
1260
*
0 commit comments