PageRenderTime 154ms CodeModel.GetById 51ms RepoModel.GetById 19ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/MailFake.php

https://gitlab.com/madwanz64/laravel
PHP | 344 lines | 158 code | 38 blank | 148 comment | 8 complexity | b28b209149a1bc2044a243dee835fb6e MD5 | raw file
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Illuminate\Contracts\Mail\Mailable;
  4. use Illuminate\Contracts\Mail\Mailer;
  5. use Illuminate\Contracts\Mail\MailQueue;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use PHPUnit\Framework\Assert as PHPUnit;
  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. $mailableNames = collect($this->mailables)->map(function ($mailable) {
  79. return get_class($mailable);
  80. })->join(', ');
  81. PHPUnit::assertEmpty($this->mailables, 'The following mailables were sent unexpectedly: '.$mailableNames);
  82. }
  83. /**
  84. * Assert if a mailable was queued based on a truth-test callback.
  85. *
  86. * @param string $mailable
  87. * @param callable|int|null $callback
  88. * @return void
  89. */
  90. public function assertQueued($mailable, $callback = null)
  91. {
  92. if (is_numeric($callback)) {
  93. return $this->assertQueuedTimes($mailable, $callback);
  94. }
  95. PHPUnit::assertTrue(
  96. $this->queued($mailable, $callback)->count() > 0,
  97. "The expected [{$mailable}] mailable was not queued."
  98. );
  99. }
  100. /**
  101. * Assert if a mailable was queued a number of times.
  102. *
  103. * @param string $mailable
  104. * @param int $times
  105. * @return void
  106. */
  107. protected function assertQueuedTimes($mailable, $times = 1)
  108. {
  109. PHPUnit::assertTrue(
  110. ($count = $this->queued($mailable)->count()) === $times,
  111. "The expected [{$mailable}] mailable was queued {$count} times instead of {$times} times."
  112. );
  113. }
  114. /**
  115. * Determine if a mailable was not queued based on a truth-test callback.
  116. *
  117. * @param string $mailable
  118. * @param callable|null $callback
  119. * @return void
  120. */
  121. public function assertNotQueued($mailable, $callback = null)
  122. {
  123. PHPUnit::assertTrue(
  124. $this->queued($mailable, $callback)->count() === 0,
  125. "The unexpected [{$mailable}] mailable was queued."
  126. );
  127. }
  128. /**
  129. * Assert that no mailables were queued.
  130. *
  131. * @return void
  132. */
  133. public function assertNothingQueued()
  134. {
  135. $mailableNames = collect($this->queuedMailables)->map(function ($mailable) {
  136. return get_class($mailable);
  137. })->join(', ');
  138. PHPUnit::assertEmpty($this->queuedMailables, 'The following mailables were queued unexpectedly: '.$mailableNames);
  139. }
  140. /**
  141. * Get all of the mailables matching a truth-test callback.
  142. *
  143. * @param string $mailable
  144. * @param callable|null $callback
  145. * @return \Illuminate\Support\Collection
  146. */
  147. public function sent($mailable, $callback = null)
  148. {
  149. if (! $this->hasSent($mailable)) {
  150. return collect();
  151. }
  152. $callback = $callback ?: function () {
  153. return true;
  154. };
  155. return $this->mailablesOf($mailable)->filter(function ($mailable) use ($callback) {
  156. return $callback($mailable);
  157. });
  158. }
  159. /**
  160. * Determine if the given mailable has been sent.
  161. *
  162. * @param string $mailable
  163. * @return bool
  164. */
  165. public function hasSent($mailable)
  166. {
  167. return $this->mailablesOf($mailable)->count() > 0;
  168. }
  169. /**
  170. * Get all of the queued mailables matching a truth-test callback.
  171. *
  172. * @param string $mailable
  173. * @param callable|null $callback
  174. * @return \Illuminate\Support\Collection
  175. */
  176. public function queued($mailable, $callback = null)
  177. {
  178. if (! $this->hasQueued($mailable)) {
  179. return collect();
  180. }
  181. $callback = $callback ?: function () {
  182. return true;
  183. };
  184. return $this->queuedMailablesOf($mailable)->filter(function ($mailable) use ($callback) {
  185. return $callback($mailable);
  186. });
  187. }
  188. /**
  189. * Determine if the given mailable has been queued.
  190. *
  191. * @param string $mailable
  192. * @return bool
  193. */
  194. public function hasQueued($mailable)
  195. {
  196. return $this->queuedMailablesOf($mailable)->count() > 0;
  197. }
  198. /**
  199. * Get all of the mailed mailables for a given type.
  200. *
  201. * @param string $type
  202. * @return \Illuminate\Support\Collection
  203. */
  204. protected function mailablesOf($type)
  205. {
  206. return collect($this->mailables)->filter(function ($mailable) use ($type) {
  207. return $mailable instanceof $type;
  208. });
  209. }
  210. /**
  211. * Get all of the mailed mailables for a given type.
  212. *
  213. * @param string $type
  214. * @return \Illuminate\Support\Collection
  215. */
  216. protected function queuedMailablesOf($type)
  217. {
  218. return collect($this->queuedMailables)->filter(function ($mailable) use ($type) {
  219. return $mailable instanceof $type;
  220. });
  221. }
  222. /**
  223. * Begin the process of mailing a mailable class instance.
  224. *
  225. * @param mixed $users
  226. * @return \Illuminate\Mail\PendingMail
  227. */
  228. public function to($users)
  229. {
  230. return (new PendingMailFake($this))->to($users);
  231. }
  232. /**
  233. * Begin the process of mailing a mailable class instance.
  234. *
  235. * @param mixed $users
  236. * @return \Illuminate\Mail\PendingMail
  237. */
  238. public function bcc($users)
  239. {
  240. return (new PendingMailFake($this))->bcc($users);
  241. }
  242. /**
  243. * Send a new message with only a raw text part.
  244. *
  245. * @param string $text
  246. * @param \Closure|string $callback
  247. * @return void
  248. */
  249. public function raw($text, $callback)
  250. {
  251. //
  252. }
  253. /**
  254. * Send a new message using a view.
  255. *
  256. * @param string|array $view
  257. * @param array $data
  258. * @param \Closure|string $callback
  259. * @return void
  260. */
  261. public function send($view, array $data = [], $callback = null)
  262. {
  263. if (! $view instanceof Mailable) {
  264. return;
  265. }
  266. if ($view instanceof ShouldQueue) {
  267. return $this->queue($view, $data);
  268. }
  269. $this->mailables[] = $view;
  270. }
  271. /**
  272. * Queue a new e-mail message for sending.
  273. *
  274. * @param \Illuminate\Contracts\Mail\Mailable|string|array $view
  275. * @param string|null $queue
  276. * @return mixed
  277. */
  278. public function queue($view, $queue = null)
  279. {
  280. if (! $view instanceof Mailable) {
  281. return;
  282. }
  283. $this->queuedMailables[] = $view;
  284. }
  285. /**
  286. * Queue a new e-mail message for sending after (n) seconds.
  287. *
  288. * @param \DateTimeInterface|\DateInterval|int $delay
  289. * @param \Illuminate\Contracts\Mail\Mailable|string|array $view
  290. * @param string $queue
  291. * @return mixed
  292. */
  293. public function later($delay, $view, $queue = null)
  294. {
  295. $this->queue($view, $queue);
  296. }
  297. /**
  298. * Get the array of failed recipients.
  299. *
  300. * @return array
  301. */
  302. public function failures()
  303. {
  304. return [];
  305. }
  306. }