From e2646d46898ab6928a811474e7dc031b94e8354d Mon Sep 17 00:00:00 2001 From: AlexeyDsov Date: Sun, 8 Jul 2012 10:24:47 +0400 Subject: [PATCH] hstore fix --- doc/ChangeLog | 4 + main/Base/Hstore.class.php | 16 ++- test/misc/DAOTest.class.php | 188 ++++++++++++++++++------------------ 3 files changed, 110 insertions(+), 98 deletions(-) diff --git a/doc/ChangeLog b/doc/ChangeLog index 0ccc248e4a..ec784f47ab 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -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 diff --git a/main/Base/Hstore.class.php b/main/Base/Hstore.class.php index baf43750c1..84238beed2 100644 --- a/main/Base/Hstore.class.php +++ b/main/Base/Hstore.class.php @@ -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; } @@ -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; @@ -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; + } } ?> \ No newline at end of file diff --git a/test/misc/DAOTest.class.php b/test/misc/DAOTest.class.php index e0e2baaf21..d19949a603 100644 --- a/test/misc/DAOTest.class.php +++ b/test/misc/DAOTest.class.php @@ -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(); }