コード例 #1
0
 /**
  * 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));
     }
 }
コード例 #2
0
 public function testFormCollection()
 {
     $collection = FormCollection::create(TestCity::proto()->makeForm()->drop('id'));
     $url = HttpUrl::create()->parse('http://i.would.like.to.create.cities/?name[77]=Moscow&capital[77]=1&large[77]=1&name[50]=Krasnogorsk&name[78]=Piter&large[78]=1');
     parse_str($url->getQuery(), $getArray);
     $collection->import($getArray);
     foreach ($collection as $number => $form) {
         switch ($number) {
             case 77:
                 $this->assertEquals('Moscow', $form->getValue('name'));
                 $this->assertTrue($form->getValue('capital'));
                 $this->assertTrue($form->getValue('large'));
                 break;
             case 78:
                 $this->assertEquals('Piter', $form->getValue('name'));
                 $this->assertFalse($form->getValue('capital'));
                 $this->assertTrue($form->getValue('large'));
                 break;
             case 50:
                 $this->assertEquals('Krasnogorsk', $form->getValue('name'));
                 $this->assertFalse($form->getValue('capital'));
                 $this->assertFalse($form->getValue('large'));
                 break;
             default:
                 $this->assertTrue(false);
                 break;
         }
     }
 }
コード例 #3
0
 public function testDialectStringObjects()
 {
     $criteria = Criteria::create(TestUser::dao())->setProjection(Projection::property('id'))->add(Expression::gt('registered', Date::create('2011-01-01')));
     $this->assertEquals($criteria->toDialectString(ImaginaryDialect::me()), 'SELECT test_user.id FROM test_user WHERE (test_user.registered > 2011-01-01)');
     $criteria = Criteria::create(TestUserWithContactExtended::dao())->setProjection(Projection::property('contactExt.city.id', 'cityId'))->add(Expression::eq('contactExt.city', TestCity::create()->setId(22)));
     $this->assertEquals($criteria->toDialectString(ImaginaryDialect::me()), 'SELECT test_user_with_contact_extended.city_id AS cityId FROM test_user_with_contact_extended WHERE (test_user_with_contact_extended.city_id = 22)');
     $cityList = array(TestCity::create()->setId(3), TestCity::create()->setId(44));
     $criteria = Criteria::create(TestUser::dao())->setProjection(Projection::property('id'))->add(Expression::in('city', $cityList));
     $this->assertEquals($criteria->toDialectString(ImaginaryDialect::me()), 'SELECT test_user.id FROM test_user WHERE (test_user.city_id IN (3, 44))');
 }
コード例 #4
0
 public function testCriteria()
 {
     foreach (DBTestPool::me()->getPool() as $db) {
         /* @var $db DB */
         DBPool::me()->setDefault($db);
         $this->getDBCreator()->fillDB();
         $queryResult = Criteria::create(TestCity::dao())->getResult();
         $this->assertEquals(2, $queryResult->getCount());
         Cache::me()->clean();
     }
 }
コード例 #5
0
 public function testIpAddressProperty()
 {
     foreach (DBTestPool::me()->getPool() as $db) {
         DBPool::me()->setDefault($db);
         $city = TestCity::create()->setName('Khimki');
         TestCity::dao()->add($city);
         $userWithIp = TestUser::create()->setCredentials(Credentials::create()->setNickName('postgreser')->setPassword(sha1('postgreser')))->setLastLogin(Timestamp::makeNow())->setRegistered(Timestamp::makeNow())->setCity($city)->setIp(IpAddress::create('127.0.0.1'));
         TestUser::dao()->add($userWithIp);
         $this->assertTrue($userWithIp->getId() >= 1);
         $this->assertTrue($userWithIp->getIp() instanceof IpAddress);
         $plainIp = DBPool::me()->getByDao(TestUser::dao())->queryColumn(OSQL::select()->get('ip')->from(TestUser::dao()->getTable())->where(Expression::eq('id', $userWithIp->getId())));
         $this->assertEquals($plainIp[0], $userWithIp->getIp()->toString());
         $count = Criteria::create(TestUser::dao())->add(Expression::eq('ip', IpAddress::create('127.0.0.1')))->addProjection(Projection::count('*', 'count'))->getCustom('count');
         $this->assertEquals($count, 1);
     }
 }
