From 8c60d3ab806ccdb80948469008bc3006dfda73dc Mon Sep 17 00:00:00 2001 From: Firetawnyowl Date: Wed, 4 Dec 2024 14:15:25 +0300 Subject: [PATCH] type casting on update + test --- src/Space.php | 8 +++++++ tests/MapperTest.php | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/Space.php b/src/Space.php index 44b9e28..d43e84a 100644 --- a/src/Space.php +++ b/src/Space.php @@ -461,6 +461,14 @@ public function setIndexes(array $indexes) public function update($instance, Operations|array $operations) { if (is_array($operations)) { + foreach ($this->format as $field) { + if (array_key_exists($field['name'], $operations)) { + $operations[$field['name']] = $this->mapper->converter->formatValue( + $field['type'], + $operations[$field['name']] + ); + } + } $data = $operations; $operations = null; foreach ($data as $k => $v) { diff --git a/tests/MapperTest.php b/tests/MapperTest.php index c3623d7..11dde0a 100644 --- a/tests/MapperTest.php +++ b/tests/MapperTest.php @@ -265,6 +265,63 @@ public function testCreateRow() $tester->drop(); } + public function testTypeCasting() + { + $mapper = $this->createMapper(arrays: true); + + $tester = $mapper->createSpace('tester'); + $tester->addProperty('id', 'unsigned'); + $tester->addProperty('data', 'unsigned'); + + $tester->addIndex(['id']); + + $testRow = $mapper->create('tester', [ + 'id' => "1", + 'data' => "1", + ]); + + $testRow2 = $mapper->create('tester', [ + 'id' => "2", + 'data' => true, + ]); + + $testRow3 = $mapper->create('tester', [ + 'id' => "3", + 'data' => false, + ]); + + // casting on create + $this->assertSame($testRow['id'], 1); + $this->assertNotSame($testRow['id'], "1"); + + $this->assertSame($testRow['data'], 1); + $this->assertNotSame($testRow['data'], "1"); + + $this->assertSame($testRow2['data'], 1); + $this->assertNotSame($testRow['data'], true); + + $this->assertSame($testRow3['data'], 0); + $this->assertNotSame($testRow['data'], false); + + //casting on update + $mapper->update('tester', $testRow, ['data' => false]); + $mapper->update('tester', $testRow2, ['data' => "5"]); + $mapper->update('tester', $testRow3, ['data' => true]); + + $testRow = $mapper->findOne('tester', ['id' => 1]); + $testRow2 = $mapper->findOne('tester', ['id' => 2]); + $testRow3 = $mapper->findOne('tester', ['id' => 3]); + + $this->assertSame($testRow['data'], 0); + $this->assertNotSame($testRow['data'], false); + + $this->assertSame($testRow2['data'], 5); + $this->assertNotSame($testRow2['data'], "5"); + + $this->assertSame($testRow3['data'], 1); + $this->assertNotSame($testRow3['data'], true); + } + public function testIndexCasting() { $mapper = $this->createMapper(arrays: true);