-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathWatchTest.php
85 lines (67 loc) · 2.61 KB
/
WatchTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
use Illuminate\Support\Facades\Event;
use Tests\Datasets\PriceChangedEvent;
use Tests\Datasets\ProductWatch;
use Tests\Datasets\RandomNumberChangedEvent;
it('returns the list of watched properties for the model', function () {
expect(ProductWatch::watchedProperties())->toBe([
'price' => PriceChangedEvent::class,
'random_number' => RandomNumberChangedEvent::class,
]);
});
describe('dispatches event when watched property is updated', function () {
it('setting individual properties', function () {
Event::fake([
PriceChangedEvent::class,
RandomNumberChangedEvent::class,
]);
$product = ProductWatch::create([
'name' => 'Product 1',
'price' => 10.99,
'random_number' => 123,
'expires_at' => '2023-12-31 23:59:59',
]);
$product->price = 20.99;
$product->random_number = 456;
$product->save();
Event::assertDispatched(PriceChangedEvent::class, fn ($event) => $event->product->id === $product->id);
Event::assertDispatched(RandomNumberChangedEvent::class, fn ($event) => $event->product->id === $product->id);
});
it('with fill + save methods', function () {
Event::fake([
PriceChangedEvent::class,
RandomNumberChangedEvent::class,
]);
$product = ProductWatch::create([
'name' => 'Product 1',
'price' => 10.99,
'random_number' => 123,
'expires_at' => '2023-12-31 23:59:59',
]);
$product->fill([
'price' => 20.99,
'random_number' => 456,
])->save();
Event::assertDispatched(PriceChangedEvent::class, fn ($event) => $event->product->id === $product->id);
Event::assertDispatched(RandomNumberChangedEvent::class, fn ($event) => $event->product->id === $product->id);
});
it('with update method', function () {
Event::fake([
PriceChangedEvent::class,
RandomNumberChangedEvent::class,
]);
$product = ProductWatch::create([
'name' => 'Product 1',
'price' => 10.99,
'random_number' => 123,
'expires_at' => '2023-12-31 23:59:59',
]);
$product->update([
'price' => 20.99,
'random_number' => 456,
]);
Event::assertDispatched(PriceChangedEvent::class, fn ($event) => $event->product->id === $product->id);
Event::assertDispatched(RandomNumberChangedEvent::class, fn ($event) => $event->product->id === $product->id);
});
});