/** * @param bool $clean * @return DBTestCreator * @throws DatabaseException */ public function dropDB($clean = false) { foreach ($this->pool->getPool() as $name => $db) { /* @var $db DB */ foreach ($this->schema->getTableNames() as $name) { try { $db->queryRaw(OSQL::dropTable($name, true)->toDialectString($db->getDialect())); } catch (DatabaseException $e) { if (!$clean) { throw $e; } } if ($db->hasSequences()) { foreach ($this->schema->getTableByName($name)->getColumns() as $columnName => $column) { try { if ($column->isAutoincrement()) { $db->queryRaw("DROP SEQUENCE {$name}_id;"); } } catch (DatabaseException $e) { if (!$clean) { throw $e; } } } } } } return $this; }
/** * 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 testBoolean() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); //creating moscow $moscow = TestCity::create()->setName('Moscow'); $moscow = $moscow->dao()->add($moscow); $moscowId = $moscow->getId(); /* @var $moscow TestCity */ //now moscow capital $moscow->dao()->merge($moscow->setCapital(true)); TestCity::dao()->dropIdentityMap(); Criteria::create(TestCity::dao())->setSilent(false)->add(Expression::isTrue('capital'))->get(); TestCity::dao()->dropIdentityMap(); $moscow = Criteria::create(TestCity::dao())->setSilent(false)->add(Expression::isNull('large'))->get(); TestCity::dao()->dropIdentityMap(); //now moscow large $moscow = $moscow->dao()->merge($moscow->setLarge(true)); TestCity::dao()->dropIdentityMap(); $moscow = TestCity::dao()->getById($moscowId); $this->assertTrue($moscow->getCapital()); $this->assertTrue($moscow->getLarge()); Criteria::create(TestCity::dao())->setSilent(false)->add(Expression::not(Expression::isFalse('large')))->get(); TestCity::dao()->dropIdentityMap(); } }
/** * @group pi */ public function testCustomImportExport() { $dbs = DBTestPool::me()->getPool(); if (empty($dbs)) { $this->fail('For test required at least one DB in config'); } DBPool::me()->setDefault(reset($dbs)); $moscow = TestCity::create()->setCapital(true)->setName('Moscow'); $moscow->dao()->add($moscow); $stalingrad = TestCity::create()->setCapital(false)->setName('Stalingrad'); $stalingrad->dao()->add($stalingrad); $prms = array(); $prms[] = Primitive::identifier('city')->setScalar(true)->of('TestCity')->setMethodName('PrimitiveIdentifierTest::getCityByName')->setExtractMethod('PrimitiveIdentifierTest::getCityName'); $prms[] = Primitive::identifier('city')->setScalar(true)->of('TestCity')->setMethodName(array(get_class($this), 'getCityByName'))->setExtractMethod(function (TestCity $city) { return $city->getName(); }); foreach ($prms as $prm) { $prm->import(array('city' => 'Moscow')); $this->assertEquals($moscow, $prm->getValue()); $this->assertEquals('Moscow', $prm->exportValue()); $prm->importValue($stalingrad); $this->assertequals($stalingrad, $prm->getValue()); $this->assertequals('Stalingrad', $prm->exportValue()); $prm->import(array('city' => $moscow)); $this->assertEquals($moscow, $prm->getValue()); $this->assertEquals('Moscow', $prm->exportValue()); } }
/** * @test */ public function getPgSQL() { foreach (DBTestPool::me()->getPool() as $db) { if ($db instanceof PgSQL) { return $db; } } $this->markTestIncomplete('Required PgSQL for testing'); }
/** * @param type $type * @return DB */ protected function getDbByType($type) { foreach (DBTestPool::me()->getPool() as $db) { if (get_class($db) == $type) { return $db; } } $this->fail('couldn\'t get db type "' . $type . '"'); }
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(); } }
public function testLazy() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $parent = TestParentObject::create(); $child = TestChildObject::create()->setParent($parent); $parent->dao()->add($parent); $child->dao()->add($child); $this->assertEquals($parent->getId(), Criteria::create(TestChildObject::dao())->setProjection(Projection::property('parent.id', 'parentId'))->add(Expression::eq('id', $child->getId()))->getCustom('parentId')); } }
public function testIpRangeProperty() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $akado = TestInternetProvider::create()->setName('Akada')->setRange(IpRange::create(IpAddress::create('192.168.1.1'), IpAddress::create('192.168.1.42'))); TestInternetProvider::dao()->add($akado); $plainRange = Criteria::create(TestInternetProvider::dao())->addProjection(Projection::property('range'))->add(Expression::eq('name', 'Akada'))->getCustom(); $this->assertEquals($plainRange['range'], '192.168.1.1-192.168.1.42'); TestInternetProvider::dao()->add(TestInternetProvider::create()->setName('DomRu')->setRange(IpRange::create('192.168.2.0/24'))); $list = Criteria::create(TestInternetProvider::dao())->addOrder('id')->getList(); $this->assertEquals(count($list), 2); } }
public function testCount() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $this->getDBCreator()->fillDB(); $count = TestUser::dao()->getTotalCount(); $this->assertGreaterThan(1, $count); $city = TestCity::create()->setId(1); $newUser = TestUser::create()->setCity($city)->setCredentials(Credentials::create()->setNickname('newuser')->setPassword(sha1('newuser')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())); TestUser::dao()->add($newUser); $newCount = TestUser::dao()->getTotalCount(); $this->assertEquals($count + 1, $newCount); } }
public static function suite() { $suite = new TestSuite('onPHP-' . ONPHP_VERSION); foreach (self::$paths as $testPath) { foreach (glob($testPath . '*Test' . EXT_CLASS, GLOB_BRACE) as $file) { $suite->addTestFile($file); } } // meta, DB and DAOs ordered tests portion if (self::$dbs) { try { Singleton::getInstance('DBTestPool', self::$dbs)->connect(); } catch (Exception $e) { Singleton::dropInstance('DBTestPool'); Singleton::getInstance('DBTestPool'); } // build stuff from meta $metaDir = ONPHP_TEST_PATH . 'meta' . DIRECTORY_SEPARATOR; $path = ONPHP_META_PATH . 'bin' . DIRECTORY_SEPARATOR . 'build.php'; $_SERVER['argv'] = array(); $_SERVER['argv'][0] = $path; $_SERVER['argv'][1] = $metaDir . 'config.inc.php'; $_SERVER['argv'][2] = $metaDir . 'config.meta.xml'; $_SERVER['argv'][] = '--force'; $_SERVER['argv'][] = '--no-schema-check'; $_SERVER['argv'][] = '--drop-stale-files'; include $path; // provide paths to autogenerated stuff set_include_path(get_include_path() . PATH_SEPARATOR . ONPHP_META_AUTO_BUSINESS_DIR . PATH_SEPARATOR . ONPHP_META_AUTO_DAO_DIR . PATH_SEPARATOR . ONPHP_META_AUTO_PROTO_DIR . PATH_SEPARATOR . ONPHP_META_DAO_DIR . PATH_SEPARATOR . ONPHP_META_BUSINESS_DIR . PATH_SEPARATOR . ONPHP_META_PROTO_DIR); $daoTest = new DAOTest(); $out = MetaConfiguration::me()->getOutput(); foreach (DBTestPool::me()->getPool() as $connector => $db) { DBPool::me()->setDefault($db); $out->info('Using ')->info(get_class($db), true)->infoLine(' connector.'); try { $daoTest->drop(); } catch (DatabaseException $e) { // previous shutdown was clean } $daoTest->create()->fill(false); MetaConfiguration::me()->checkIntegrity(); $out->newLine(); $daoTest->drop(); } DBPool::me()->dropDefault(); } $suite->addTestSuite('DAOTest'); return $suite; }
public function testRecursionObjects() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $parentProperties = Singleton::getInstance('ProtoTestParentObject')->getPropertyList(); $resultRoot = $parentProperties['root']->getFetchStrategyId() == FetchStrategy::LAZY; $childProperties = Singleton::getInstance('ProtoTestChildObject')->getPropertyList(); $resultParent = $childProperties['parent']->getFetchStrategyId() == FetchStrategy::LAZY; $selfRecursiveProperties = Singleton::getInstance('ProtoTestSelfRecursion')->getPropertyList(); $resultSelfRecursive = $selfRecursiveProperties['parent']->getFetchStrategyId() == FetchStrategy::LAZY; $this->assertTrue($resultRoot); $this->assertTrue($resultParent); $this->assertTrue($resultSelfRecursive); } }
public function testGetByEmptyId() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $this->getByEmptyIdTest(0); $this->getByEmptyIdTest(null); $this->getByEmptyIdTest(''); $this->getByEmptyIdTest('0'); $this->getByEmptyIdTest(false); $empty = TestLazy::create(); $this->assertNull($empty->getCity()); $this->assertNull($empty->getCityOptional()); $this->assertNull($empty->getEnum()); $this->assertNull($empty->getStaticEnum()); } }
public static function suite() { $suite = new TestSuite('onPHP-' . ONPHP_VERSION); // meta, DB and DAOs ordered tests portion if (self::$dbs) { try { /** * @todo fail - constructor with argument, but static method 'me' - without */ Singleton::getInstance('DBTestPool', self::$dbs)->connect(); } catch (Exception $e) { Singleton::dropInstance('DBTestPool'); Singleton::getInstance('DBTestPool'); } // build stuff from meta $metaDir = ONPHP_TEST_PATH . 'meta' . DIRECTORY_SEPARATOR; $path = ONPHP_META_PATH . 'bin' . DIRECTORY_SEPARATOR . 'build.php'; $_SERVER['argv'] = array(); $_SERVER['argv'][0] = $path; $_SERVER['argv'][1] = $metaDir . 'config.inc.php'; $_SERVER['argv'][2] = $metaDir . 'config.meta.xml'; $_SERVER['argv'][] = '--force'; $_SERVER['argv'][] = '--no-schema-check'; $_SERVER['argv'][] = '--drop-stale-files'; include $path; AutoloaderPool::get('onPHP')->addPaths(array(ONPHP_META_AUTO_BUSINESS_DIR, ONPHP_META_AUTO_DAO_DIR, ONPHP_META_AUTO_PROTO_DIR, ONPHP_META_DAO_DIR, ONPHP_META_BUSINESS_DIR, ONPHP_META_PROTO_DIR)); $dBCreator = DBTestCreator::create()->setSchemaPath(ONPHP_META_AUTO_DIR . 'schema.php')->setTestPool(DBTestPool::me()); $out = MetaConfiguration::me()->getOutput(); foreach (DBTestPool::me()->getPool() as $connector => $db) { DBPool::me()->setDefault($db); $out->info('Using ')->info(get_class($db), true)->infoLine(' connector.'); $dBCreator->dropDB(true); $dBCreator->createDB()->fillDB(); MetaConfiguration::me()->checkIntegrity(); $out->newLine(); $dBCreator->dropDB(); } DBPool::me()->dropDefault(); } foreach (self::$paths as $testPath) { foreach (glob($testPath . '*Test' . EXT_CLASS, GLOB_BRACE) as $file) { $suite->addTestFile($file); } } return $suite; }
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'))); } }
protected function setUp() { if (!DBTestPool::me()->getPool()) { $this->markTestSkipped('haven\'t connected database pool'); } }
public function testWorkingWithCache() { $this->create(); foreach (DBTestPool::me()->getPool() as $connector => $db) { DBPool::me()->setDefault($db); $item = TestItem::create()->setName('testItem1'); TestItem::dao()->add($item); $encapsulant = TestEncapsulant::create()->setName('testEncapsulant1'); TestEncapsulant::dao()->add($encapsulant); $subItem1 = TestSubItem::create()->setName('testSubItem1')->setEncapsulant($encapsulant)->setItem($item); $subItem2 = TestSubItem::create()->setName('testSubItem2')->setEncapsulant($encapsulant)->setItem($item); TestSubItem::dao()->add($subItem1); TestSubItem::dao()->add($subItem2); $items = Criteria::create(TestItem::dao())->getList(); foreach ($items as $item) { foreach ($item->getSubItems()->getList() as $subItem) { $this->assertEquals($subItem->getEncapsulant()->getName(), 'testEncapsulant1'); } } $encapsulant = TestEncapsulant::dao()->getById(1); $encapsulant->setName('testEncapsulant1_changed'); TestEncapsulant::dao()->save($encapsulant); // drop identityMap TestEncapsulant::dao()->dropIdentityMap(); TestSubItem::dao()->dropIdentityMap(); TestItem::dao()->dropIdentityMap(); $items = Criteria::create(TestItem::dao())->getList(); foreach ($items as $item) { foreach ($item->getSubItems()->getList() as $subItem) { $this->assertEquals($subItem->getEncapsulant()->getName(), 'testEncapsulant1_changed'); } } // drop identityMap TestEncapsulant::dao()->dropIdentityMap(); TestSubItem::dao()->dropIdentityMap(); TestItem::dao()->dropIdentityMap(); $subItem = TestSubItem::dao()->getById(1); $this->assertEquals($subItem->getEncapsulant()->getName(), 'testEncapsulant1_changed'); // drop identityMap TestEncapsulant::dao()->dropIdentityMap(); TestSubItem::dao()->dropIdentityMap(); TestItem::dao()->dropIdentityMap(); $subItems = Criteria::create(TestSubItem::dao())->getList(); foreach ($subItems as $subItem) { $this->assertEquals($subItem->getEncapsulant()->getName(), 'testEncapsulant1_changed'); } } $this->drop(); }