Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Support\Testing\Fakes;
4
 
5
use Closure;
6
use Illuminate\Contracts\Mail\Factory;
7
use Illuminate\Contracts\Mail\Mailable;
8
use Illuminate\Contracts\Mail\Mailer;
9
use Illuminate\Contracts\Mail\MailQueue;
10
use Illuminate\Contracts\Queue\ShouldQueue;
11
use Illuminate\Support\Traits\ReflectsClosures;
12
use PHPUnit\Framework\Assert as PHPUnit;
13
 
14
class MailFake implements Factory, Mailer, MailQueue
15
{
16
    use ReflectsClosures;
17
 
18
    /**
19
     * The mailer currently being used to send a message.
20
     *
21
     * @var string
22
     */
23
    protected $currentMailer;
24
 
25
    /**
26
     * All of the mailables that have been sent.
27
     *
28
     * @var array
29
     */
30
    protected $mailables = [];
31
 
32
    /**
33
     * All of the mailables that have been queued.
34
     *
35
     * @var array
36
     */
37
    protected $queuedMailables = [];
38
 
39
    /**
40
     * Assert if a mailable was sent based on a truth-test callback.
41
     *
42
     * @param  string|\Closure  $mailable
43
     * @param  callable|int|null  $callback
44
     * @return void
45
     */
46
    public function assertSent($mailable, $callback = null)
47
    {
48
        [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
49
 
50
        if (is_numeric($callback)) {
51
            return $this->assertSentTimes($mailable, $callback);
52
        }
53
 
54
        $message = "The expected [{$mailable}] mailable was not sent.";
55
 
56
        if (count($this->queuedMailables) > 0) {
57
            $message .= ' Did you mean to use assertQueued() instead?';
58
        }
59
 
60
        PHPUnit::assertTrue(
61
            $this->sent($mailable, $callback)->count() > 0,
62
            $message
63
        );
64
    }
65
 
66
    /**
67
     * Assert if a mailable was sent a number of times.
68
     *
69
     * @param  string  $mailable
70
     * @param  int  $times
71
     * @return void
72
     */
73
    protected function assertSentTimes($mailable, $times = 1)
74
    {
75
        $count = $this->sent($mailable)->count();
76
 
77
        PHPUnit::assertSame(
78
            $times, $count,
79
            "The expected [{$mailable}] mailable was sent {$count} times instead of {$times} times."
80
        );
81
    }
82
 
83
    /**
84
     * Determine if a mailable was not sent or queued to be sent based on a truth-test callback.
85
     *
86
     * @param  string|\Closure  $mailable
87
     * @param  callable|null  $callback
88
     * @return void
89
     */
90
    public function assertNotOutgoing($mailable, $callback = null)
91
    {
92
        $this->assertNotSent($mailable, $callback);
93
        $this->assertNotQueued($mailable, $callback);
94
    }
95
 
96
    /**
97
     * Determine if a mailable was not sent based on a truth-test callback.
98
     *
99
     * @param  string|\Closure  $mailable
100
     * @param  callable|null  $callback
101
     * @return void
102
     */
103
    public function assertNotSent($mailable, $callback = null)
104
    {
105
        [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
106
 
107
        PHPUnit::assertCount(
108
            0, $this->sent($mailable, $callback),
109
            "The unexpected [{$mailable}] mailable was sent."
110
        );
111
    }
112
 
113
    /**
114
     * Assert that no mailables were sent or queued to be sent.
115
     *
116
     * @return void
117
     */
118
    public function assertNothingOutgoing()
119
    {
120
        $this->assertNothingSent();
121
        $this->assertNothingQueued();
122
    }
123
 
124
    /**
125
     * Assert that no mailables were sent.
126
     *
127
     * @return void
128
     */
129
    public function assertNothingSent()
130
    {
131
        $mailableNames = collect($this->mailables)->map(
132
            fn ($mailable) => get_class($mailable)
133
        )->join(', ');
134
 
135
        PHPUnit::assertEmpty($this->mailables, 'The following mailables were sent unexpectedly: '.$mailableNames);
136
    }
137
 
138
    /**
139
     * Assert if a mailable was queued based on a truth-test callback.
140
     *
141
     * @param  string|\Closure  $mailable
142
     * @param  callable|int|null  $callback
143
     * @return void
144
     */
145
    public function assertQueued($mailable, $callback = null)
146
    {
147
        [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
148
 
149
        if (is_numeric($callback)) {
150
            return $this->assertQueuedTimes($mailable, $callback);
151
        }
152
 
153
        PHPUnit::assertTrue(
154
            $this->queued($mailable, $callback)->count() > 0,
155
            "The expected [{$mailable}] mailable was not queued."
156
        );
157
    }
158
 
159
    /**
160
     * Assert if a mailable was queued a number of times.
161
     *
162
     * @param  string  $mailable
163
     * @param  int  $times
164
     * @return void
165
     */
166
    protected function assertQueuedTimes($mailable, $times = 1)
167
    {
168
        $count = $this->queued($mailable)->count();
169
 
170
        PHPUnit::assertSame(
171
            $times, $count,
172
            "The expected [{$mailable}] mailable was queued {$count} times instead of {$times} times."
173
        );
174
    }
175
 
176
    /**
177
     * Determine if a mailable was not queued based on a truth-test callback.
178
     *
179
     * @param  string|\Closure  $mailable
180
     * @param  callable|null  $callback
181
     * @return void
182
     */
183
    public function assertNotQueued($mailable, $callback = null)
184
    {
185
        [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
186
 
187
        PHPUnit::assertCount(
188
            0, $this->queued($mailable, $callback),
189
            "The unexpected [{$mailable}] mailable was queued."
190
        );
191
    }
192
 
193
    /**
194
     * Assert that no mailables were queued.
195
     *
196
     * @return void
197
     */
198
    public function assertNothingQueued()
199
    {
200
        $mailableNames = collect($this->queuedMailables)->map(
201
            fn ($mailable) => get_class($mailable)
202
        )->join(', ');
203
 
204
        PHPUnit::assertEmpty($this->queuedMailables, 'The following mailables were queued unexpectedly: '.$mailableNames);
205
    }
206
 
207
    /**
208
     * Get all of the mailables matching a truth-test callback.
209
     *
210
     * @param  string|\Closure  $mailable
211
     * @param  callable|null  $callback
212
     * @return \Illuminate\Support\Collection
213
     */
214
    public function sent($mailable, $callback = null)
215
    {
216
        [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
217
 
218
        if (! $this->hasSent($mailable)) {
219
            return collect();
220
        }
221
 
222
        $callback = $callback ?: fn () => true;
223
 
224
        return $this->mailablesOf($mailable)->filter(fn ($mailable) => $callback($mailable));
225
    }
226
 
227
    /**
228
     * Determine if the given mailable has been sent.
229
     *
230
     * @param  string  $mailable
231
     * @return bool
232
     */
233
    public function hasSent($mailable)
234
    {
235
        return $this->mailablesOf($mailable)->count() > 0;
236
    }
237
 
238
    /**
239
     * Get all of the queued mailables matching a truth-test callback.
240
     *
241
     * @param  string|\Closure  $mailable
242
     * @param  callable|null  $callback
243
     * @return \Illuminate\Support\Collection
244
     */
245
    public function queued($mailable, $callback = null)
246
    {
247
        [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
248
 
249
        if (! $this->hasQueued($mailable)) {
250
            return collect();
251
        }
252
 
253
        $callback = $callback ?: fn () => true;
254
 
255
        return $this->queuedMailablesOf($mailable)->filter(fn ($mailable) => $callback($mailable));
256
    }
257
 
258
    /**
259
     * Determine if the given mailable has been queued.
260
     *
261
     * @param  string  $mailable
262
     * @return bool
263
     */
264
    public function hasQueued($mailable)
265
    {
266
        return $this->queuedMailablesOf($mailable)->count() > 0;
267
    }
268
 
269
    /**
270
     * Get all of the mailed mailables for a given type.
271
     *
272
     * @param  string  $type
273
     * @return \Illuminate\Support\Collection
274
     */
275
    protected function mailablesOf($type)
276
    {
277
        return collect($this->mailables)->filter(fn ($mailable) => $mailable instanceof $type);
278
    }
279
 
280
    /**
281
     * Get all of the mailed mailables for a given type.
282
     *
283
     * @param  string  $type
284
     * @return \Illuminate\Support\Collection
285
     */
286
    protected function queuedMailablesOf($type)
287
    {
288
        return collect($this->queuedMailables)->filter(fn ($mailable) => $mailable instanceof $type);
289
    }
290
 
291
    /**
292
     * Get a mailer instance by name.
293
     *
294
     * @param  string|null  $name
295
     * @return \Illuminate\Contracts\Mail\Mailer
296
     */
297
    public function mailer($name = null)
298
    {
299
        $this->currentMailer = $name;
300
 
301
        return $this;
302
    }
303
 
304
    /**
305
     * Begin the process of mailing a mailable class instance.
306
     *
307
     * @param  mixed  $users
308
     * @return \Illuminate\Mail\PendingMail
309
     */
310
    public function to($users)
311
    {
312
        return (new PendingMailFake($this))->to($users);
313
    }
314
 
315
    /**
316
     * Begin the process of mailing a mailable class instance.
317
     *
318
     * @param  mixed  $users
319
     * @return \Illuminate\Mail\PendingMail
320
     */
321
    public function cc($users)
322
    {
323
        return (new PendingMailFake($this))->cc($users);
324
    }
325
 
326
    /**
327
     * Begin the process of mailing a mailable class instance.
328
     *
329
     * @param  mixed  $users
330
     * @return \Illuminate\Mail\PendingMail
331
     */
332
    public function bcc($users)
333
    {
334
        return (new PendingMailFake($this))->bcc($users);
335
    }
336
 
337
    /**
338
     * Send a new message with only a raw text part.
339
     *
340
     * @param  string  $text
341
     * @param  \Closure|string  $callback
342
     * @return void
343
     */
344
    public function raw($text, $callback)
345
    {
346
        //
347
    }
348
 
349
    /**
350
     * Send a new message using a view.
351
     *
352
     * @param  \Illuminate\Contracts\Mail\Mailable|string|array  $view
353
     * @param  array  $data
354
     * @param  \Closure|string|null  $callback
355
     * @return void
356
     */
357
    public function send($view, array $data = [], $callback = null)
358
    {
359
        if (! $view instanceof Mailable) {
360
            return;
361
        }
362
 
363
        $view->mailer($this->currentMailer);
364
 
365
        if ($view instanceof ShouldQueue) {
366
            return $this->queue($view, $data);
367
        }
368
 
369
        $this->currentMailer = null;
370
 
371
        $this->mailables[] = $view;
372
    }
373
 
374
    /**
375
     * Queue a new e-mail message for sending.
376
     *
377
     * @param  \Illuminate\Contracts\Mail\Mailable|string|array  $view
378
     * @param  string|null  $queue
379
     * @return mixed
380
     */
381
    public function queue($view, $queue = null)
382
    {
383
        if (! $view instanceof Mailable) {
384
            return;
385
        }
386
 
387
        $view->mailer($this->currentMailer);
388
 
389
        $this->currentMailer = null;
390
 
391
        $this->queuedMailables[] = $view;
392
    }
393
 
394
    /**
395
     * Queue a new e-mail message for sending after (n) seconds.
396
     *
397
     * @param  \DateTimeInterface|\DateInterval|int  $delay
398
     * @param  \Illuminate\Contracts\Mail\Mailable|string|array  $view
399
     * @param  string|null  $queue
400
     * @return mixed
401
     */
402
    public function later($delay, $view, $queue = null)
403
    {
404
        $this->queue($view, $queue);
405
    }
406
 
407
    /**
408
     * Get the array of failed recipients.
409
     *
410
     * @return array
411
     */
412
    public function failures()
413
    {
414
        return [];
415
    }
416
 
417
    /**
418
     * Infer mailable class using reflection if a typehinted closure is passed to assertion.
419
     *
420
     * @param  string|\Closure  $mailable
421
     * @param  callable|null  $callback
422
     * @return array
423
     */
424
    protected function prepareMailableAndCallback($mailable, $callback)
425
    {
426
        if ($mailable instanceof Closure) {
427
            return [$this->firstClosureParameterType($mailable), $mailable];
428
        }
429
 
430
        return [$mailable, $callback];
431
    }
432
 
433
    /**
434
     * Forget all of the resolved mailer instances.
435
     *
436
     * @return $this
437
     */
438
    public function forgetMailers()
439
    {
440
        $this->currentMailer = null;
441
 
442
        return $this;
443
    }
444
}