Skip to content

Commit

Permalink
type casting on update + test
Browse files Browse the repository at this point in the history
  • Loading branch information
Firetawnyowl authored and nekufa committed Dec 4, 2024
1 parent 8f794ed commit 8c60d3a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
57 changes: 57 additions & 0 deletions tests/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8c60d3a

Please sign in to comment.