Skip to content

Commit 61072c9

Browse files
authored
refactor: update tests with old entities definition (#10026)
* refactor: update tests with old entities definition * phpstan baseline * remove unused public properties from UserModel
1 parent c134386 commit 61072c9

File tree

6 files changed

+83
-285
lines changed

6 files changed

+83
-285
lines changed

tests/_support/Models/UserModel.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,4 @@ class UserModel extends Model
3030
protected $returnType = 'object';
3131
protected $useSoftDeletes = true;
3232
protected $dateFormat = 'datetime';
33-
public $name = '';
34-
public $email = '';
35-
public $country = '';
3633
}

tests/system/Models/InsertModelTest.php

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,19 @@ public function testInsertResultFail(): void
194194
public function testInsertBatchNewEntityWithDateTime(): void
195195
{
196196
$entity = new class () extends Entity {
197-
protected $id;
198-
protected $name;
199-
protected $email;
200-
protected $country;
201-
protected $deleted;
202-
protected $created_at;
203-
protected $updated_at;
204-
protected $_options = [
205-
'datamap' => [],
206-
'dates' => [
207-
'created_at',
208-
'updated_at',
209-
'deleted_at',
210-
],
211-
'casts' => [],
197+
protected $attributes = [
198+
'id' => null,
199+
'name' => null,
200+
'email' => null,
201+
'country' => null,
202+
'deleted_at' => null,
203+
'created_at' => null,
204+
'updated_at' => null,
205+
];
206+
protected $dates = [
207+
'created_at',
208+
'updated_at',
209+
'deleted_at',
212210
];
213211
};
214212

@@ -219,13 +217,13 @@ public function testInsertBatchNewEntityWithDateTime(): void
219217
$entity->name = 'Mark One';
220218
$entity->email = 'markone@example.com';
221219
$entity->country = 'India';
222-
$entity->deleted = 0;
220+
$entity->deleted_at = null;
223221
$entity->created_at = new Time('now');
224222

225223
$entityTwo->name = 'Mark Two';
226224
$entityTwo->email = 'marktwo@example.com';
227225
$entityTwo->country = 'India';
228-
$entityTwo->deleted = 0;
226+
$entityTwo->deleted_at = null;
229227
$entityTwo->created_at = $entity->created_at;
230228

231229
$this->setPrivateProperty($this->model, 'useTimestamps', true);
@@ -288,21 +286,10 @@ public function testInsertEntityWithNoDataExceptionNoAllowedData(): void
288286
$this->createModel(UserModel::class);
289287

290288
$entity = new class () extends Entity {
291-
protected $id;
292-
protected $name;
293-
protected $email;
294-
protected $country;
295-
protected $deleted;
296-
protected $created_at;
297-
protected $updated_at;
298-
protected $_options = [
299-
'datamap' => [],
300-
'dates' => [
301-
'created_at',
302-
'updated_at',
303-
'deleted_at',
304-
],
305-
'casts' => [],
289+
protected $dates = [
290+
'created_at',
291+
'updated_at',
292+
'deleted_at',
306293
];
307294
};
308295

tests/system/Models/SaveModelTest.php

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,19 @@ public function testEmptySaveData(): void
239239
public function testSaveNewEntityWithDateTime(): void
240240
{
241241
$entity = new class () extends Entity {
242-
protected $id;
243-
protected $name;
244-
protected $email;
245-
protected $country;
246-
protected $deleted;
247-
protected $created_at;
248-
protected $updated_at;
249-
protected $_options = [
250-
'datamap' => [],
251-
'dates' => [
252-
'created_at',
253-
'updated_at',
254-
'deleted_at',
255-
],
256-
'casts' => [],
242+
protected $attributes = [
243+
'id' => null,
244+
'name' => null,
245+
'email' => null,
246+
'country' => null,
247+
'deleted_at' => null,
248+
'created_at' => null,
249+
'updated_at' => null,
250+
];
251+
protected $dates = [
252+
'created_at',
253+
'updated_at',
254+
'deleted_at',
257255
];
258256
};
259257

@@ -262,7 +260,7 @@ public function testSaveNewEntityWithDateTime(): void
262260
$entity->name = 'Mark';
263261
$entity->email = 'mark@example.com';
264262
$entity->country = 'India';
265-
$entity->deleted = 0;
263+
$entity->deleted_at = null;
266264
$entity->created_at = new Time('now');
267265

268266
$this->setPrivateProperty($this->model, 'useTimestamps', true);
@@ -272,18 +270,16 @@ public function testSaveNewEntityWithDateTime(): void
272270
public function testSaveNewEntityWithDate(): void
273271
{
274272
$entity = new class () extends Entity {
275-
protected $id;
276-
protected $name;
277-
protected $created_at;
278-
protected $updated_at;
279-
protected $_options = [
280-
'datamap' => [],
281-
'dates' => [
282-
'created_at',
283-
'updated_at',
284-
'deleted_at',
285-
],
286-
'casts' => [],
273+
protected $attributes = [
274+
'id' => null,
275+
'name' => null,
276+
'created_at' => null,
277+
'updated_at' => null,
278+
];
279+
protected $dates = [
280+
'created_at',
281+
'updated_at',
282+
'deleted_at',
287283
];
288284
};
289285

tests/system/Models/UpdateModelTest.php

Lines changed: 37 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use CodeIgniter\Entity\Entity;
2020
use CodeIgniter\Exceptions\InvalidArgumentException;
2121
use Config\Database;
22-
use DateTimeInterface;
2322
use PHPUnit\Framework\Attributes\DataProvider;
2423
use PHPUnit\Framework\Attributes\Group;
2524
use stdClass;
@@ -209,64 +208,34 @@ public function testUpdateBatchValidationFail(): void
209208
public function testUpdateBatchWithEntity(): void
210209
{
211210
$entity1 = new class () extends Entity {
212-
protected int $id;
213-
protected string $name;
214-
protected string $email;
215-
protected string $country;
216-
protected bool $deleted;
217-
protected DateTimeInterface $created_at;
218-
protected DateTimeInterface $updated_at;
219-
220-
/**
221-
* @var array{'datamap': array{}, 'dates': array{string, string, string}, 'casts': array{}}
222-
*/
223-
protected $_options = [
224-
'datamap' => [],
225-
'dates' => [
226-
'created_at',
227-
'updated_at',
228-
'deleted_at',
229-
],
230-
'casts' => [],
211+
protected $attributes = [
212+
'id' => null,
213+
'name' => null,
214+
'country' => null,
215+
'deleted_at' => null,
231216
];
232-
};
233-
234-
$entity2 = new class () extends Entity {
235-
protected int $id;
236-
protected string $name;
237-
protected string $email;
238-
protected string $country;
239-
protected bool $deleted;
240-
protected DateTimeInterface $created_at;
241-
protected DateTimeInterface $updated_at;
242-
243-
/**
244-
* @var array{'datamap': array{}, 'dates': array{string, string, string}, 'casts': array{}}
245-
*/
246-
protected $_options = [
247-
'datamap' => [],
248-
'dates' => [
249-
'created_at',
250-
'updated_at',
251-
'deleted_at',
252-
],
253-
'casts' => [],
217+
protected $dates = [
218+
'created_at',
219+
'updated_at',
220+
'deleted_at',
254221
];
255222
};
256223

257-
$entity1->id = 1;
258-
$entity1->name = 'Jones Martin';
259-
$entity1->country = 'India';
260-
$entity1->deleted = 0;
224+
$entity2 = clone $entity1;
225+
226+
$entity1->id = 1;
227+
$entity1->name = 'Jones Martin';
228+
$entity1->country = 'India';
229+
$entity1->deleted_at = null;
261230
$entity1->syncOriginal();
262231
// Update the entity.
263232
$entity1->country = 'China';
264233

265234
// This entity is not updated.
266-
$entity2->id = 4;
267-
$entity2->name = 'Jones Martin';
268-
$entity2->country = 'India';
269-
$entity2->deleted = 0;
235+
$entity2->id = 4;
236+
$entity2->name = 'Jones Martin';
237+
$entity2->country = 'India';
238+
$entity2->deleted_at = null;
270239
$entity2->syncOriginal();
271240

272241
$model = $this->createModel(UserModel::class);
@@ -408,33 +377,27 @@ public function testUpdateWithEntityNoAllowedFields(): void
408377
$this->createModel(UserModel::class);
409378

410379
$entity = new class () extends Entity {
411-
protected int $id;
412-
protected string $name;
413-
protected string $email;
414-
protected string $country;
415-
protected bool $deleted;
416-
protected DateTimeInterface $created_at;
417-
protected DateTimeInterface $updated_at;
418-
419-
/**
420-
* @var array{'datamap': array{}, 'dates': array{string, string, string}, 'casts': array{}}
421-
*/
422-
protected $_options = [
423-
'datamap' => [],
424-
'dates' => [
425-
'created_at',
426-
'updated_at',
427-
'deleted_at',
428-
],
429-
'casts' => [],
380+
protected $attributes = [
381+
'id' => null,
382+
'name' => null,
383+
'email' => null,
384+
'country' => null,
385+
'deleted_at' => null,
386+
'created_at' => null,
387+
'updated_at' => null,
388+
];
389+
protected $dates = [
390+
'created_at',
391+
'updated_at',
392+
'deleted_at',
430393
];
431394
};
432395

433-
$entity->id = 1;
434-
$entity->name = 'Jones Martin';
435-
$entity->email = 'jones@example.org';
436-
$entity->country = 'India';
437-
$entity->deleted = 0;
396+
$entity->id = 1;
397+
$entity->name = 'Jones Martin';
398+
$entity->email = 'jones@example.org';
399+
$entity->country = 'India';
400+
$entity->deleted_at = null;
438401

439402
$id = $this->model->insert($entity);
440403

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 2092 errors
1+
# total 2063 errors
22

33
includes:
44
- argument.type.neon

0 commit comments

Comments
 (0)