diff --git a/doc/ChangeLog b/doc/ChangeLog index fec84171cc..8f93bac6c3 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,6 @@ +2012-09-16 Alexey S. Denisov + * DAOTest splitted to more Test classes in new db directory + 2012-09-12 Alexey S. Denisov * main/Net/GenericUri.class.php main/Net/HttpUrl.class.php diff --git a/test/AllTests.php b/test/AllTests.php index 7ba2e66e3f..71a0f205a1 100644 --- a/test/AllTests.php +++ b/test/AllTests.php @@ -1,6 +1,4 @@ markTestSuiteSkipped('No more workers available.'); - } - } - - public function tearDown() - { - echo "\n"; - } - } + mb_internal_encoding(ENCODING); + mb_regex_encoding(ENCODING); + + set_include_path( + // current path + get_include_path().PATH_SEPARATOR + .ONPHP_TEST_PATH.'misc'.PATH_SEPARATOR + ); + + $testPathes = array( + ONPHP_TEST_PATH.'core'.DIRECTORY_SEPARATOR, + ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR, + ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Ip'.DIRECTORY_SEPARATOR, + ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR, + ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR, + ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR.'Routers'.DIRECTORY_SEPARATOR, + ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR.'AMQP'.DIRECTORY_SEPARATOR, + ONPHP_TEST_PATH.'db'.DIRECTORY_SEPARATOR, + ); + + $config = dirname(__FILE__).'/config.inc.php'; + + include is_readable($config) ? $config : $config.'.tpl'; final class AllTests { @@ -53,13 +55,12 @@ 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 { + /** + * @todo fail - constructor with argument, but static method 'me' - without + */ Singleton::getInstance('DBTestPool', self::$dbs)->connect(); } catch (Exception $e) { Singleton::dropInstance('DBTestPool'); @@ -97,7 +98,9 @@ public static function suite() .ONPHP_META_PROTO_DIR ); - $daoTest = new DAOTest(); + $dBCreator = DBTestCreator::create()-> + setSchemaPath(ONPHP_META_AUTO_DIR.'schema.php')-> + setTestPool(DBTestPool::me()); $out = MetaConfiguration::me()->getOutput(); @@ -109,24 +112,22 @@ public static function suite() info(get_class($db), true)-> infoLine(' connector.'); - try { - $daoTest->drop(); - } catch (DatabaseException $e) { - // previous shutdown was clean - } + $dBCreator->dropDB(true); - $daoTest->create()->fill(false); + $dBCreator->createDB()->fillDB(); MetaConfiguration::me()->checkIntegrity(); $out->newLine(); - $daoTest->drop(); + $dBCreator->dropDB(); } DBPool::me()->dropDefault(); } - $suite->addTestSuite('DAOTest'); + foreach (self::$paths as $testPath) + foreach (glob($testPath.'*Test'.EXT_CLASS, GLOB_BRACE) as $file) + $suite->addTestFile($file); return $suite; } diff --git a/test/config.inc.php.tpl b/test/config.inc.php.tpl index bfea8866f0..683dee0bf6 100644 --- a/test/config.inc.php.tpl +++ b/test/config.inc.php.tpl @@ -1,29 +1,4 @@ array( diff --git a/test/db/CacheAndLazyDBTest.class.php b/test/db/CacheAndLazyDBTest.class.php new file mode 100644 index 0000000000..42d4a17f2e --- /dev/null +++ b/test/db/CacheAndLazyDBTest.class.php @@ -0,0 +1,127 @@ +getPool() as $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' + ); + } + } + } + + 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') + ); + } + } + } +?> \ No newline at end of file diff --git a/test/db/CountAndUnifiedDBTest.class.php b/test/db/CountAndUnifiedDBTest.class.php new file mode 100644 index 0000000000..31c0b8dab4 --- /dev/null +++ b/test/db/CountAndUnifiedDBTest.class.php @@ -0,0 +1,131 @@ +getPool() as $db) { + DBPool::me()->setDefault($db); + $this->getDBCreator()->fillDB(); + + $this->unified(); + + Cache::me()->clean(); + + TestUser::dao()->dropById(1); + try { + TestUser::dao()->dropByIds(array(1, 2)); + $this->fail(); + } catch (WrongStateException $e) { + // ok + } + } + } + + 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 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); + } + } +?> \ No newline at end of file diff --git a/test/db/CriteriaDBTest.class.php b/test/db/CriteriaDBTest.class.php new file mode 100644 index 0000000000..e7346c76c4 --- /dev/null +++ b/test/db/CriteriaDBTest.class.php @@ -0,0 +1,18 @@ +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(); + } + } + } +?> \ No newline at end of file diff --git a/test/db/DataDBTest.class.php b/test/db/DataDBTest.class.php new file mode 100644 index 0000000000..5a3c3d048c --- /dev/null +++ b/test/db/DataDBTest.class.php @@ -0,0 +1,227 @@ +getPool() as $db) { + DBPool::me()->setDefault($db); + $this->getDBCreator()->fillDB($this); + + $this->getSome(); // 41! + Cache::me()->clean(); + $this->getSome(); + + $this->nonIntegerIdentifier(); + + $this->racySave(); + $this->binaryTest(); + $this->lazyTest(); + } + } + + 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(); + } + } + + /** + * this method used in DBTestCreator::fill + */ + public function getListByIdsTest() + { + $first = TestUser::dao()->getById(1); + + TestUser::dao()->dropIdentityMap(); + + $list = TestUser::dao()->getListByIds(array(1, 3, 2, 1, 1, 1)); + + $this->assertEquals(count($list), 5); + + $this->assertEquals($list[0]->getId(), 1); + $this->assertEquals($list[1]->getId(), 2); + $this->assertEquals($list[2]->getId(), 1); + $this->assertEquals($list[3]->getId(), 1); + $this->assertEquals($list[4]->getId(), 1); + + $this->assertEquals($list[0], $first); + + $this->assertEquals( + array(), + TestUser::dao()->getListByIds(array(42, 42, 1738)) + ); + } + + private function getSome() + { + for ($i = 1; $i < 3; ++$i) { + $this->assertTrue( + TestUser::dao()->getByLogic( + Expression::eq('city_id', $i) + ) + == TestUser::dao()->getById($i) + ); + } + + $this->assertEquals( + count(TestUser::dao()->getPlainList()), + count(TestCity::dao()->getPlainList()) + ); + } + + private function nonIntegerIdentifier() + { + $id = 'non-integer-one'; + $binaryData = "\0!bbq!\0"; + + $bin = + TestBinaryStuff::create()-> + setId($id)-> + setData($binaryData); + + try { + TestBinaryStuff::dao()->import($bin); + } catch (DatabaseException $e) { + return $this->fail($e->getMessage()); + } + + TestBinaryStuff::dao()->dropIdentityMap(); + Cache::me()->clean(); + + $prm = Primitive::prototypedIdentifier('TestBinaryStuff', 'id'); + + $this->assertTrue($prm->import(array('id' => $id))); + $this->assertSame($prm->getValue()->getId(), $id); + + $binLoaded = TestBinaryStuff::dao()->getById($id); + $this->assertEquals($binLoaded, $bin); + $this->assertEquals($binLoaded->getData(), $binaryData); + $this->assertEquals(TestBinaryStuff::dao()->dropById($id), 1); + + $integerIdPrimitive = Primitive::prototypedIdentifier('TestUser'); + try { + $integerIdPrimitive->import(array('id' => 'string-instead-of-integer')); + } catch (DatabaseException $e) { + return $this->fail($e->getMessage()); + } + } + + private function racySave() + { + $lost = + TestCity::create()-> + setId(424242)-> + setName('inexistant city'); + + try { + TestCity::dao()->save($lost); + + $this->fail(); + } catch (WrongStateException $e) { + /* pass */ + } + } + + private function binaryTest() + { + $data = null; + + for ($i = 0; $i < 256; ++$i) + $data .= chr($i); + + $id = sha1('all sessions are evil'); + + $stuff = + TestBinaryStuff::create()-> + setId($id)-> + setData($data); + + $stuff = $stuff->dao()->import($stuff); + + Cache::me()->clean(); + + $this->assertEquals( + TestBinaryStuff::dao()->getById($id)->getData(), + $data + ); + + TestBinaryStuff::dao()->dropById($id); + } + + private function lazyTest() + { + $city = TestCity::dao()->getById(1); + + $object = TestLazy::dao()->add( + TestLazy::create()-> + setCity($city)-> + setCityOptional($city)-> + setEnum( + new ImageType(ImageType::getAnyId()) + )->setStaticEnum( + new MimeType(MimeType::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) + ); + } + } + } + } +?> \ No newline at end of file diff --git a/test/db/HstoreDBTest.class.php b/test/db/HstoreDBTest.class.php new file mode 100644 index 0000000000..a499f5fd48 --- /dev/null +++ b/test/db/HstoreDBTest.class.php @@ -0,0 +1,108 @@ +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) + ); + } + } + } +?> \ No newline at end of file diff --git a/test/db/IdDBTest.class.php b/test/db/IdDBTest.class.php new file mode 100644 index 0000000000..d85733583e --- /dev/null +++ b/test/db/IdDBTest.class.php @@ -0,0 +1,46 @@ +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 function testStringIdentifier() + { + $identifier = + TestStringIdentifier::proto()->getPropertyByName('id'); + + $this->assertEquals($identifier->getType(), 'scalarIdentifier'); + + $identifier = + TestStringIdentifierRelated::proto()->getPropertyByName('test'); + + $this->assertEquals($identifier->getType(), 'scalarIdentifier'); + } + + private function getByEmptyIdTest($id) + { + try { + TestUser::dao()->getById($id); + $this->fail(); + } catch (WrongArgumentException $e) { + // pass + } + } + } +?> \ No newline at end of file diff --git a/test/db/InnerTransactionDBTest.class.php b/test/db/InnerTransactionDBTest.class.php new file mode 100644 index 0000000000..13c8708050 --- /dev/null +++ b/test/db/InnerTransactionDBTest.class.php @@ -0,0 +1,36 @@ +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'))); + } + } + } +?> \ No newline at end of file diff --git a/test/db/IpDBTest.class.php b/test/db/IpDBTest.class.php new file mode 100644 index 0000000000..8a36fbb13f --- /dev/null +++ b/test/db/IpDBTest.class.php @@ -0,0 +1,104 @@ +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); + } + } + + 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); + } + } + } +?> \ No newline at end of file diff --git a/test/db/RecursionDBTest.class.php b/test/db/RecursionDBTest.class.php new file mode 100644 index 0000000000..66b4553938 --- /dev/null +++ b/test/db/RecursionDBTest.class.php @@ -0,0 +1,79 @@ +markTestSkipped('wontfix'); + + foreach (DBTestPool::me()->getPool() as $db) { + DBPool::me()->setDefault($db); + TestObject::dao()->import( + TestObject::create()-> + setId(1)-> + setName('test object') + ); + + TestType::dao()->import( + TestType::create()-> + setId(1)-> + setName('test type') + ); + + $type = TestType::dao()->getById(1); + + $type->getObjects()->fetch()->setList( + array(TestObject::dao()->getById(1)) + )-> + save(); + + $object = TestObject::dao()->getById(1); + + TestObject::dao()->save($object->setName('test object modified')); + + $list = $type->getObjects()->getList(); + + $modifiedObject = TestObject::dao()->getById(1); + + $this->assertEquals($list[0], $modifiedObject); + } + } + + 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); + } + } + } +?> \ No newline at end of file diff --git a/test/misc/DAOTest.class.php b/test/misc/DAOTest.class.php deleted file mode 100644 index d19949a603..0000000000 --- a/test/misc/DAOTest.class.php +++ /dev/null @@ -1,1025 +0,0 @@ -schema-> - getTableByName('test_parent_object')-> - getColumnByName('root_id')-> - dropReference(); - - return parent::create(); - } - - public function testSchema() - { - return $this->create()->drop(); - } - - public function testData() - { - $this->create(); - - foreach (DBTestPool::me()->getPool() as $connector => $db) { - DBPool::me()->setDefault($db); - $this->fill(); - - $this->getSome(); // 41! - Cache::me()->clean(); - $this->getSome(); - - $this->nonIntegerIdentifier(); - - $this->racySave(); - $this->binaryTest(); - $this->lazyTest(); - } - - $this->drop(); - } - - public function testBoolean() - { - $this->create(); - - foreach (DBTestPool::me()->getPool() as $connector => $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(); - } - - $this->drop(); - } - - public function testInnerTransaction() - { - $this->create(); - - foreach (DBTestPool::me()->getPool() as $connector => $db) { - DBPool::me()->setDefault($db); - $this->fill(); - - $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'))); - } - - $this->drop(); - } - - public function testCriteria() - { - $this->create(); - - foreach (DBTestPool::me()->getPool() as $connector => $db) { - DBPool::me()->setDefault($db); - $this->fill(); - - $this->criteriaResult(); - - Cache::me()->clean(); - } - - $this->deletedCount(); - - $this->drop(); - } - - public function testUnified() - { - $this->create(); - - foreach (DBTestPool::me()->getPool() as $connector => $db) { - DBPool::me()->setDefault($db); - $this->fill(); - - $this->unified(); - - Cache::me()->clean(); - } - - $this->deletedCount(); - - $this->drop(); - } - - public function testCount() - { - $this->create(); - - foreach (DBTestPool::me()->getPool() as $connector => $db) { - DBPool::me()->setDefault($db); - - $this->fill(); - - $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); - } - - $this->drop(); - } - - public function testGetByEmptyId() - { - $this->create(); - - $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()); - - $this->drop(); - } - - public function deletedCount() - { - TestUser::dao()->dropById(1); - - try { - TestUser::dao()->dropByIds(array(1, 2)); - $this->fail(); - } catch (WrongStateException $e) { - // ok - } - } - - public function fill($assertions = true) - { - $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 ($assertions) { - $this->assertEquals($piter->getId(), 1); - $this->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 ($assertions) { - $this->assertEquals($postgreser->getId(), 1); - $this->assertEquals($mysqler->getId(), 2); - } - - if ($assertions) { - // put them in cache now - TestUser::dao()->dropIdentityMap(); - - TestUser::dao()->getById(1); - TestUser::dao()->getById(2); - - $this->getListByIdsTest(); - - Cache::me()->clean(); - - $this->assertTrue( - ($postgreser == TestUser::dao()->getById(1)) - ); - - $this->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 ($assertions) { - $this->assertEquals($firstCount, 1); - $this->assertEquals($secondCount, 1); - - try { - TestUser::dao()->getById(1); - $this->fail(); - } catch (ObjectNotFoundException $e) { - /* pass */ - } - - $result = - Criteria::create(TestUser::dao())-> - add(Expression::eq(1, 2))-> - getResult(); - - $this->assertEquals($result->getCount(), 0); - $this->assertEquals($result->getList(), array()); - } - - TestUser::dao()->import($firstClone); - TestUser::dao()->import($secondClone); - - if ($assertions) { - // cache multi-get - $this->getListByIdsTest(); - $this->getListByIdsTest(); - } - } - - public function criteriaResult() - { - $queryResult = Criteria::create(TestCity::dao())->getResult(); - $this->assertEquals(2, $queryResult->getCount()); - } - - 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); - } - - 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(); - } - - /** - * Install hstore - * /usr/share/postgresql/contrib # cat hstore.sql | psql -U pgsql -d onphp - **/ - public function testHstore() - { - $this->create(); - - 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) - ); - } - - $this->drop(); - } - - /** - * @see http://lists.shadanakar.org/onphp-dev-ru/0811/0774.html - **/ - public function testRecursiveContainers() - { - $this->markTestSkipped('wontfix'); - - $this->create(); - - TestObject::dao()->import( - TestObject::create()-> - setId(1)-> - setName('test object') - ); - - TestType::dao()->import( - TestType::create()-> - setId(1)-> - setName('test type') - ); - - $type = TestType::dao()->getById(1); - - $type->getObjects()->fetch()->setList( - array(TestObject::dao()->getById(1)) - )-> - save(); - - $object = TestObject::dao()->getById(1); - - TestObject::dao()->save($object->setName('test object modified')); - - $list = $type->getObjects()->getList(); - - $modifiedObject = TestObject::dao()->getById(1); - - $this->assertEquals($list[0], $modifiedObject); - - $this->drop(); - } - - public function testRecursionObjects() - { - $this->create(); - - $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->drop(); - - $this->assertTrue($resultRoot); - $this->assertTrue($resultParent); - $this->assertTrue($resultSelfRecursive); - } - - public function testStringIdentifier() - { - $identifier = - TestStringIdentifier::proto()->getPropertyByName('id'); - - $this->assertEquals($identifier->getType(), 'scalarIdentifier'); - - $identifier = - TestStringIdentifierRelated::proto()->getPropertyByName('test'); - - $this->assertEquals($identifier->getType(), 'scalarIdentifier'); - } - - public function nonIntegerIdentifier() - { - $id = 'non-integer-one'; - $binaryData = "\0!bbq!\0"; - - $bin = - TestBinaryStuff::create()-> - setId($id)-> - setData($binaryData); - - try { - TestBinaryStuff::dao()->import($bin); - } catch (DatabaseException $e) { - return $this->fail($e->getMessage()); - } - - TestBinaryStuff::dao()->dropIdentityMap(); - Cache::me()->clean(); - - $prm = Primitive::prototypedIdentifier('TestBinaryStuff', 'id'); - - $this->assertTrue($prm->import(array('id' => $id))); - $this->assertSame($prm->getValue()->getId(), $id); - - $binLoaded = TestBinaryStuff::dao()->getById($id); - $this->assertEquals($binLoaded, $bin); - $this->assertEquals($binLoaded->getData(), $binaryData); - $this->assertEquals(TestBinaryStuff::dao()->dropById($id), 1); - - $integerIdPrimitive = Primitive::prototypedIdentifier('TestUser'); - try { - $integerIdPrimitive->import(array('id' => 'string-instead-of-integer')); - } catch (DatabaseException $e) { - return $this->fail($e->getMessage()); - } - } - - public function testIpAddressProperty() - { - $this->create(); - - $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); - - $this->drop(); - } - - public function testIpRangeProperty() - { - $this->create(); - - $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); - - $this->drop(); - } - - public function testLazy() - { - $this->create(); - - $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') - ); - - $this->drop(); - } - - protected function getSome() - { - for ($i = 1; $i < 3; ++$i) { - $this->assertTrue( - TestUser::dao()->getByLogic( - Expression::eq('city_id', $i) - ) - == TestUser::dao()->getById($i) - ); - } - - $this->assertEquals( - count(TestUser::dao()->getPlainList()), - count(TestCity::dao()->getPlainList()) - ); - } - - private function racySave() - { - $lost = - TestCity::create()-> - setId(424242)-> - setName('inexistant city'); - - try { - TestCity::dao()->save($lost); - - $this->fail(); - } catch (WrongStateException $e) { - /* pass */ - } - } - - private function binaryTest() - { - $data = null; - - for ($i = 0; $i < 256; ++$i) - $data .= chr($i); - - $id = sha1('all sessions are evil'); - - $stuff = - TestBinaryStuff::create()-> - setId($id)-> - setData($data); - - $stuff = $stuff->dao()->import($stuff); - - Cache::me()->clean(); - - $this->assertEquals( - TestBinaryStuff::dao()->getById($id)->getData(), - $data - ); - - TestBinaryStuff::dao()->dropById($id); - } - - private function getListByIdsTest() - { - $first = TestUser::dao()->getById(1); - - TestUser::dao()->dropIdentityMap(); - - $list = TestUser::dao()->getListByIds(array(1, 3, 2, 1, 1, 1)); - - $this->assertEquals(count($list), 5); - - $this->assertEquals($list[0]->getId(), 1); - $this->assertEquals($list[1]->getId(), 2); - $this->assertEquals($list[2]->getId(), 1); - $this->assertEquals($list[3]->getId(), 1); - $this->assertEquals($list[4]->getId(), 1); - - $this->assertEquals($list[0], $first); - - $this->assertEquals( - array(), - TestUser::dao()->getListByIds(array(42, 42, 1738)) - ); - } - - private function lazyTest() - { - $city = TestCity::dao()->getById(1); - - $object = TestLazy::dao()->add( - TestLazy::create()-> - setCity($city)-> - setCityOptional($city)-> - setEnum( - new ImageType(ImageType::getAnyId()) - )->setStaticEnum( - new MimeType(MimeType::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) - ); - } - } - } - - private function getByEmptyIdTest($id) - { - try { - TestUser::dao()->getById($id); - $this->fail(); - } catch (WrongArgumentException $e) { - // pass - } - } - } -?> \ No newline at end of file diff --git a/test/misc/DBTestCreator.class.php b/test/misc/DBTestCreator.class.php new file mode 100644 index 0000000000..c3550c0bb8 --- /dev/null +++ b/test/misc/DBTestCreator.class.php @@ -0,0 +1,237 @@ +schema = $schema; + return $this; + } + + /** + * @param DBTestPool $testPool + * @return DBTestCreator + */ + public function setTestPool(DBTestPool $testPool) { + $this->pool = $testPool; + return $this; + } + + /** + * @return DBTestCreator + */ + public function createDB() { + /** + * @see testRecursionObjects() and meta + * for TestParentObject and TestChildObject + **/ + $this->schema-> + getTableByName('test_parent_object')-> + getColumnByName('root_id')-> + dropReference(); + + foreach ($this->pool->getPool() as $name => $db) { + foreach ($this->schema->getTables() as $name => $table) { + $db->queryRaw($table->toDialectString($db->getDialect())); + } + } + + return $this; + } + + /** + * @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; + } + + /** + * @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; + } + } +?> \ No newline at end of file diff --git a/test/misc/DBTestPool.class.php b/test/misc/DBTestPool.class.php index 7ad3b8c4e6..c020b60cb1 100644 --- a/test/misc/DBTestPool.class.php +++ b/test/misc/DBTestPool.class.php @@ -1,10 +1,11 @@ \ No newline at end of file diff --git a/test/misc/TestCaseDAO.class.php b/test/misc/TestCaseDAO.class.php new file mode 100644 index 0000000000..0b9160ee9b --- /dev/null +++ b/test/misc/TestCaseDAO.class.php @@ -0,0 +1,11 @@ +getDBCreator()->dropDB(true)->createDB(); + } + } +?> \ No newline at end of file diff --git a/test/misc/TestCaseDB.class.php b/test/misc/TestCaseDB.class.php new file mode 100644 index 0000000000..56c77c72db --- /dev/null +++ b/test/misc/TestCaseDB.class.php @@ -0,0 +1,26 @@ +dBCreator = DBTestCreator::create()-> + setSchemaPath(ONPHP_META_AUTO_DIR.'schema.php')-> + setTestPool(DBTestPool::me()); + } + + public function tearDown() { + parent::tearDown(); + } + + /** + * @return DBTestCreator + */ + protected function getDBCreator() { + return $this->dBCreator; + } + } +?> \ No newline at end of file diff --git a/test/misc/TestSuite.class.php b/test/misc/TestSuite.class.php new file mode 100644 index 0000000000..54b4b28095 --- /dev/null +++ b/test/misc/TestSuite.class.php @@ -0,0 +1,21 @@ +markTestSuiteSkipped('No more workers available.'); + } + } + + public function tearDown() + { + echo "\n"; + } + } +?> \ No newline at end of file diff --git a/test/misc/TestTables.class.php b/test/misc/TestTables.class.php index 6befdf2dd3..ba3cd96a2f 100644 --- a/test/misc/TestTables.class.php +++ b/test/misc/TestTables.class.php @@ -1,6 +1,4 @@