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 Exception;
7
use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;
8
use Illuminate\Contracts\Notifications\Factory as NotificationFactory;
9
use Illuminate\Contracts\Translation\HasLocalePreference;
10
use Illuminate\Notifications\AnonymousNotifiable;
11
use Illuminate\Support\Collection;
12
use Illuminate\Support\Str;
13
use Illuminate\Support\Traits\Macroable;
14
use Illuminate\Support\Traits\ReflectsClosures;
15
use PHPUnit\Framework\Assert as PHPUnit;
16
 
17
class NotificationFake implements NotificationDispatcher, NotificationFactory
18
{
19
    use Macroable, ReflectsClosures;
20
 
21
    /**
22
     * All of the notifications that have been sent.
23
     *
24
     * @var array
25
     */
26
    protected $notifications = [];
27
 
28
    /**
29
     * Locale used when sending notifications.
30
     *
31
     * @var string|null
32
     */
33
    public $locale;
34
 
35
    /**
36
     * Assert if a notification was sent on-demand based on a truth-test callback.
37
     *
38
     * @param  string|\Closure  $notification
39
     * @param  callable|null  $callback
40
     * @return void
41
     *
42
     * @throws \Exception
43
     */
44
    public function assertSentOnDemand($notification, $callback = null)
45
    {
46
        $this->assertSentTo(new AnonymousNotifiable, $notification, $callback);
47
    }
48
 
49
    /**
50
     * Assert if a notification was sent based on a truth-test callback.
51
     *
52
     * @param  mixed  $notifiable
53
     * @param  string|\Closure  $notification
54
     * @param  callable|null  $callback
55
     * @return void
56
     *
57
     * @throws \Exception
58
     */
59
    public function assertSentTo($notifiable, $notification, $callback = null)
60
    {
61
        if (is_array($notifiable) || $notifiable instanceof Collection) {
62
            if (count($notifiable) === 0) {
63
                throw new Exception('No notifiable given.');
64
            }
65
 
66
            foreach ($notifiable as $singleNotifiable) {
67
                $this->assertSentTo($singleNotifiable, $notification, $callback);
68
            }
69
 
70
            return;
71
        }
72
 
73
        if ($notification instanceof Closure) {
74
            [$notification, $callback] = [$this->firstClosureParameterType($notification), $notification];
75
        }
76
 
77
        if (is_numeric($callback)) {
78
            return $this->assertSentToTimes($notifiable, $notification, $callback);
79
        }
80
 
81
        PHPUnit::assertTrue(
82
            $this->sent($notifiable, $notification, $callback)->count() > 0,
83
            "The expected [{$notification}] notification was not sent."
84
        );
85
    }
86
 
87
    /**
88
     * Assert if a notification was sent on-demand a number of times.
89
     *
90
     * @param  string  $notification
91
     * @param  int  $times
92
     * @return void
93
     */
94
    public function assertSentOnDemandTimes($notification, $times = 1)
95
    {
96
        return $this->assertSentToTimes(new AnonymousNotifiable, $notification, $times);
97
    }
98
 
99
    /**
100
     * Assert if a notification was sent a number of times.
101
     *
102
     * @param  mixed  $notifiable
103
     * @param  string  $notification
104
     * @param  int  $times
105
     * @return void
106
     */
107
    public function assertSentToTimes($notifiable, $notification, $times = 1)
108
    {
109
        $count = $this->sent($notifiable, $notification)->count();
110
 
111
        PHPUnit::assertSame(
112
            $times, $count,
113
            "Expected [{$notification}] to be sent {$times} times, but was sent {$count} times."
114
        );
115
    }
116
 
117
    /**
118
     * Determine if a notification was sent based on a truth-test callback.
119
     *
120
     * @param  mixed  $notifiable
121
     * @param  string|\Closure  $notification
122
     * @param  callable|null  $callback
123
     * @return void
124
     *
125
     * @throws \Exception
126
     */
127
    public function assertNotSentTo($notifiable, $notification, $callback = null)
128
    {
129
        if (is_array($notifiable) || $notifiable instanceof Collection) {
130
            if (count($notifiable) === 0) {
131
                throw new Exception('No notifiable given.');
132
            }
133
 
134
            foreach ($notifiable as $singleNotifiable) {
135
                $this->assertNotSentTo($singleNotifiable, $notification, $callback);
136
            }
137
 
138
            return;
139
        }
140
 
141
        if ($notification instanceof Closure) {
142
            [$notification, $callback] = [$this->firstClosureParameterType($notification), $notification];
143
        }
144
 
145
        PHPUnit::assertCount(
146
            0, $this->sent($notifiable, $notification, $callback),
147
            "The unexpected [{$notification}] notification was sent."
148
        );
149
    }
150
 
151
    /**
152
     * Assert that no notifications were sent.
153
     *
154
     * @return void
155
     */
156
    public function assertNothingSent()
157
    {
158
        PHPUnit::assertEmpty($this->notifications, 'Notifications were sent unexpectedly.');
159
    }
160
 
161
    /**
162
     * Assert that no notifications were sent to the given notifiable.
163
     *
164
     * @param  mixed  $notifiable
165
     * @return void
166
     *
167
     * @throws \Exception
168
     */
169
    public function assertNothingSentTo($notifiable)
170
    {
171
        if (is_array($notifiable) || $notifiable instanceof Collection) {
172
            if (count($notifiable) === 0) {
173
                throw new Exception('No notifiable given.');
174
            }
175
 
176
            foreach ($notifiable as $singleNotifiable) {
177
                $this->assertNothingSentTo($singleNotifiable);
178
            }
179
 
180
            return;
181
        }
182
 
183
        PHPUnit::assertEmpty(
184
            $this->notifications[get_class($notifiable)][$notifiable->getKey()] ?? [],
185
            'Notifications were sent unexpectedly.',
186
        );
187
    }
188
 
189
    /**
190
     * Assert the total amount of times a notification was sent.
191
     *
192
     * @param  string  $notification
193
     * @param  int  $expectedCount
194
     * @return void
195
     */
196
    public function assertSentTimes($notification, $expectedCount)
197
    {
198
        $actualCount = collect($this->notifications)
199
            ->flatten(1)
200
            ->reduce(fn ($count, $sent) => $count + count($sent[$notification] ?? []), 0);
201
 
202
        PHPUnit::assertSame(
203
            $expectedCount, $actualCount,
204
            "Expected [{$notification}] to be sent {$expectedCount} times, but was sent {$actualCount} times."
205
        );
206
    }
207
 
208
    /**
209
     * Assert the total count of notification that were sent.
210
     *
211
     * @param  int  $expectedCount
212
     * @return void
213
     */
214
    public function assertCount($expectedCount)
215
    {
216
        $actualCount = collect($this->notifications)->flatten(3)->count();
217
 
218
        PHPUnit::assertSame(
219
            $expectedCount, $actualCount,
220
            "Expected {$expectedCount} notifications to be sent, but {$actualCount} were sent."
221
        );
222
    }
223
 
224
    /**
225
     * Assert the total amount of times a notification was sent.
226
     *
227
     * @param  int  $expectedCount
228
     * @param  string  $notification
229
     * @return void
230
     *
231
     * @deprecated Use the assertSentTimes method instead
232
     */
233
    public function assertTimesSent($expectedCount, $notification)
234
    {
235
        $this->assertSentTimes($notification, $expectedCount);
236
    }
237
 
238
    /**
239
     * Get all of the notifications matching a truth-test callback.
240
     *
241
     * @param  mixed  $notifiable
242
     * @param  string  $notification
243
     * @param  callable|null  $callback
244
     * @return \Illuminate\Support\Collection
245
     */
246
    public function sent($notifiable, $notification, $callback = null)
247
    {
248
        if (! $this->hasSent($notifiable, $notification)) {
249
            return collect();
250
        }
251
 
252
        $callback = $callback ?: fn () => true;
253
 
254
        $notifications = collect($this->notificationsFor($notifiable, $notification));
255
 
256
        return $notifications->filter(
257
            fn ($arguments) => $callback(...array_values($arguments))
258
        )->pluck('notification');
259
    }
260
 
261
    /**
262
     * Determine if there are more notifications left to inspect.
263
     *
264
     * @param  mixed  $notifiable
265
     * @param  string  $notification
266
     * @return bool
267
     */
268
    public function hasSent($notifiable, $notification)
269
    {
270
        return ! empty($this->notificationsFor($notifiable, $notification));
271
    }
272
 
273
    /**
274
     * Get all of the notifications for a notifiable entity by type.
275
     *
276
     * @param  mixed  $notifiable
277
     * @param  string  $notification
278
     * @return array
279
     */
280
    protected function notificationsFor($notifiable, $notification)
281
    {
282
        return $this->notifications[get_class($notifiable)][$notifiable->getKey()][$notification] ?? [];
283
    }
284
 
285
    /**
286
     * Send the given notification to the given notifiable entities.
287
     *
288
     * @param  \Illuminate\Support\Collection|array|mixed  $notifiables
289
     * @param  mixed  $notification
290
     * @return void
291
     */
292
    public function send($notifiables, $notification)
293
    {
294
        $this->sendNow($notifiables, $notification);
295
    }
296
 
297
    /**
298
     * Send the given notification immediately.
299
     *
300
     * @param  \Illuminate\Support\Collection|array|mixed  $notifiables
301
     * @param  mixed  $notification
302
     * @param  array|null  $channels
303
     * @return void
304
     */
305
    public function sendNow($notifiables, $notification, array $channels = null)
306
    {
307
        if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
308
            $notifiables = [$notifiables];
309
        }
310
 
311
        foreach ($notifiables as $notifiable) {
312
            if (! $notification->id) {
313
                $notification->id = Str::uuid()->toString();
314
            }
315
 
316
            $notifiableChannels = $channels ?: $notification->via($notifiable);
317
 
318
            if (method_exists($notification, 'shouldSend')) {
319
                $notifiableChannels = array_filter(
320
                    $notifiableChannels,
321
                    fn ($channel) => $notification->shouldSend($notifiable, $channel) !== false
322
                );
323
 
324
                if (empty($notifiableChannels)) {
325
                    continue;
326
                }
327
            }
328
 
329
            $this->notifications[get_class($notifiable)][$notifiable->getKey()][get_class($notification)][] = [
330
                'notification' => $notification,
331
                'channels' => $notifiableChannels,
332
                'notifiable' => $notifiable,
333
                'locale' => $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
334
                    if ($notifiable instanceof HasLocalePreference) {
335
                        return $notifiable->preferredLocale();
336
                    }
337
                }),
338
            ];
339
        }
340
    }
341
 
342
    /**
343
     * Get a channel instance by name.
344
     *
345
     * @param  string|null  $name
346
     * @return mixed
347
     */
348
    public function channel($name = null)
349
    {
350
        //
351
    }
352
 
353
    /**
354
     * Set the locale of notifications.
355
     *
356
     * @param  string  $locale
357
     * @return $this
358
     */
359
    public function locale($locale)
360
    {
361
        $this->locale = $locale;
362
 
363
        return $this;
364
    }
365
 
366
    /**
367
     * Get the notifications that have been sent.
368
     *
369
     * @return array
370
     */
371
    public function sentNotifications()
372
    {
373
        return $this->notifications;
374
    }
375
}