コード例 #6
0
 public function testInnerTransaction()
 {
     foreach (DBTestPool::me()->getPool() as $connector => $db) {
         DBPool::me()->setDefault($db);
         $this->getDBCreator()->fillDB();
         $moscow = TestCity::dao()->getByLogic(Expression::eq('name', 'Moscow'));
         $piter = TestCity::dao()->getByLogic(Expression::eq('name', 'Saint-Peterburg'));
         $cityNewer = function (TestCity $city) {
             $city->dao()->merge($city->setName('New ' . $city->getName()));
         };
         $citiesNewer = function ($moscow, $piter) use($cityNewer, $db) {
             $cityNewer($moscow);
             InnerTransactionWrapper::create()->setDB($db)->setFunction($cityNewer)->run($piter);
         };
         InnerTransactionWrapper::create()->setDao($moscow->dao())->setFunction($citiesNewer)->run($moscow, $piter);
         $this->assertNotNull(TestCity::dao()->getByLogic(Expression::eq('name', 'New Moscow')));
         $this->assertNotNull(TestCity::dao()->getByLogic(Expression::eq('name', 'New Saint-Peterburg')));
     }
 }
コード例 #7
0
 public function unified()
 {
     $user = TestUser::dao()->getById(1);
     $encapsulant = TestEncapsulant::dao()->getPlainList();
     $collectionDao = $user->getEncapsulants();
     $collectionDao->fetch()->setList($encapsulant);
     $collectionDao->save();
     unset($collectionDao);
     // fetch
     $encapsulantsList = $user->getEncapsulants()->getList();
     $piter = TestCity::dao()->getById(1);
     $moscow = TestCity::dao()->getById(2);
     for ($i = 0; $i < 10; $i++) {
         $this->assertEquals($encapsulantsList[$i]->getId(), $i + 1);
         $this->assertEquals($encapsulantsList[$i]->getName(), $i);
         $cityList = $encapsulantsList[$i]->getCities()->getList();
         $this->assertEquals($cityList[0], $piter);
         $this->assertEquals($cityList[1], $moscow);
     }
     unset($encapsulantsList);
     // lazy fetch
     $encapsulantsList = $user->getEncapsulants(true)->getList();
     for ($i = 1; $i < 11; $i++) {
         $this->assertEquals($encapsulantsList[$i], $i);
     }
     // count
     $user->getEncapsulants()->clean();
     $this->assertEquals($user->getEncapsulants()->getCount(), 10);
     $criteria = Criteria::create(TestEncapsulant::dao())->add(Expression::in('cities.id', array($piter->getId(), $moscow->getId())));
     $user->getEncapsulants()->setCriteria($criteria);
     $this->assertEquals($user->getEncapsulants()->getCount(), 20);
     // distinct count
     $user->getEncapsulants()->clean();
     $user->getEncapsulants()->setCriteria($criteria->setDistinct(true));
     if (DBPool::me()->getLink() instanceof SQLite) {
         // TODO: sqlite does not support such queries yet
         return null;
     }
     $this->assertEquals($user->getEncapsulants()->getCount(), 10);
 }
 /**
  * @return TestCity
  */
 private function spawnCity($options = array())
 {
     $options += array('capital' => true, 'large' => true, 'name' => 'Saint-Peterburg', 'id' => 20);
     return $this->spawnObject(TestCity::create(), $options);
 }
コード例 #9
0
 public function testSqlFunction()
 {
     $criteria = Criteria::create(TestCity::dao())->addProjection(Projection::property(SQLFunction::create('count', SQLFunction::create('substring', BinaryExpression::create('name', BinaryExpression::create(DBValue::create('M....w'), DBValue::create('#'), 'for')->noBrackets(), 'from')->noBrackets()))->setAggregateDistinct()->setAlias('my_alias')));
     $this->assertEquals($criteria->toDialectString(ImaginaryDialect::me()), 'SELECT count(DISTINCT substring(custom_table.name from M....w for #)) AS my_alias FROM custom_table');
 }
