Skip to content

Commit b768a0e

Browse files
author
Alex Schmitz
committed
Add tests for model update empty checks.
1 parent b5b6ddf commit b768a0e

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

tests/system/Models/UpdateModelTest.php

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,26 @@ public function testUpdateObjectWithDataException(): void
260260
$this->createModel(EventModel::class);
261261

262262
$data = (object) [
263+
'name' => 'Foo',
264+
'email' => 'foo@example.com',
265+
'country' => 'US',
266+
'deleted' => 0,
267+
];
268+
269+
$id = $this->model->insert($data);
270+
271+
$data = new stdClass();
272+
273+
$this->expectException(DataException::class);
274+
$this->expectExceptionMessage('There is no data to update.');
275+
$this->model->update($id, $data);
276+
}
277+
278+
public function testUpdateArrayWithDataExceptionNoAllowedFields(): void
279+
{
280+
$this->createModel(EventModel::class);
281+
282+
$data = [
263283
'name' => 'Foo',
264284
'email' => 'foo@example.com',
265285
'country' => 'US',
@@ -268,11 +288,50 @@ public function testUpdateObjectWithDataException(): void
268288

269289
$id = $this->model->insert($data);
270290

271-
$data = new stdClass();
291+
$this->expectException(DataException::class);
292+
$this->expectExceptionMessage('There is no data to update.');
293+
$this->model->update($id, ['thisKeyIsNotAllowed' => 'Bar']);
294+
}
295+
296+
public function testUpdateWithEntityNoAllowedFields(): void
297+
{
298+
$this->createModel(UserModel::class);
299+
300+
$entity = new class extends Entity
301+
{
302+
protected $id;
303+
protected $name;
304+
protected $email;
305+
protected $country;
306+
protected $deleted;
307+
protected $created_at;
308+
protected $updated_at;
309+
310+
protected $_options = [
311+
'datamap' => [],
312+
'dates' => [
313+
'created_at',
314+
'updated_at',
315+
'deleted_at',
316+
],
317+
'casts' => [],
318+
];
319+
};
320+
321+
$entity->id = 1;
322+
$entity->name = 'Jones Martin';
323+
$entity->country = 'India';
324+
$entity->deleted = 0;
325+
326+
$id = $this->model->insert($entity);
327+
328+
$entity->syncOriginal();
329+
330+
$entity->fill(['thisKeyIsNotAllowed' => 'Bar']);
272331

273332
$this->expectException(DataException::class);
274333
$this->expectExceptionMessage('There is no data to update.');
275-
$this->model->update($id, $data);
334+
$this->model->update($id, $entity);
276335
}
277336

278337
public function testUseAutoIncrementSetToFalseUpdate(): void
@@ -301,9 +360,9 @@ public function testUpdateWithSetAndEscape(): void
301360
$this->assertTrue($this->model->set('country', '2+2', false)->set('email', '1+1')->update(1, $userData));
302361

303362
$this->seeInDatabase('user', [
304-
'name' => 'Scott',
363+
'name' => 'Scott',
305364
'country' => '4',
306-
'email' => '1+1',
365+
'email' => '1+1',
307366
]);
308367
}
309368
}

0 commit comments

Comments
 (0)