MailFake.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Illuminate\Contracts\Mail\Mailer;
  4. use Illuminate\Contracts\Mail\Mailable;
  5. use PHPUnit\Framework\Assert as PHPUnit;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. class MailFake implements Mailer
  8. {
  9. /**
  10. * All of the mailables that have been sent.
  11. *
  12. * @var array
  13. */
  14. protected $mailables = [];
  15. /**
  16. * All of the mailables that have been queued.
  17. *
  18. * @var array
  19. */
  20. protected $queuedMailables = [];
  21. /**
  22. * Assert if a mailable was sent based on a truth-test callback.
  23. *
  24. * @param string $mailable
  25. * @param callable|int|null $callback
  26. * @return void
  27. */
  28. public function assertSent($mailable, $callback = null)
  29. {
  30. if (is_numeric($callback)) {
  31. return $this->assertSentTimes($mailable, $callback);
  32. }
  33. PHPUnit::assertTrue(
  34. $this->sent($mailable, $callback)->count() > 0,
  35. "The expected [{$mailable}] mailable was not sent."
  36. );
  37. }
  38. /**
  39. * Assert if a mailable was sent a number of times.
  40. *
  41. * @param string $mailable
  42. * @param int $times
  43. * @return void
  44. */
  45. protected function assertSentTimes($mailable, $times = 1)
  46. {
  47. PHPUnit::assertTrue(
  48. ($count = $this->sent($mailable)->count()) === $times,
  49. "The expected [{$mailable}] mailable was sent {$count} times instead of {$times} times."
  50. );
  51. }
  52. /**
  53. * Determine if a mailable was not sent based on a truth-test callback.
  54. *
  55. * @param string $mailable
  56. * @param callable|null $callback
  57. * @return void
  58. */
  59. public function assertNotSent($mailable, $callback = null)
  60. {
  61. PHPUnit::assertTrue(
  62. $this->sent($mailable, $callback)->count() === 0,
  63. "The unexpected [{$mailable}] mailable was sent."
  64. );
  65. }
  66. /**
  67. * Assert that no mailables were sent.
  68. *
  69. * @return void
  70. */
  71. public function assertNothingSent()
  72. {
  73. PHPUnit::assertEmpty($this->mailables, 'Mailables were sent unexpectedly.');
  74. }
  75. /**
  76. * Assert if a mailable was queued based on a truth-test callback.
  77. *
  78. * @param string $mailable
  79. * @param callable|int|null $callback
  80. * @return void
  81. */
  82. public function assertQueued($mailable, $callback = null)
  83. {
  84. if (is_numeric($callback)) {
  85. return $this->assertQueuedTimes($mailable, $callback);
  86. }
  87. PHPUnit::assertTrue(
  88. $this->queued($mailable, $callback)->count() > 0,
  89. "The expected [{$mailable}] mailable was not queued."
  90. );
  91. }
  92. /**
  93. * Assert if a mailable was queued a number of times.
  94. *
  95. * @param string $mailable
  96. * @param int $times
  97. * @return void
  98. */
  99. protected function assertQueuedTimes($mailable, $times = 1)
  100. {
  101. PHPUnit::assertTrue(
  102. ($count = $this->queued($mailable)->count()) === $times,
  103. "The expected [{$mailable}] mailable was queued {$count} times instead of {$times} times."
  104. );
  105. }
  106. /**
  107. * Determine if a mailable was not queued based on a truth-test callback.
  108. *
  109. * @param string $mailable
  110. * @param callable|null $callback
  111. * @return void
  112. */
  113. public function assertNotQueued($mailable, $callback = null)
  114. {
  115. PHPUnit::assertTrue(
  116. $this->queued($mailable, $callback)->count() === 0,
  117. "The unexpected [{$mailable}] mailable was queued."
  118. );
  119. }
  120. /**
  121. * Assert that no mailables were queued.
  122. *
  123. * @return void
  124. */
  125. public function assertNothingQueued()
  126. {
  127. PHPUnit::assertEmpty($this->queuedMailables, 'Mailables were queued unexpectedly.');
  128. }
  129. /**
  130. * Get all of the mailables matching a truth-test callback.
  131. *
  132. * @param string $mailable
  133. * @param callable|null $callback
  134. * @return \Illuminate\Support\Collection
  135. */
  136. public function sent($mailable, $callback = null)
  137. {
  138. if (! $this->hasSent($mailable)) {
  139. return collect();
  140. }
  141. $callback = $callback ?: function () {
  142. return true;
  143. };
  144. return $this->mailablesOf($mailable)->filter(function ($mailable) use ($callback) {
  145. return $callback($mailable);
  146. });
  147. }
  148. /**
  149. * Determine if the given mailable has been sent.
  150. *
  151. * @param string $mailable
  152. * @return bool
  153. */
  154. public function hasSent($mailable)
  155. {
  156. return $this->mailablesOf($mailable)->count() > 0;
  157. }
  158. /**
  159. * Get all of the queued mailables matching a truth-test callback.
  160. *
  161. * @param string $mailable
  162. * @param callable|null $callback
  163. * @return \Illuminate\Support\Collection
  164. */
  165. public function queued($mailable, $callback = null)
  166. {
  167. if (! $this->hasQueued($mailable)) {
  168. return collect();
  169. }
  170. $callback = $callback ?: function () {
  171. return true;
  172. };
  173. return $this->queuedMailablesOf($mailable)->filter(function ($mailable) use ($callback) {
  174. return $callback($mailable);
  175. });
  176. }
  177. /**
  178. * Determine if the given mailable has been queued.
  179. *
  180. * @param string $mailable
  181. * @return bool
  182. */
  183. public function hasQueued($mailable)
  184. {
  185. return $this->queuedMailablesOf($mailable)->count() > 0;
  186. }
  187. /**
  188. * Get all of the mailed mailables for a given type.
  189. *
  190. * @param string $type
  191. * @return \Illuminate\Support\Collection
  192. */
  193. protected function mailablesOf($type)
  194. {
  195. return collect($this->mailables)->filter(function ($mailable) use ($type) {
  196. return $mailable instanceof $type;
  197. });
  198. }
  199. /**
  200. * Get all of the mailed mailables for a given type.
  201. *
  202. * @param string $type
  203. * @return \Illuminate\Support\Collection
  204. */
  205. protected function queuedMailablesOf($type)
  206. {
  207. return collect($this->queuedMailables)->filter(function ($mailable) use ($type) {
  208. return $mailable instanceof $type;
  209. });
  210. }
  211. /**
  212. * Begin the process of mailing a mailable class instance.
  213. *
  214. * @param mixed $users
  215. * @return \Illuminate\Mail\PendingMail
  216. */
  217. public function to($users)
  218. {
  219. return (new PendingMailFake($this))->to($users);
  220. }
  221. /**
  222. * Begin the process of mailing a mailable class instance.
  223. *
  224. * @param mixed $users
  225. * @return \Illuminate\Mail\PendingMail
  226. */
  227. public function bcc($users)
  228. {
  229. return (new PendingMailFake($this))->bcc($users);
  230. }
  231. /**
  232. * Send a new message when only a raw text part.
  233. *
  234. * @param string $text
  235. * @param \Closure|string $callback
  236. * @return int
  237. */
  238. public function raw($text, $callback)
  239. {
  240. //
  241. }
  242. /**
  243. * Send a new message using a view.
  244. *
  245. * @param string|array $view
  246. * @param array $data
  247. * @param \Closure|string $callback
  248. * @return void
  249. */
  250. public function send($view, array $data = [], $callback = null)
  251. {
  252. if (! $view instanceof Mailable) {
  253. return;
  254. }
  255. if ($view instanceof ShouldQueue) {
  256. return $this->queue($view, $data);
  257. }
  258. $this->mailables[] = $view;
  259. }
  260. /**
  261. * Queue a new e-mail message for sending.
  262. *
  263. * @param string|array $view
  264. * @param string|null $queue
  265. * @return mixed
  266. */
  267. public function queue($view, $queue = null)
  268. {
  269. if (! $view instanceof Mailable) {
  270. return;
  271. }
  272. $this->queuedMailables[] = $view;
  273. }
  274. /**
  275. * Get the array of failed recipients.
  276. *
  277. * @return array
  278. */
  279. public function failures()
  280. {
  281. //
  282. }
  283. }