Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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