/
ModelPropertyExtension.php
242 lines (194 loc) · 5.52 KB
/
ModelPropertyExtension.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
declare(strict_types=1);
namespace Tests\Features\Properties;
use App\Account;
use App\Address;
use App\Group;
use App\GuardedModel;
use App\Role;
use App\Team;
use App\Thread;
use App\User;
use ArrayObject;
use Carbon\Carbon as BaseCarbon;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use function PHPStan\Testing\assertType;
class ModelPropertyExtension
{
/** @var User */
private $user; // @phpstan-ignore-line
/** @var Account */
private $account; // @phpstan-ignore-line
/** @var Role */
private $role; // @phpstan-ignore-line
/** @var Group */
private $group; // @phpstan-ignore-line
/** @var Team */
private $team; // @phpstan-ignore-line
public function testPropertyReturnType(): int
{
return $this->user->id;
}
public function testBooleanProperty(): bool
{
return $this->user->blocked;
}
public function testBooleanAcceptsZero(): void
{
$this->user->blocked = 0;
}
public function testBooleanAcceptsOne(): void
{
$this->user->blocked = 1;
}
public function testBooleanAcceptsFalse(): void
{
$this->user->blocked = false;
}
/**
* @return Carbon|BaseCarbon|null
*/
public function testDateReturnType()
{
return $this->user->created_at;
}
public function testWriteToProperty(): void
{
$this->user->created_at = 'test';
$this->user->created_at = now();
$this->user->created_at = null;
$this->user->created_at = BaseCarbon::now();
}
/** @return array<int, string> */
public function testCast(): array
{
return $this->user->meta;
}
/** @return mixed */
public function testKnownColumnNameWithUnknownType()
{
$this->user->unknown_column = 5;
$this->user->unknown_column = 'foo';
return $this->user->unknown_column;
}
public function testMigrationWithoutSchemaFacadeImport(): string
{
return $this->account->active;
}
public function testReadUUIDProperty(): string
{
return $this->role->id;
}
public function testWriteUUIDProperty(): bool
{
$role = new Role();
$role->id = 'abcd-efgh-ijkl';
return $role->save();
}
public function testReadIdPropertyWhenMigrationsCouldntBeRead(): int
{
return $this->group->id;
}
public function testWriteIdPropertyWhenMigrationsCouldntBeRead(): bool
{
$group = new Group();
$group->id = 5;
return $group->save();
}
public function testReadIdPropertyWhenMigrationsCouldntBeReadAndKeyTypeIsString(): string
{
return $this->team->id;
}
public function testWriteIdPropertyWhenMigrationsCouldntBeReadAndKeyTypeIsString(): bool
{
$team = new Team();
$team->id = 'five';
return $team->save();
}
public function testModelWithGuardedProperties(GuardedModel $guardedModel): string
{
return $guardedModel->name;
}
public function testCustomAccessorOnModels(Thread $thread): string
{
return $thread->custom_property;
}
public function testDateCast(User $user): ?BaseCarbon
{
assertType('string', $user->email);
$user->email_verified_at = now();
return $user->email_verified_at;
}
public function testNullablePropertyWithCast(User $user): void
{
$user->email_verified_at = null;
}
/** @return array<string> */
public function testEncryptedArrayCast(User $user): array
{
return $user->allowed_ips;
}
public function testPrecisionDecimalCast(User $user): string
{
return $user->floatButRoundedDecimalString;
}
/** @return ArrayObject<array-key, mixed> */
public function testAsArrayObjectCast(User $user): ArrayObject
{
return $user->options;
}
public function testAsArrayObjectCastCount(User $user): int
{
return count($user->options);
}
/** @return Collection<array-key, mixed> */
public function testAsCollectionCast(User $user): Collection
{
return $user->properties;
}
public function testAsCollectionCastCount(User $user): int
{
return count($user->properties);
}
/** @phpstan-return mixed */
public function testAsCollectionCastElements(User $user)
{
return $user->properties->first();
}
public function testSoftDeletesCastDateTimeAndNullable(User $user): ?string
{
return $user->deleted_at?->format('d/m/Y');
}
public function testWriteToSoftDeletesColumn(): void
{
$this->user->deleted_at = 'test';
$this->user->deleted_at = now();
$this->user->deleted_at = null;
$this->user->deleted_at = BaseCarbon::now();
}
public function testForeignIdFor(Address $address): int
{
return $address->user_id;
}
public function testForeignIdForName(Address $address): int
{
return $address->custom_foreign_id_for_name;
}
public function testForeignIdUUID(Address $address): string
{
return $address->address_id;
}
public function testForeignIdNullable(Address $address): ?string
{
return $address->nullable_address_id;
}
public function testForeignIdConstrained(Address $address): int
{
return $address->foreign_id_constrained;
}
public function testForeignIdConstrainedNullable(Address $address): ?int
{
return $address->nullable_foreign_id_constrained;
}
}