Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2012-07-08 Alexey S. Denisov

* main/Base/Hstore.class.php: hstore fix and tests update

2012-07-03 Alexey S. Denisov

* core/Cache/Cache.class.php
Expand Down
16 changes: 11 additions & 5 deletions main/Base/Hstore.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ public function toValue($raw)
if (!$raw)
return $this;

$return = null;
eval('$return = array('.$raw.');');
$this->properties = $return;
$this->properties = $this->parseString($raw);

return $this;
}
Expand All @@ -112,9 +110,9 @@ public function toString()

foreach ($this->properties as $k => $v) {
if ($v !== null)
$string .= "'".$this->quoteValue($k)."'=>'".$this->quoteValue($v)."',";
$string .= "\"{$this->quoteValue($k)}\"=>\"{$this->quoteValue($v)}\",";
else
$string .= "'{$this->quoteValue($k)}'=>NULL,";
$string .= "\"{$this->quoteValue($k)}\"=>NULL,";
}

return $string;
Expand All @@ -124,5 +122,13 @@ protected function quoteValue($value)
{
return addslashes($value);
}

private function parseString($raw)
{
$raw = preg_replace('/([$])/u', "\\\\$1", $raw);
$unescapedHStore = array();
eval('$unescapedHStore = array(' . $raw . ');');
return $unescapedHStore;
}
}
?>
188 changes: 95 additions & 93 deletions test/misc/DAOTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,100 +547,102 @@ public function testHstore()
{
$this->create();

$properties = array(
'age' => '23',
'weight' => 80,
'comment' => null,
);

$user =
TestUser::create()->
setCity(
$moscow = TestCity::create()->
setName('Moscow')
)->
setCredentials(
Credentials::create()->
setNickname('fake')->
setPassword(sha1('passwd'))
)->
setLastLogin(
Timestamp::create(time())
)->
setRegistered(
Timestamp::create(time())->modify('-1 day')
)->
setProperties(Hstore::make($properties));

$moscow = TestCity::dao()->add($moscow);

$user = TestUser::dao()->add($user);

Cache::me()->clean();
TestUser::dao()->dropIdentityMap();

$user = TestUser::dao()->getById('1');

$this->assertInstanceOf('Hstore', $user->getProperties());

$this->assertEquals(
$properties,
$user->getProperties()->getList()
);


$form = TestUser::proto()->makeForm();

$form->get('properties')->
setFormMapping(
array(
Primitive::string('age'),
Primitive::integer('weight'),
Primitive::string('comment'),
)
foreach (DBTestPool::me()->getPool() as $connector => $db) {
DBPool::me()->setDefault($db);
$properties = array(
'age' => '23',
'weight' => 80,
'comment' => null,
);

$form->import(
array('id' => $user->getId())
);

$this->assertNotNull($form->getValue('id'));

$object = $user;

FormUtils::object2form($object, $form);

$this->assertInstanceOf('Hstore', $form->getValue('properties'));

$this->assertEquals(
array_filter($properties),
$form->getValue('properties')->getList()
);

$subform = $form->get('properties')->getInnerForm();

$this->assertEquals(
$subform->getValue('age'),
'23'
);

$this->assertEquals(
$subform->getValue('weight'),
80
);

$this->assertNull(
$subform->getValue('comment')
);

$user = new TestUser();

FormUtils::form2object($form, $user, false);

$this->assertEquals(
$user->getProperties()->getList(),
array_filter($properties)
);

$user =
TestUser::create()->
setCity(
$moscow = TestCity::create()->
setName('Moscow')
)->
setCredentials(
Credentials::create()->
setNickname('fake')->
setPassword(sha1('passwd'))
)->
setLastLogin(
Timestamp::create(time())
)->
setRegistered(
Timestamp::create(time())->modify('-1 day')
)->
setProperties(Hstore::make($properties));

$moscow = TestCity::dao()->add($moscow);

$user = TestUser::dao()->add($user);

Cache::me()->clean();
TestUser::dao()->dropIdentityMap();

$user = TestUser::dao()->getById('1');

$this->assertInstanceOf('Hstore', $user->getProperties());

$this->assertEquals(
$properties,
$user->getProperties()->getList()
);

$form = TestUser::proto()->makeForm();

$form->get('properties')->
setFormMapping(
array(
Primitive::string('age'),
Primitive::integer('weight'),
Primitive::string('comment'),
)
);

$form->import(
array('id' => $user->getId())
);

$this->assertNotNull($form->getValue('id'));

$object = $user;

FormUtils::object2form($object, $form);

$this->assertInstanceOf('Hstore', $form->getValue('properties'));

$this->assertEquals(
array_filter($properties),
$form->getValue('properties')->getList()
);

$subform = $form->get('properties')->getInnerForm();

$this->assertEquals(
$subform->getValue('age'),
'23'
);

$this->assertEquals(
$subform->getValue('weight'),
80
);

$this->assertNull(
$subform->getValue('comment')
);

$user = new TestUser();

FormUtils::form2object($form, $user, false);

$this->assertEquals(
$user->getProperties()->getList(),
array_filter($properties)
);
}

$this->drop();
}
Expand Down