/** * Install hstore * /usr/share/postgresql/contrib # cat hstore.sql | psql -U pgsql -d onphp **/ public function testHstore() { foreach (DBTestPool::me()->getPool() as $connector => $db) { DBPool::me()->setDefault($db); $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'))); $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)); } }
public function testErrors() { $form = Form::create()->add(Primitive::ternary('flag')->setFalseValue('0')->setTrueValue('1'))->add(Primitive::integer('old')->required())->addRule('someRule', Expression::between(FormField::create('old'), '18', '35')); //empty import $form->import(array())->checkRules(); //checking $expectingErrors = array('old' => Form::MISSING, 'someRule' => Form::WRONG); $this->assertEquals($expectingErrors, $form->getErrors()); $this->assertEquals(Form::MISSING, $form->getError('old')); $this->assertEquals(Form::WRONG, $form->getError('someRule')); $this->assertTrue($form->hasError('old')); $this->assertFalse($form->hasError('flag')); //drop errors $form->dropAllErrors(); $this->assertEquals(array(), $form->getErrors()); //import wrong data $form->clean()->importMore(array('flag' => '3', 'old' => '17'))->checkRules(); //checking $expectingErrors = array('flag' => Form::WRONG, 'someRule' => Form::WRONG); $this->assertEquals($expectingErrors, $form->getErrors()); $this->assertTrue($form->hasError('someRule')); //marking good and custom check errors $form->markGood('someRule')->markCustom('flag', 3); $this->assertEquals(array('flag' => 3), $form->getErrors()); $this->assertFalse($form->hasError('someRule')); $this->assertNull($form->getError('someRule')); $this->assertEquals(3, $form->getError('flag')); //import right data $form->dropAllErrors()->clean()->importMore(array('flag' => '1', 'old' => '35')); //checking $this->assertEquals(array(), $form->getErrors()); }
public function testChainForm() { $form = Form::create()->add(Primitive::string('a'))->add(Primitive::string('b'))->add(Primitive::integer('c'))->add(Primitive::integer('d'))->add(Primitive::boolean('e'))->add(Primitive::string('f'))->import(array('a' => 'true', 'c' => 123, 'd' => 123)); $andChain = Expression::chain()->expAnd(Expression::expOr(new FormField('a'), Expression::notNull(new FormField('b'))))->expAnd(Expression::eq(new FormField('c'), new FormField('d')))->expAnd(Expression::isFalse(new FormField('e'))); $this->assertTrue($andChain->toBoolean($form)); $form->importMore(array('e' => 'on')); $this->assertFalse($andChain->toBoolean($form)); $orChain = Expression::chain()->expOr(Expression::eq(new FormField('a'), new FormField('b')))->expOr(Expression::expOr(new FormField('e'), Expression::gt(new FormField('c'), new FormField('d'))))->expOr(Expression::in(new FormField('f'), array('qwer', 'asdf', 'zxcv'))); $form->import(array()); $this->assertFalse($orChain->toBoolean($form)); $form->import(array('e' => '1')); $this->assertTrue($orChain->toBoolean($form)); $form->import(array('a' => 'asdf', 'b' => 'qwerq', 'c' => '13', 'd' => '1313', 'f' => 'iukj')); $this->assertFalse($orChain->toBoolean($form)); $form->import(array('c' => '13', 'd' => '12')); $this->assertTrue($orChain->toBoolean($form)); $form->import(array('f' => 'asdfwer')); $this->assertFalse($orChain->toBoolean($form)); $form->import(array('f' => 'qwer')); $this->assertTrue($orChain->toBoolean($form)); }
/** * @return PrimitiveHstore **/ protected function create() { return Primitive::hstore('properties')->setFormMapping(array(Primitive::string('age'), Primitive::integer('weight'), Primitive::string('comment'))); }
public function testInteger() { $prm = Primitive::integer('int'); $this->assertTrue($prm->importValue(0)); $this->assertFalse($prm->importValue('abc')); }