Skip to content

Commit 8c60d3a

Browse files
Firetawnyowlnekufa
authored andcommitted
type casting on update + test
1 parent 8f794ed commit 8c60d3a

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Space.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,14 @@ public function setIndexes(array $indexes)
461461
public function update($instance, Operations|array $operations)
462462
{
463463
if (is_array($operations)) {
464+
foreach ($this->format as $field) {
465+
if (array_key_exists($field['name'], $operations)) {
466+
$operations[$field['name']] = $this->mapper->converter->formatValue(
467+
$field['type'],
468+
$operations[$field['name']]
469+
);
470+
}
471+
}
464472
$data = $operations;
465473
$operations = null;
466474
foreach ($data as $k => $v) {

tests/MapperTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,63 @@ public function testCreateRow()
265265
$tester->drop();
266266
}
267267

268+
public function testTypeCasting()
269+
{
270+
$mapper = $this->createMapper(arrays: true);
271+
272+
$tester = $mapper->createSpace('tester');
273+
$tester->addProperty('id', 'unsigned');
274+
$tester->addProperty('data', 'unsigned');
275+
276+
$tester->addIndex(['id']);
277+
278+
$testRow = $mapper->create('tester', [
279+
'id' => "1",
280+
'data' => "1",
281+
]);
282+
283+
$testRow2 = $mapper->create('tester', [
284+
'id' => "2",
285+
'data' => true,
286+
]);
287+
288+
$testRow3 = $mapper->create('tester', [
289+
'id' => "3",
290+
'data' => false,
291+
]);
292+
293+
// casting on create
294+
$this->assertSame($testRow['id'], 1);
295+
$this->assertNotSame($testRow['id'], "1");
296+
297+
$this->assertSame($testRow['data'], 1);
298+
$this->assertNotSame($testRow['data'], "1");
299+
300+
$this->assertSame($testRow2['data'], 1);
301+
$this->assertNotSame($testRow['data'], true);
302+
303+
$this->assertSame($testRow3['data'], 0);
304+
$this->assertNotSame($testRow['data'], false);
305+
306+
//casting on update
307+
$mapper->update('tester', $testRow, ['data' => false]);
308+
$mapper->update('tester', $testRow2, ['data' => "5"]);
309+
$mapper->update('tester', $testRow3, ['data' => true]);
310+
311+
$testRow = $mapper->findOne('tester', ['id' => 1]);
312+
$testRow2 = $mapper->findOne('tester', ['id' => 2]);
313+
$testRow3 = $mapper->findOne('tester', ['id' => 3]);
314+
315+
$this->assertSame($testRow['data'], 0);
316+
$this->assertNotSame($testRow['data'], false);
317+
318+
$this->assertSame($testRow2['data'], 5);
319+
$this->assertNotSame($testRow2['data'], "5");
320+
321+
$this->assertSame($testRow3['data'], 1);
322+
$this->assertNotSame($testRow3['data'], true);
323+
}
324+
268325
public function testIndexCasting()
269326
{
270327
$mapper = $this->createMapper(arrays: true);

0 commit comments

Comments
 (0)