コード例 #10
0
 private function lazyTest()
 {
     $city = TestCity::dao()->getById(1);
     $object = TestLazy::dao()->add(TestLazy::create()->setCity($city)->setCityOptional($city)->setEnum(new ImageType(ImageType::getAnyId())));
     Cache::me()->clean();
     $form = TestLazy::proto()->makeForm();
     $form->import(array('id' => $object->getId()));
     $this->assertNotNull($form->getValue('id'));
     FormUtils::object2form($object, $form);
     foreach ($object->proto()->getPropertyList() as $name => $property) {
         if ($property->getRelationId() == MetaRelation::ONE_TO_ONE && $property->getFetchStrategyId() == FetchStrategy::LAZY) {
             $this->assertEquals($object->{$property->getGetter()}(), $form->getValue($name));
         }
     }
 }
コード例 #11
0
 public function testConvertObjectList()
 {
     $list = array(TestCity::create()->setId(42)->setName('Beldyazki'), TestCity::create()->setId(666)->setName('Moscow'));
     $this->assertEquals(array(42, 666), array_keys(ArrayUtils::convertObjectList($list)));
     $this->assertEquals(array('Beldyazki', 'Moscow'), array_keys(ArrayUtils::convertObjectList($list, 'getName')));
 }
コード例 #12
0
 /**
  * @param TestCase $test
  * @return DBTestCreator
  */
 public function fillDB(TestCase $test = null)
 {
     $moscow = TestCity::create()->setName('Moscow');
     $piter = TestCity::create()->setName('Saint-Peterburg');
     $mysqler = TestUser::create()->setCity($moscow)->setCredentials(Credentials::create()->setNickname('mysqler')->setPassword(sha1('mysqler')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())->modify('-1 day'));
     $postgreser = clone $mysqler;
     $postgreser->setCredentials(Credentials::create()->setNickName('postgreser')->setPassword(sha1('postgreser')))->setCity($piter)->setUrl(HttpUrl::create()->parse('http://postgresql.org/'));
     $piter = TestCity::dao()->add($piter);
     $moscow = TestCity::dao()->add($moscow);
     if ($test) {
         $test->assertEquals($piter->getId(), 1);
         $test->assertEquals($moscow->getId(), 2);
     }
     $postgreser = TestUser::dao()->add($postgreser);
     for ($i = 0; $i < 10; $i++) {
         $encapsulant = TestEncapsulant::dao()->add(TestEncapsulant::create()->setName($i));
         $encapsulant->getCities()->fetch()->setList(array($piter, $moscow))->save();
     }
     $mysqler = TestUser::dao()->add($mysqler);
     if ($test) {
         $test->assertEquals($postgreser->getId(), 1);
         $test->assertEquals($mysqler->getId(), 2);
     }
     if ($test) {
         // put them in cache now
         TestUser::dao()->dropIdentityMap();
         TestUser::dao()->getById(1);
         TestUser::dao()->getById(2);
         if ($test instanceof DBDataTest) {
             $test->getListByIdsTest();
         }
         Cache::me()->clean();
         $test->assertTrue($postgreser == TestUser::dao()->getById(1));
         $test->assertTrue($mysqler == TestUser::dao()->getById(2));
     }
     $firstClone = clone $postgreser;
     $secondClone = clone $mysqler;
     $firstCount = TestUser::dao()->dropById($postgreser->getId());
     $secondCount = TestUser::dao()->dropByIds(array($mysqler->getId()));
     if ($test) {
         $test->assertEquals($firstCount, 1);
         $test->assertEquals($secondCount, 1);
         try {
             TestUser::dao()->getById(1);
             $test->fail();
         } catch (ObjectNotFoundException $e) {
             /* pass */
         }
         $result = Criteria::create(TestUser::dao())->add(Expression::eq(1, 2))->getResult();
         $test->assertEquals($result->getCount(), 0);
         $test->assertEquals($result->getList(), array());
     }
     TestUser::dao()->import($firstClone);
     TestUser::dao()->import($secondClone);
     if ($test && $test instanceof DBDataTest) {
         // cache multi-get
         $test->getListByIdsTest();
         $test->getListByIdsTest();
     }
     return $this;
 }
コード例 #13
0
 public static function getCityName(TestCity $city)
 {
     return $city->getName();
 }