-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathImmutableTest.php
83 lines (71 loc) · 2.6 KB
/
ImmutableTest.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
<?php
declare(strict_types=1);
use Tests\Datasets\ProductImmutable;
use WendellAdriel\Lift\Exceptions\ImmutablePropertyException;
it('returns the list of immutable properties for the model', function () {
expect(ProductImmutable::immutableProperties())->toBe([
'name',
'random_number',
]);
});
describe('throws exception when trying to update immutable property', function () {
it('setting individual properties', function () {
$product = ProductImmutable::create([
'name' => 'Product 1',
'price' => 10.99,
'random_number' => 123456,
'expires_at' => '2023-12-31 23:59:59',
]);
$product->name = 'Product 2';
$product->price = 20.99;
$product->random_number = 654321;
$product->save();
$this->assertDatabaseCount(ProductImmutable::class, 1);
$this->assertDatabaseHas(ProductImmutable::class, [
'name' => 'Product 1',
'price' => 10.99,
'random_number' => 123456,
'expires_at' => '2023-12-31 23:59:59',
]);
})->throws(ImmutablePropertyException::class);
it('with fill + save methods', function () {
$product = ProductImmutable::create([
'name' => 'Product 1',
'price' => 10.99,
'random_number' => 123456,
'expires_at' => '2023-12-31 23:59:59',
]);
$product->fill([
'name' => 'Product 2',
'price' => 20.99,
'random_number' => 654321,
])->save();
$this->assertDatabaseCount(ProductImmutable::class, 1);
$this->assertDatabaseHas(ProductImmutable::class, [
'name' => 'Product 1',
'price' => 10.99,
'random_number' => 123456,
'expires_at' => '2023-12-31 23:59:59',
]);
})->throws(ImmutablePropertyException::class);
it('with update method', function () {
$product = ProductImmutable::create([
'name' => 'Product 1',
'price' => 10.99,
'random_number' => 123456,
'expires_at' => '2023-12-31 23:59:59',
]);
$product->update([
'name' => 'Product 2',
'price' => 20.99,
'random_number' => 654321,
]);
$this->assertDatabaseCount(ProductImmutable::class, 1);
$this->assertDatabaseHas(ProductImmutable::class, [
'name' => 'Product 1',
'price' => 10.99,
'random_number' => 123456,
'expires_at' => '2023-12-31 23:59:59',
]);
})->throws(ImmutablePropertyException::class);
});