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